Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

open_ai: Responses API improvements (#56476)

Ben Brandt78c889c21d7e
diff --git a/crates/agent/src/tests/mod.rs b/crates/agent/src/tests/mod.rs
index abae7e55642..7713907f893 100644
--- a/crates/agent/src/tests/mod.rs
+++ b/crates/agent/src/tests/mod.rs
@@ -3123,6 +3123,57 @@ async fn test_truncate_first_message(cx: &mut TestAppContext) {
});
}
+#[gpui::test]
+async fn test_latest_token_usage_counts_cached_input_tokens(cx: &mut TestAppContext) {
+ let ThreadTest { model, thread, .. } = setup(cx, TestModel::Fake).await;
+ let fake_model = model.as_fake();
+
+ let message_1_id = UserMessageId::new();
+ thread
+ .update(cx, |thread, cx| {
+ thread.send(message_1_id, ["Message 1"], cx)
+ })
+ .unwrap();
+ cx.run_until_parked();
+
+ fake_model.send_last_completion_stream_text_chunk("Response 1");
+ fake_model.send_last_completion_stream_event(LanguageModelCompletionEvent::UsageUpdate(
+ language_model::TokenUsage {
+ input_tokens: 100,
+ output_tokens: 50,
+ cache_creation_input_tokens: 25,
+ cache_read_input_tokens: 75,
+ },
+ ));
+ fake_model.end_last_completion_stream();
+ cx.run_until_parked();
+
+ thread.read_with(cx, |thread, _| {
+ assert_eq!(
+ thread.latest_token_usage(),
+ Some(acp_thread::TokenUsage {
+ used_tokens: 250,
+ max_tokens: 1_000_000,
+ max_output_tokens: None,
+ input_tokens: 200,
+ output_tokens: 50,
+ })
+ );
+ });
+
+ let message_2_id = UserMessageId::new();
+ thread
+ .update(cx, |thread, cx| {
+ thread.send(message_2_id.clone(), ["Message 2"], cx)
+ })
+ .unwrap();
+ cx.run_until_parked();
+
+ thread.read_with(cx, |thread, _| {
+ assert_eq!(thread.tokens_before_message(&message_2_id), Some(200));
+ });
+}
+
#[gpui::test]
async fn test_truncate_second_message(cx: &mut TestAppContext) {
let ThreadTest { model, thread, .. } = setup(cx, TestModel::Fake).await;
diff --git a/crates/agent/src/thread.rs b/crates/agent/src/thread.rs
index 921c9e89eba..2220c1543cb 100644
--- a/crates/agent/src/thread.rs
+++ b/crates/agent/src/thread.rs
@@ -1750,11 +1750,13 @@ impl Thread {
pub fn latest_token_usage(&self) -> Option<acp_thread::TokenUsage> {
let usage = self.latest_request_token_usage()?;
let model = self.model.clone()?;
+ let input_tokens = total_input_tokens(usage);
+
Some(acp_thread::TokenUsage {
max_tokens: model.max_token_count(),
max_output_tokens: model.max_output_tokens(),
used_tokens: usage.total_tokens(),
- input_tokens: usage.input_tokens,
+ input_tokens,
output_tokens: usage.output_tokens,
})
}
@@ -1773,7 +1775,7 @@ impl Thread {
if &user_msg.id == target_id {
let prev_id = previous_user_message_id?;
let usage = self.request_token_usage.get(prev_id)?;
- return Some(usage.input_tokens);
+ return Some(total_input_tokens(*usage));
}
previous_user_message_id = Some(&user_msg.id);
}
@@ -3224,6 +3226,13 @@ impl Thread {
}
}
+fn total_input_tokens(usage: language_model::TokenUsage) -> u64 {
+ usage
+ .input_tokens
+ .saturating_add(usage.cache_creation_input_tokens)
+ .saturating_add(usage.cache_read_input_tokens)
+}
+
struct RunningTurn {
/// Holds the task that handles agent interaction until the end of the turn.
/// Survives across multiple requests as the model performs tool calls and
diff --git a/crates/language_model_core/src/language_model_core.rs b/crates/language_model_core/src/language_model_core.rs
index 5f932690869..c6717414730 100644
--- a/crates/language_model_core/src/language_model_core.rs
+++ b/crates/language_model_core/src/language_model_core.rs
@@ -468,6 +468,7 @@ pub enum ModelMode {
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum ReasoningEffort {
+ None,
Minimal,
Low,
Medium,
diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs
index 81c3f07a31c..3ecc4ba02f0 100644
--- a/crates/language_models/src/provider/open_ai.rs
+++ b/crates/language_models/src/provider/open_ai.rs
@@ -217,6 +217,107 @@ impl LanguageModelProvider for OpenAiLanguageModelProvider {
}
}
+fn default_thinking_reasoning_effort(model: &open_ai::Model) -> Option<open_ai::ReasoningEffort> {
+ use open_ai::ReasoningEffort;
+
+ model
+ .reasoning_effort()
+ .filter(|effort| *effort != ReasoningEffort::None)
+ .or_else(|| {
+ let supported_efforts = model.supported_reasoning_efforts();
+ if supported_efforts.contains(&ReasoningEffort::Medium) {
+ Some(ReasoningEffort::Medium)
+ } else {
+ supported_efforts
+ .iter()
+ .copied()
+ .find(|effort| *effort != ReasoningEffort::None)
+ }
+ })
+}
+
+fn supports_selectable_thinking_effort(model: &open_ai::Model) -> bool {
+ model.uses_responses_api()
+ && model
+ .supported_reasoning_efforts()
+ .iter()
+ .any(|effort| *effort != open_ai::ReasoningEffort::None)
+}
+
+fn supported_thinking_effort_levels(model: &open_ai::Model) -> Vec<LanguageModelEffortLevel> {
+ if !supports_selectable_thinking_effort(model) {
+ return Vec::new();
+ }
+
+ let default_effort = default_thinking_reasoning_effort(model);
+ model
+ .supported_reasoning_efforts()
+ .iter()
+ .copied()
+ .filter_map(|effort| {
+ let (name, value) = match effort {
+ open_ai::ReasoningEffort::None => return None,
+ open_ai::ReasoningEffort::Minimal => ("Minimal", "minimal"),
+ open_ai::ReasoningEffort::Low => ("Low", "low"),
+ open_ai::ReasoningEffort::Medium => ("Medium", "medium"),
+ open_ai::ReasoningEffort::High => ("High", "high"),
+ open_ai::ReasoningEffort::XHigh => ("Extra High", "xhigh"),
+ };
+
+ Some(LanguageModelEffortLevel {
+ name: name.into(),
+ value: value.into(),
+ is_default: Some(effort) == default_effort,
+ })
+ })
+ .collect()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn supported_thinking_effort_levels_hide_none() {
+ let effort_levels = supported_thinking_effort_levels(&open_ai::Model::FivePointTwo);
+ let values = effort_levels
+ .iter()
+ .map(|level| level.value.as_ref())
+ .collect::<Vec<_>>();
+
+ assert_eq!(values, ["low", "medium", "high", "xhigh"]);
+ assert_eq!(
+ effort_levels
+ .iter()
+ .find(|level| level.is_default)
+ .map(|level| level.value.as_ref()),
+ Some("medium")
+ );
+ }
+
+ #[test]
+ fn models_supporting_only_none_have_no_selectable_thinking_effort() {
+ let model = open_ai::Model::Custom {
+ name: "custom-model".to_string(),
+ display_name: None,
+ max_tokens: 128_000,
+ max_output_tokens: None,
+ max_completion_tokens: None,
+ reasoning_effort: Some(open_ai::ReasoningEffort::None),
+ supports_chat_completions: false,
+ supports_images: true,
+ };
+
+ assert!(!supports_selectable_thinking_effort(&model));
+ assert!(supported_thinking_effort_levels(&model).is_empty());
+ assert!(
+ model
+ .supported_reasoning_efforts()
+ .contains(&open_ai::ReasoningEffort::None)
+ );
+ }
+}
+
pub struct OpenAiLanguageModel {
id: LanguageModelId,
model: open_ai::Model,
@@ -316,22 +417,20 @@ impl LanguageModel for OpenAiLanguageModel {
use open_ai::Model;
match &self.model {
Model::FourOmniMini
- | Model::FourPointOneNano
| Model::Five
- | Model::FiveCodex
| Model::FiveMini
| Model::FiveNano
| Model::FivePointOne
| Model::FivePointTwo
- | Model::FivePointTwoCodex
| Model::FivePointThreeCodex
| Model::FivePointFour
+ | Model::FivePointFourMini
+ | Model::FivePointFourNano
| Model::FivePointFourPro
| Model::FivePointFive
| Model::FivePointFivePro
- | Model::O1
| Model::O3 => true,
- Model::ThreePointFiveTurbo | Model::Four | Model::FourTurbo | Model::O3Mini => false,
+ Model::Four => false,
Model::Custom {
supports_images, ..
} => *supports_images,
@@ -351,34 +450,11 @@ impl LanguageModel for OpenAiLanguageModel {
}
fn supports_thinking(&self) -> bool {
- self.model.uses_responses_api() && self.model.reasoning_effort().is_some()
+ supports_selectable_thinking_effort(&self.model)
}
fn supported_effort_levels(&self) -> Vec<LanguageModelEffortLevel> {
- if !self.supports_thinking() {
- return Vec::new();
- }
-
- let default_effort = self.model.reasoning_effort();
- self.model
- .supported_reasoning_efforts()
- .iter()
- .map(|effort| {
- let (name, value) = match effort {
- open_ai::ReasoningEffort::Minimal => ("Minimal", "minimal"),
- open_ai::ReasoningEffort::Low => ("Low", "low"),
- open_ai::ReasoningEffort::Medium => ("Medium", "medium"),
- open_ai::ReasoningEffort::High => ("High", "high"),
- open_ai::ReasoningEffort::XHigh => ("Extra High", "xhigh"),
- };
-
- LanguageModelEffortLevel {
- name: name.into(),
- value: value.into(),
- is_default: Some(*effort) == default_effort,
- }
- })
- .collect()
+ supported_thinking_effort_levels(&self.model)
}
fn supports_split_token_display(&self) -> bool {
@@ -418,7 +494,10 @@ impl LanguageModel for OpenAiLanguageModel {
self.model.supports_parallel_tool_calls(),
self.model.supports_prompt_cache_key(),
self.max_output_tokens(),
- self.model.reasoning_effort(),
+ default_thinking_reasoning_effort(&self.model),
+ self.model
+ .supported_reasoning_efforts()
+ .contains(&open_ai::ReasoningEffort::None),
);
let completions = self.stream_response(request, cx);
async move {
diff --git a/crates/language_models/src/provider/open_ai_compatible.rs b/crates/language_models/src/provider/open_ai_compatible.rs
index a80965eced5..b2baf79ecdc 100644
--- a/crates/language_models/src/provider/open_ai_compatible.rs
+++ b/crates/language_models/src/provider/open_ai_compatible.rs
@@ -397,7 +397,10 @@ impl LanguageModel for OpenAiCompatibleLanguageModel {
self.model.capabilities.parallel_tool_calls,
self.model.capabilities.prompt_cache_key,
self.max_output_tokens(),
- self.model.reasoning_effort,
+ self.model
+ .reasoning_effort
+ .filter(|effort| *effort != open_ai::ReasoningEffort::None),
+ self.model.reasoning_effort == Some(open_ai::ReasoningEffort::None),
);
let completions = self.stream_response(request, cx);
async move {
diff --git a/crates/language_models/src/provider/opencode.rs b/crates/language_models/src/provider/opencode.rs
index 647e8496b05..1611694905f 100644
--- a/crates/language_models/src/provider/opencode.rs
+++ b/crates/language_models/src/provider/opencode.rs
@@ -32,6 +32,7 @@ use crate::provider::open_ai::{
fn normalize_reasoning_effort(effort: &str) -> Option<ReasoningEffort> {
match effort.trim().to_ascii_lowercase().as_str() {
+ "none" => Some(ReasoningEffort::None),
"minimal" => Some(ReasoningEffort::Minimal),
"low" => Some(ReasoningEffort::Low),
"medium" => Some(ReasoningEffort::Medium),
@@ -43,6 +44,7 @@ fn normalize_reasoning_effort(effort: &str) -> Option<ReasoningEffort> {
fn reasoning_effort_display(effort: ReasoningEffort) -> (&'static str, &'static str) {
match effort {
+ ReasoningEffort::None => ("None", "none"),
ReasoningEffort::Minimal => ("Minimal", "minimal"),
ReasoningEffort::Low => ("Low", "low"),
ReasoningEffort::Medium => ("Medium", "medium"),
@@ -549,13 +551,17 @@ impl LanguageModel for OpenCodeLanguageModel {
fn supports_thinking(&self) -> bool {
self.model
.supported_reasoning_effort_levels()
- .is_some_and(|levels| !levels.is_empty())
+ .is_some_and(|levels| levels.iter().any(|effort| *effort != ReasoningEffort::None))
}
fn supported_effort_levels(&self) -> Vec<LanguageModelEffortLevel> {
self.model
.supported_reasoning_effort_levels()
.map(|levels| {
+ let levels = levels
+ .into_iter()
+ .filter(|effort| *effort != ReasoningEffort::None)
+ .collect::<Vec<_>>();
if levels.is_empty() {
return Vec::new();
}
@@ -675,21 +681,18 @@ impl LanguageModel for OpenCodeLanguageModel {
.boxed()
}
ApiProtocol::OpenAiResponses => {
- let reasoning_effort = if request.thinking_allowed {
- request
- .thinking_effort
- .as_deref()
- .and_then(normalize_reasoning_effort)
- } else {
- None
- };
+ let supports_none_reasoning_effort = self
+ .model
+ .supported_reasoning_effort_levels()
+ .is_some_and(|levels| levels.contains(&ReasoningEffort::None));
let response_request = into_open_ai_response(
request,
self.model.id(),
false,
false,
self.model.max_output_tokens(),
- reasoning_effort,
+ None,
+ supports_none_reasoning_effort,
);
let stream = self.stream_openai_response(response_request, http_client, cx);
async move {
diff --git a/crates/language_models_cloud/src/language_models_cloud.rs b/crates/language_models_cloud/src/language_models_cloud.rs
index 9d04e100c09..18e3dd66a7d 100644
--- a/crates/language_models_cloud/src/language_models_cloud.rs
+++ b/crates/language_models_cloud/src/language_models_cloud.rs
@@ -460,7 +460,13 @@ impl<TP: CloudLlmTokenProvider + 'static> LanguageModel for CloudLanguageModel<T
let effort = request
.thinking_effort
.as_ref()
- .and_then(|effort| open_ai::ReasoningEffort::from_str(effort).ok());
+ .and_then(|effort| open_ai::ReasoningEffort::from_str(effort).ok())
+ .filter(|effort| *effort != open_ai::ReasoningEffort::None);
+ let supports_none_reasoning_effort =
+ self.model.supported_effort_levels.iter().any(|effort| {
+ open_ai::ReasoningEffort::from_str(&effort.value)
+ .is_ok_and(|effort| effort == open_ai::ReasoningEffort::None)
+ });
let mut request = into_open_ai_response(
request,
@@ -469,6 +475,7 @@ impl<TP: CloudLlmTokenProvider + 'static> LanguageModel for CloudLanguageModel<T
true,
None,
None,
+ supports_none_reasoning_effort,
);
if enable_thinking && let Some(effort) = effort {
diff --git a/crates/open_ai/src/completion.rs b/crates/open_ai/src/completion.rs
index 24c8f3b120e..5be0a5d6286 100644
--- a/crates/open_ai/src/completion.rs
+++ b/crates/open_ai/src/completion.rs
@@ -12,16 +12,21 @@ use std::pin::Pin;
use std::sync::Arc;
use crate::responses::{
- Request as ResponseRequest, ResponseFunctionCallItem, ResponseFunctionCallOutputContent,
- ResponseFunctionCallOutputItem, ResponseInputContent, ResponseInputItem, ResponseMessageItem,
- ResponseOutputItem, ResponseSummary as ResponsesSummary, ResponseUsage as ResponsesUsage,
- StreamEvent as ResponsesStreamEvent,
+ Request as ResponseRequest, ResponseError, ResponseFunctionCallItem,
+ ResponseFunctionCallOutputContent, ResponseFunctionCallOutputItem, ResponseIncludable,
+ ResponseInputContent, ResponseInputItem, ResponseMessageItem, ResponseOutputItem,
+ ResponseOutputMessage, ResponseReasoningInputItem, ResponseReasoningItem,
+ ResponseReasoningSummaryPart, ResponseSummary as ResponsesSummary,
+ ResponseUsage as ResponsesUsage, StreamEvent as ResponsesStreamEvent,
};
use crate::{
FunctionContent, FunctionDefinition, ImageUrl, MessagePart, ReasoningEffort,
ResponseStreamEvent, ToolCall, ToolCallContent,
};
+const RESPONSE_MESSAGE_PHASE_COMMENTARY: &str = "commentary";
+const RESPONSE_MESSAGE_PHASE_FINAL_ANSWER: &str = "final_answer";
+
pub fn into_open_ai(
request: LanguageModelRequest,
model_id: &str,
@@ -177,7 +182,8 @@ pub fn into_open_ai_response(
supports_parallel_tool_calls: bool,
supports_prompt_cache_key: bool,
max_output_tokens: Option<u64>,
- reasoning_effort: Option<ReasoningEffort>,
+ default_reasoning_effort: Option<ReasoningEffort>,
+ supports_none_reasoning_effort: bool,
) -> ResponseRequest {
let stream = !model_id.starts_with("o1-");
@@ -196,8 +202,14 @@ pub fn into_open_ai_response(
} = request;
let mut input_items = Vec::new();
+ let mut replayed_reasoning_item_indexes = HashMap::default();
for (index, message) in messages.into_iter().enumerate() {
- append_message_to_response_items(message, index, &mut input_items);
+ append_message_to_response_items(
+ message,
+ index,
+ &mut replayed_reasoning_item_indexes,
+ &mut input_items,
+ );
}
let tools: Vec<_> = tools
@@ -210,9 +222,46 @@ pub fn into_open_ai_response(
})
.collect();
+ let default_reasoning_effort =
+ default_reasoning_effort.filter(|effort| *effort != ReasoningEffort::None);
+ let reasoning_effort = if thinking_allowed {
+ thinking_effort
+ .as_deref()
+ .and_then(|effort| effort.parse::<ReasoningEffort>().ok())
+ .filter(|effort| *effort != ReasoningEffort::None)
+ .or(default_reasoning_effort)
+ } else if supports_none_reasoning_effort {
+ Some(ReasoningEffort::None)
+ } else {
+ None
+ };
+
+ let reasoning = reasoning_effort.map(|effort| crate::responses::ReasoningConfig {
+ effort,
+ summary: if effort == ReasoningEffort::None {
+ None
+ } else {
+ Some(crate::responses::ReasoningSummaryMode::Auto)
+ },
+ });
+
+ let include = if reasoning
+ .as_ref()
+ .is_some_and(|reasoning| reasoning.effort != ReasoningEffort::None)
+ || input_items
+ .iter()
+ .any(|item| matches!(item, ResponseInputItem::Reasoning(_)))
+ {
+ vec![ResponseIncludable::ReasoningEncryptedContent]
+ } else {
+ Vec::new()
+ };
+
ResponseRequest {
model: model_id.into(),
input: input_items,
+ store: false,
+ include,
stream,
temperature,
top_p: None,
@@ -233,42 +282,55 @@ pub fn into_open_ai_response(
} else {
None
},
- reasoning: if thinking_allowed {
- thinking_effort
- .as_deref()
- .and_then(|effort| effort.parse::<ReasoningEffort>().ok())
- .or(reasoning_effort)
- .map(|effort| crate::responses::ReasoningConfig {
- effort,
- summary: Some(crate::responses::ReasoningSummaryMode::Auto),
- })
- } else {
- None
- },
+ reasoning,
}
}
fn append_message_to_response_items(
message: LanguageModelRequestMessage,
index: usize,
+ replayed_reasoning_item_indexes: &mut HashMap<String, usize>,
input_items: &mut Vec<ResponseInputItem>,
) {
let mut content_parts: Vec<ResponseInputContent> = Vec::new();
- for content in message.content {
+ let LanguageModelRequestMessage {
+ role,
+ content,
+ reasoning_details,
+ ..
+ } = message;
+ let phase = if role == Role::Assistant {
+ response_message_phase_from_details(reasoning_details.as_ref())
+ } else {
+ None
+ };
+
+ if role == Role::Assistant {
+ append_reasoning_details_to_response_items(
+ reasoning_details.as_ref(),
+ replayed_reasoning_item_indexes,
+ input_items,
+ );
+ }
+
+ for content in content {
match content {
MessageContent::Text(text) => {
- push_response_text_part(&message.role, text, &mut content_parts);
- }
- MessageContent::Thinking { text, .. } => {
- push_response_text_part(&message.role, text, &mut content_parts);
+ push_response_text_part(&role, text, &mut content_parts);
}
- MessageContent::RedactedThinking(_) => {}
+ MessageContent::Thinking { .. } | MessageContent::RedactedThinking(_) => {}
MessageContent::Image(image) => {
- push_response_image_part(&message.role, image, &mut content_parts);
+ push_response_image_part(&role, image, &mut content_parts);
}
MessageContent::ToolUse(tool_use) => {
- flush_response_parts(&message.role, index, &mut content_parts, input_items);
+ flush_response_parts(
+ &role,
+ index,
+ phase.as_deref(),
+ &mut content_parts,
+ input_items,
+ );
let call_id = tool_use.id.to_string();
input_items.push(ResponseInputItem::FunctionCall(ResponseFunctionCallItem {
call_id,
@@ -277,7 +339,13 @@ fn append_message_to_response_items(
}));
}
MessageContent::ToolResult(tool_result) => {
- flush_response_parts(&message.role, index, &mut content_parts, input_items);
+ flush_response_parts(
+ &role,
+ index,
+ phase.as_deref(),
+ &mut content_parts,
+ input_items,
+ );
let output = match tool_result.content.as_slice() {
[LanguageModelToolResultContent::Text(text)] => {
ResponseFunctionCallOutputContent::Text(text.to_string())
@@ -312,7 +380,48 @@ fn append_message_to_response_items(
}
}
- flush_response_parts(&message.role, index, &mut content_parts, input_items);
+ flush_response_parts(
+ &role,
+ index,
+ phase.as_deref(),
+ &mut content_parts,
+ input_items,
+ );
+}
+
+fn append_reasoning_details_to_response_items(
+ reasoning_details: Option<&serde_json::Value>,
+ replayed_reasoning_item_indexes: &mut HashMap<String, usize>,
+ input_items: &mut Vec<ResponseInputItem>,
+) {
+ let Some(reasoning_details) = reasoning_details else {
+ return;
+ };
+
+ let Some(metadata) = response_message_metadata_from_details(reasoning_details) else {
+ return;
+ };
+
+ for reasoning_item in metadata.reasoning_items {
+ push_replayed_reasoning_item(reasoning_item, replayed_reasoning_item_indexes, input_items);
+ }
+}
+
+fn push_replayed_reasoning_item(
+ reasoning_item: ResponseReasoningInputItem,
+ replayed_reasoning_item_indexes: &mut HashMap<String, usize>,
+ input_items: &mut Vec<ResponseInputItem>,
+) {
+ if let Some(id) = reasoning_item.id.as_ref() {
+ if let Some(index) = replayed_reasoning_item_indexes.get(id) {
+ input_items[*index] = ResponseInputItem::Reasoning(reasoning_item);
+ return;
+ }
+
+ replayed_reasoning_item_indexes.insert(id.clone(), input_items.len());
+ }
+
+ input_items.push(ResponseInputItem::Reasoning(reasoning_item));
}
fn push_response_text_part(
@@ -353,6 +462,7 @@ fn push_response_image_part(
fn flush_response_parts(
role: &Role,
_index: usize,
+ phase: Option<&str>,
parts: &mut Vec<ResponseInputContent>,
input_items: &mut Vec<ResponseInputItem>,
) {
@@ -367,6 +477,10 @@ fn flush_response_parts(
Role::System => crate::Role::System,
},
content: parts.clone(),
+ phase: match role {
+ Role::Assistant => phase.map(str::to_string),
+ Role::User | Role::System => None,
+ },
});
input_items.push(item);
@@ -555,6 +669,8 @@ struct RawToolCall {
pub struct OpenAiResponseEventMapper {
function_calls_by_item: HashMap<String, PendingResponseFunctionCall>,
+ reasoning_items: Vec<ResponseReasoningInputItem>,
+ current_message_phase: Option<String>,
pending_stop_reason: Option<StopReason>,
}
@@ -569,6 +685,8 @@ impl OpenAiResponseEventMapper {
pub fn new() -> Self {
Self {
function_calls_by_item: HashMap::default(),
+ reasoning_items: Vec::new(),
+ current_message_phase: None,
pending_stop_reason: None,
}
}
@@ -601,6 +719,7 @@ impl OpenAiResponseEventMapper {
message_id: id.clone(),
}));
}
+ events.extend(self.capture_message_phase(message));
}
ResponseOutputItem::FunctionCall(function_call) => {
if let Some(item_id) = function_call.id.clone() {
@@ -640,6 +759,11 @@ impl OpenAiResponseEventMapper {
vec![Ok(LanguageModelCompletionEvent::Text(delta))]
}
}
+ ResponsesStreamEvent::RefusalDelta { .. }
+ | ResponsesStreamEvent::RefusalDone { .. } => {
+ self.pending_stop_reason = Some(StopReason::Refusal);
+ Vec::new()
+ }
ResponsesStreamEvent::FunctionCallArgumentsDelta { item_id, delta, .. } => {
if let Some(entry) = self.function_calls_by_item.get_mut(&item_id) {
entry.arguments.push_str(&delta);
@@ -700,11 +824,11 @@ impl OpenAiResponseEventMapper {
}
ResponsesStreamEvent::Incomplete { response } => {
let reason = response
- .status_details
+ .incomplete_details
.as_ref()
.and_then(|details| details.reason.as_deref());
- let stop_reason = match reason {
- Some("max_output_tokens") => StopReason::MaxTokens,
+ let mut stop_reason = match reason {
+ Some("max_tokens" | "max_output_tokens") => StopReason::MaxTokens,
Some("content_filter") => {
self.pending_stop_reason = Some(StopReason::Refusal);
StopReason::Refusal
@@ -716,6 +840,13 @@ impl OpenAiResponseEventMapper {
};
let mut events = Vec::new();
+ events.extend(self.capture_reasoning_items_from_output(&response.output));
+ if response_output_contains_refusal(&response.output)
+ && !matches!(stop_reason, StopReason::MaxTokens)
+ {
+ self.pending_stop_reason = Some(StopReason::Refusal);
+ stop_reason = StopReason::Refusal;
+ }
if self.pending_stop_reason.is_none() {
events.extend(self.emit_tool_calls_from_output(&response.output));
}
@@ -728,17 +859,13 @@ impl OpenAiResponseEventMapper {
events
}
ResponsesStreamEvent::Failed { response } => {
- let message = response
- .status_details
- .and_then(|details| details.error)
- .map(|error| error.to_string())
- .unwrap_or_else(|| "response failed".to_string());
+ let message = response_failure_message(&response);
vec![Err(LanguageModelCompletionError::Other(anyhow!(message)))]
}
ResponsesStreamEvent::Error { error }
| ResponsesStreamEvent::GenericError { error } => {
vec![Err(LanguageModelCompletionError::Other(anyhow!(
- error.message
+ response_error_message(&error)
)))]
}
ResponsesStreamEvent::ReasoningSummaryPartAdded { summary_index, .. } => {
@@ -751,8 +878,12 @@ impl OpenAiResponseEventMapper {
Vec::new()
}
}
+ ResponsesStreamEvent::OutputItemDone { item, .. } => match item {
+ ResponseOutputItem::Reasoning(reasoning) => self.capture_reasoning_item(&reasoning),
+ ResponseOutputItem::Message(message) => self.capture_message_phase(&message),
+ ResponseOutputItem::FunctionCall(_) | ResponseOutputItem::Unknown => Vec::new(),
+ },
ResponsesStreamEvent::OutputTextDone { .. }
- | ResponsesStreamEvent::OutputItemDone { .. }
| ResponsesStreamEvent::ContentPartAdded { .. }
| ResponsesStreamEvent::ContentPartDone { .. }
| ResponsesStreamEvent::ReasoningSummaryTextDone { .. }
@@ -770,6 +901,12 @@ impl OpenAiResponseEventMapper {
) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
let mut events = Vec::new();
+ events.extend(self.capture_reasoning_items_from_output(&response.output));
+
+ if response_output_contains_refusal(&response.output) {
+ self.pending_stop_reason = Some(StopReason::Refusal);
+ }
+
if self.pending_stop_reason.is_none() {
events.extend(self.emit_tool_calls_from_output(&response.output));
}
@@ -832,23 +969,198 @@ impl OpenAiResponseEventMapper {
}
events
}
+
+ fn capture_reasoning_items_from_output(
+ &mut self,
+ output: &[ResponseOutputItem],
+ ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
+ let mut events = Vec::new();
+ for item in output {
+ if let ResponseOutputItem::Reasoning(reasoning) = item {
+ events.extend(self.capture_reasoning_item(reasoning));
+ }
+ }
+ events
+ }
+
+ fn capture_message_phase(
+ &mut self,
+ message: &ResponseOutputMessage,
+ ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
+ self.current_message_phase = message
+ .phase
+ .as_deref()
+ .and_then(normalize_response_message_phase)
+ .map(str::to_string);
+
+ if self.current_message_phase.is_none() && self.reasoning_items.is_empty() {
+ return Vec::new();
+ }
+
+ self.emit_response_message_metadata()
+ }
+
+ fn capture_reasoning_item(
+ &mut self,
+ reasoning: &ResponseReasoningItem,
+ ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
+ let reasoning_item = response_reasoning_input_item_from_output(reasoning);
+
+ if self.reasoning_items.contains(&reasoning_item) {
+ return Vec::new();
+ }
+
+ if let Some(id) = reasoning_item.id.as_ref()
+ && let Some(existing_reasoning_item) = self
+ .reasoning_items
+ .iter_mut()
+ .find(|existing_reasoning_item| existing_reasoning_item.id.as_ref() == Some(id))
+ {
+ *existing_reasoning_item = reasoning_item;
+ } else {
+ self.reasoning_items.push(reasoning_item);
+ }
+
+ self.emit_response_message_metadata()
+ }
+
+ fn emit_response_message_metadata(
+ &self,
+ ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
+ let details = serde_json::to_value(ResponseMessageMetadata {
+ phase: self.current_message_phase.clone(),
+ reasoning_items: self.reasoning_items.clone(),
+ });
+
+ match details {
+ Ok(details) => vec![Ok(LanguageModelCompletionEvent::ReasoningDetails(details))],
+ Err(error) => vec![Err(LanguageModelCompletionError::Other(anyhow!(error)))],
+ }
+ }
+}
+
+#[derive(serde::Serialize, serde::Deserialize)]
+struct ResponseMessageMetadata {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ phase: Option<String>,
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ reasoning_items: Vec<ResponseReasoningInputItem>,
+}
+
+fn response_message_metadata_from_details(
+ details: &serde_json::Value,
+) -> Option<ResponseMessageMetadata> {
+ serde_json::from_value::<ResponseMessageMetadata>(details.clone()).ok()
+}
+
+fn response_message_phase_from_details(details: Option<&serde_json::Value>) -> Option<String> {
+ let metadata = response_message_metadata_from_details(details?)?;
+ metadata
+ .phase
+ .as_deref()
+ .and_then(normalize_response_message_phase)
+ .map(str::to_string)
+}
+
+fn normalize_response_message_phase(phase: &str) -> Option<&'static str> {
+ match phase {
+ RESPONSE_MESSAGE_PHASE_COMMENTARY => Some(RESPONSE_MESSAGE_PHASE_COMMENTARY),
+ RESPONSE_MESSAGE_PHASE_FINAL_ANSWER => Some(RESPONSE_MESSAGE_PHASE_FINAL_ANSWER),
+ _ => None,
+ }
+}
+
+fn response_failure_message(response: &ResponsesSummary) -> String {
+ if let Some(error) = response.error.as_ref() {
+ return response_error_message(error);
+ }
+
+ response
+ .status
+ .as_deref()
+ .map(|status| format!("response.{status}"))
+ .unwrap_or_else(|| "response.failed".to_string())
+}
+
+fn response_error_message(error: &ResponseError) -> String {
+ let code = error.code.as_deref().filter(|code| !code.trim().is_empty());
+ let message = error.message.trim();
+
+ match (code, message.is_empty()) {
+ (Some(code), false) => format!("{code}: {message}"),
+ (Some(code), true) => code.to_string(),
+ (None, false) => message.to_string(),
+ (None, true) => "response error".to_string(),
+ }
+}
+
+fn response_output_contains_refusal(output: &[ResponseOutputItem]) -> bool {
+ output.iter().any(|item| {
+ if let ResponseOutputItem::Message(message) = item {
+ message.content.iter().any(response_content_is_refusal)
+ } else {
+ false
+ }
+ })
+}
+
+fn response_content_is_refusal(content: &serde_json::Value) -> bool {
+ let content_type = content
+ .get("type")
+ .and_then(|content_type| content_type.as_str());
+ let refusal = content
+ .get("refusal")
+ .and_then(|refusal| refusal.as_str())
+ .unwrap_or_default();
+
+ content_type == Some("refusal") || !refusal.is_empty()
}
fn token_usage_from_response_usage(usage: &ResponsesUsage) -> TokenUsage {
+ let cache_read_input_tokens = usage.input_tokens_details.cached_tokens;
+
TokenUsage {
- input_tokens: usage.input_tokens.unwrap_or_default(),
+ input_tokens: usage
+ .input_tokens
+ .unwrap_or_default()
+ .saturating_sub(cache_read_input_tokens),
output_tokens: usage.output_tokens.unwrap_or_default(),
cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
+ cache_read_input_tokens,
+ }
+}
+
+fn response_reasoning_input_item_from_output(
+ reasoning: &ResponseReasoningItem,
+) -> ResponseReasoningInputItem {
+ let encrypted_content = reasoning.encrypted_content.clone();
+
+ let summary = reasoning
+ .summary
+ .iter()
+ .filter_map(|part| match part {
+ crate::responses::ReasoningSummaryPart::SummaryText { text } => {
+ Some(ResponseReasoningSummaryPart::SummaryText { text: text.clone() })
+ }
+ crate::responses::ReasoningSummaryPart::Unknown => None,
+ })
+ .collect();
+
+ ResponseReasoningInputItem {
+ id: reasoning.id.clone(),
+ summary,
+ content: reasoning.content.clone(),
+ encrypted_content,
+ status: reasoning.status.clone(),
}
}
#[cfg(test)]
mod tests {
use crate::responses::{
- ReasoningSummaryPart, ResponseFunctionToolCall, ResponseOutputItem, ResponseOutputMessage,
- ResponseReasoningItem, ResponseStatusDetails, ResponseSummary, ResponseUsage,
- StreamEvent as ResponsesStreamEvent,
+ ReasoningSummaryPart, ResponseError, ResponseFunctionToolCall, ResponseIncompleteDetails,
+ ResponseInputTokensDetails, ResponseOutputItem, ResponseOutputMessage,
+ ResponseReasoningItem, ResponseSummary, ResponseUsage, StreamEvent as ResponsesStreamEvent,
};
use futures::{StreamExt, executor::block_on};
use language_model_core::{
@@ -893,6 +1205,7 @@ mod tests {
role: Some("assistant".to_string()),
status: Some("in_progress".to_string()),
content: vec![],
+ phase: None,
})
}
@@ -906,6 +1219,21 @@ mod tests {
})
}
+ fn response_reasoning_item(
+ id: &str,
+ summary: Vec<ReasoningSummaryPart>,
+ encrypted_content: Option<&str>,
+ status: Option<String>,
+ ) -> ResponseReasoningItem {
+ ResponseReasoningItem {
+ id: Some(id.to_string()),
+ summary,
+ content: Vec::new(),
+ encrypted_content: encrypted_content.map(str::to_string),
+ status,
+ }
+ }
+
#[test]
fn responses_stream_maps_text_and_usage() {
let events = vec![
@@ -924,8 +1252,10 @@ mod tests {
response: ResponseSummary {
usage: Some(ResponseUsage {
input_tokens: Some(5),
+ input_tokens_details: ResponseInputTokensDetails { cached_tokens: 2 },
output_tokens: Some(3),
total_tokens: Some(8),
+ ..Default::default()
}),
..Default::default()
},
@@ -944,8 +1274,9 @@ mod tests {
assert!(matches!(
mapped[2],
LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
- input_tokens: 5,
+ input_tokens: 3,
output_tokens: 3,
+ cache_read_input_tokens: 2,
..
})
));
@@ -955,6 +1286,34 @@ mod tests {
));
}
+ #[test]
+ fn response_usage_deserializes_cached_tokens() -> Result<()> {
+ let usage: ResponseUsage = serde_json::from_value(json!({
+ "input_tokens": 5,
+ "input_tokens_details": {
+ "cached_tokens": 2,
+ },
+ "output_tokens": 3,
+ "output_tokens_details": {
+ "reasoning_tokens": 1,
+ },
+ "total_tokens": 8,
+ }))?;
+
+ assert_eq!(usage.output_tokens_details.reasoning_tokens, 1);
+ assert_eq!(
+ token_usage_from_response_usage(&usage),
+ TokenUsage {
+ input_tokens: 3,
+ output_tokens: 3,
+ cache_creation_input_tokens: 0,
+ cache_read_input_tokens: 2,
+ }
+ );
+
+ Ok(())
+ }
+
#[test]
fn into_open_ai_response_builds_complete_payload() {
let tool_call_id = LanguageModelToolUseId::from("call-42");
@@ -1038,6 +1397,7 @@ mod tests {
true,
Some(2048),
Some(ReasoningEffort::Low),
+ false,
);
let serialized = serde_json::to_value(&response).unwrap();
@@ -1078,6 +1438,8 @@ mod tests {
"output": "Sunny"
}
],
+ "store": false,
+ "include": ["reasoning.encrypted_content"],
"stream": true,
"max_output_tokens": 2048,
"parallel_tool_calls": true,
@@ -1098,7 +1460,176 @@ mod tests {
}
#[test]
- fn into_open_ai_response_omits_reasoning_when_thinking_is_disabled() {
+ fn into_open_ai_response_replays_encrypted_reasoning_details() {
+ let tool_call_id = LanguageModelToolUseId::from("call-42");
+ let tool_arguments = "{\"city\":\"Boston\"}".to_string();
+ let tool_use = LanguageModelToolUse {
+ id: tool_call_id,
+ name: Arc::from("get_weather"),
+ raw_input: tool_arguments.clone(),
+ input: json!({ "city": "Boston" }),
+ is_input_complete: true,
+ thought_signature: None,
+ };
+
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![LanguageModelRequestMessage {
+ role: Role::Assistant,
+ content: vec![MessageContent::ToolUse(tool_use)],
+ cache: false,
+ reasoning_details: Some(json!({
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "Checked what information is needed."
+ }
+ ],
+ "content": [
+ {
+ "type": "reasoning_text",
+ "text": "Internal reasoning text."
+ }
+ ],
+ "encrypted_content": "ENC",
+ "status": "completed",
+ }
+ ]
+ })),
+ }],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: false,
+ thinking_effort: None,
+ speed: None,
+ };
+
+ let response = into_open_ai_response(
+ request,
+ "gpt-5",
+ true,
+ true,
+ None,
+ Some(ReasoningEffort::Low),
+ false,
+ );
+
+ let serialized = serde_json::to_value(&response).unwrap();
+ assert_eq!(
+ serialized["input"],
+ json!([
+ {
+ "type": "reasoning",
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "Checked what information is needed."
+ }
+ ],
+ "content": [
+ {
+ "type": "reasoning_text",
+ "text": "Internal reasoning text."
+ }
+ ],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ },
+ {
+ "type": "function_call",
+ "call_id": "call-42",
+ "name": "get_weather",
+ "arguments": tool_arguments
+ }
+ ])
+ );
+ assert_eq!(
+ serialized["include"],
+ json!(["reasoning.encrypted_content"])
+ );
+ assert_eq!(serialized.get("reasoning"), None);
+ }
+
+ #[test]
+ fn into_open_ai_response_replays_reasoning_without_encrypted_content() {
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![LanguageModelRequestMessage {
+ role: Role::Assistant,
+ content: vec![MessageContent::Text("Done.".into())],
+ cache: false,
+ reasoning_details: Some(json!({
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [],
+ "status": "completed"
+ },
+ {
+ "id": "rs_456",
+ "summary": [],
+ "encrypted_content": "",
+ "status": "completed"
+ }
+ ]
+ })),
+ }],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: false,
+ thinking_effort: None,
+ speed: None,
+ };
+
+ let response =
+ into_open_ai_response(request, "custom-model", false, false, None, None, false);
+ let serialized = serde_json::to_value(&response).unwrap();
+
+ assert_eq!(
+ serialized["input"],
+ json!([
+ {
+ "type": "reasoning",
+ "id": "rs_123",
+ "summary": [],
+ "status": "completed"
+ },
+ {
+ "type": "reasoning",
+ "id": "rs_456",
+ "summary": [],
+ "encrypted_content": "",
+ "status": "completed"
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ {
+ "type": "output_text",
+ "text": "Done.",
+ "annotations": []
+ }
+ ]
+ }
+ ])
+ );
+ }
+
+ #[test]
+ fn into_open_ai_response_omits_reasoning_when_thinking_is_disabled_and_none_is_unsupported() {
let request = LanguageModelRequest {
thread_id: None,
prompt_id: None,
@@ -1125,6 +1656,7 @@ mod tests {
true,
None,
Some(ReasoningEffort::Medium),
+ false,
);
let serialized = serde_json::to_value(&response).unwrap();
@@ -1132,24 +1664,347 @@ mod tests {
}
#[test]
- fn responses_stream_maps_tool_calls() {
- let events = vec![
- ResponsesStreamEvent::OutputItemAdded {
- output_index: 0,
- sequence_number: None,
- item: response_item_function_call("item_fn", Some("{\"city\":\"Bos")),
- },
- ResponsesStreamEvent::FunctionCallArgumentsDelta {
- item_id: "item_fn".into(),
- output_index: 0,
- delta: "ton\"}".into(),
- sequence_number: None,
- },
- ResponsesStreamEvent::FunctionCallArgumentsDone {
- item_id: "item_fn".into(),
- output_index: 0,
- arguments: "{\"city\":\"Boston\"}".into(),
- sequence_number: None,
+ fn into_open_ai_response_sends_none_reasoning_when_thinking_is_disabled() -> Result<()> {
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![LanguageModelRequestMessage {
+ role: Role::User,
+ content: vec![MessageContent::Text("Hello".into())],
+ cache: false,
+ reasoning_details: None,
+ }],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: false,
+ thinking_effort: Some("high".into()),
+ speed: None,
+ };
+
+ let response = into_open_ai_response(
+ request,
+ "gpt-5.1",
+ true,
+ true,
+ None,
+ Some(ReasoningEffort::Medium),
+ true,
+ );
+
+ let serialized = serde_json::to_value(&response)?;
+ assert_eq!(serialized["reasoning"], json!({ "effort": "none" }));
+ assert_eq!(serialized.get("include"), None);
+
+ Ok(())
+ }
+
+ #[test]
+ fn into_open_ai_response_uses_default_effort_when_selected_effort_is_none() -> Result<()> {
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![LanguageModelRequestMessage {
+ role: Role::User,
+ content: vec![MessageContent::Text("Hello".into())],
+ cache: false,
+ reasoning_details: None,
+ }],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: true,
+ thinking_effort: Some("none".into()),
+ speed: None,
+ };
+
+ let response = into_open_ai_response(
+ request,
+ "gpt-5.1",
+ true,
+ true,
+ None,
+ Some(ReasoningEffort::Medium),
+ true,
+ );
+
+ let serialized = serde_json::to_value(&response)?;
+ assert_eq!(
+ serialized["reasoning"],
+ json!({ "effort": "medium", "summary": "auto" })
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn into_open_ai_response_replays_assistant_phase() {
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![LanguageModelRequestMessage {
+ role: Role::Assistant,
+ content: vec![MessageContent::Text("Done.".into())],
+ cache: false,
+ reasoning_details: Some(json!({
+ "phase": "final_answer",
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ }
+ ]
+ })),
+ }],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: true,
+ thinking_effort: None,
+ speed: None,
+ };
+
+ let response = into_open_ai_response(
+ request,
+ "gpt-5.3-codex",
+ true,
+ true,
+ None,
+ Some(ReasoningEffort::Medium),
+ false,
+ );
+
+ let serialized = serde_json::to_value(&response).unwrap();
+ assert_eq!(
+ serialized["input"],
+ json!([
+ {
+ "type": "reasoning",
+ "id": "rs_123",
+ "summary": [],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ { "type": "output_text", "text": "Done.", "annotations": [] }
+ ],
+ "phase": "final_answer"
+ }
+ ])
+ );
+ }
+
+ #[test]
+ fn into_open_ai_response_deduplicates_replayed_reasoning_items() {
+ let first_reasoning_details = json!({
+ "phase": "final_answer",
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [],
+ "encrypted_content": "ENC_OLD",
+ "status": "in_progress"
+ }
+ ]
+ });
+ let second_reasoning_details = json!({
+ "phase": "final_answer",
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "Later metadata has the complete summary."
+ }
+ ],
+ "encrypted_content": "ENC_NEW",
+ "status": "completed"
+ }
+ ]
+ });
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![
+ LanguageModelRequestMessage {
+ role: Role::Assistant,
+ content: vec![MessageContent::Text("First.".into())],
+ cache: false,
+ reasoning_details: Some(first_reasoning_details),
+ },
+ LanguageModelRequestMessage {
+ role: Role::Assistant,
+ content: vec![MessageContent::Text("Second.".into())],
+ cache: false,
+ reasoning_details: Some(second_reasoning_details),
+ },
+ ],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: true,
+ thinking_effort: None,
+ speed: None,
+ };
+
+ let response = into_open_ai_response(
+ request,
+ "gpt-5.3-codex",
+ true,
+ true,
+ None,
+ Some(ReasoningEffort::Medium),
+ false,
+ );
+
+ let serialized = serde_json::to_value(&response).unwrap();
+ assert_eq!(
+ serialized["input"],
+ json!([
+ {
+ "type": "reasoning",
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "Later metadata has the complete summary."
+ }
+ ],
+ "encrypted_content": "ENC_NEW",
+ "status": "completed"
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ { "type": "output_text", "text": "First.", "annotations": [] }
+ ],
+ "phase": "final_answer"
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ { "type": "output_text", "text": "Second.", "annotations": [] }
+ ],
+ "phase": "final_answer"
+ }
+ ])
+ );
+ }
+
+ #[test]
+ fn into_open_ai_response_replays_reasoning_details_but_not_thinking_text() {
+ let request = LanguageModelRequest {
+ thread_id: None,
+ prompt_id: None,
+ intent: None,
+ messages: vec![LanguageModelRequestMessage {
+ role: Role::Assistant,
+ content: vec![
+ MessageContent::Thinking {
+ text: "This is a reasoning summary, not assistant output.".into(),
+ signature: None,
+ },
+ MessageContent::Text("This is visible assistant output.".into()),
+ ],
+ cache: false,
+ reasoning_details: Some(json!({
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "This is the reasoning summary to preserve."
+ }
+ ],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ }
+ ]
+ })),
+ }],
+ tools: Vec::new(),
+ tool_choice: None,
+ stop: Vec::new(),
+ temperature: None,
+ thinking_allowed: false,
+ thinking_effort: None,
+ speed: None,
+ };
+
+ let response =
+ into_open_ai_response(request, "custom-model", false, false, None, None, false);
+ let serialized = serde_json::to_value(&response).unwrap();
+
+ assert_eq!(
+ serialized["input"],
+ json!([
+ {
+ "type": "reasoning",
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "This is the reasoning summary to preserve."
+ }
+ ],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ {
+ "type": "output_text",
+ "text": "This is visible assistant output.",
+ "annotations": []
+ }
+ ]
+ }
+ ])
+ );
+ assert_eq!(
+ serialized["include"],
+ json!(["reasoning.encrypted_content"])
+ );
+ }
+
+ #[test]
+ fn responses_stream_maps_tool_calls() {
+ let events = vec![
+ ResponsesStreamEvent::OutputItemAdded {
+ output_index: 0,
+ sequence_number: None,
+ item: response_item_function_call("item_fn", Some("{\"city\":\"Bos")),
+ },
+ ResponsesStreamEvent::FunctionCallArgumentsDelta {
+ item_id: "item_fn".into(),
+ output_index: 0,
+ delta: "ton\"}".into(),
+ sequence_number: None,
+ },
+ ResponsesStreamEvent::FunctionCallArgumentsDone {
+ item_id: "item_fn".into(),
+ output_index: 0,
+ arguments: "{\"city\":\"Boston\"}".into(),
+ sequence_number: None,
},
ResponsesStreamEvent::Completed {
response: ResponseSummary::default(),
@@ -1187,15 +2042,14 @@ mod tests {
fn responses_stream_uses_max_tokens_stop_reason() {
let events = vec![ResponsesStreamEvent::Incomplete {
response: ResponseSummary {
- status_details: Some(ResponseStatusDetails {
- reason: Some("max_output_tokens".into()),
- r#type: Some("incomplete".into()),
- error: None,
+ incomplete_details: Some(ResponseIncompleteDetails {
+ reason: Some("max_tokens".into()),
}),
usage: Some(ResponseUsage {
input_tokens: Some(10),
output_tokens: Some(20),
total_tokens: Some(30),
+ ..Default::default()
}),
..Default::default()
},
@@ -1216,6 +2070,128 @@ mod tests {
));
}
+ #[test]
+ fn responses_stream_failed_uses_response_error_message() {
+ let mut mapper = OpenAiResponseEventMapper::new();
+ let mapped = mapper.map_event(ResponsesStreamEvent::Failed {
+ response: ResponseSummary {
+ status: Some("failed".into()),
+ error: Some(ResponseError {
+ code: Some("server_error".into()),
+ message: "The model failed to generate a response.".into(),
+ param: None,
+ }),
+ ..Default::default()
+ },
+ });
+
+ assert_eq!(mapped.len(), 1);
+ let error = mapped.into_iter().next().unwrap().unwrap_err();
+ assert_eq!(
+ error.to_string(),
+ "server_error: The model failed to generate a response."
+ );
+ }
+
+ #[test]
+ fn responses_stream_deserializes_documented_error_event() {
+ let event = serde_json::from_value::<ResponsesStreamEvent>(json!({
+ "type": "error",
+ "code": "ERR_SOMETHING",
+ "message": "Something went wrong",
+ "param": null,
+ "sequence_number": 1
+ }))
+ .expect("documented error event");
+
+ let mut mapper = OpenAiResponseEventMapper::new();
+ let mapped = mapper.map_event(event);
+
+ assert_eq!(mapped.len(), 1);
+ let error = mapped.into_iter().next().unwrap().unwrap_err();
+ assert_eq!(error.to_string(), "ERR_SOMETHING: Something went wrong");
+ }
+
+ #[test]
+ fn responses_stream_deserializes_response_error_event() {
+ let event = serde_json::from_value::<ResponsesStreamEvent>(json!({
+ "type": "response.error",
+ "error": {
+ "code": "invalid_request_error",
+ "message": "Invalid request."
+ }
+ }))
+ .expect("response error event");
+
+ let mut mapper = OpenAiResponseEventMapper::new();
+ let mapped = mapper.map_event(event);
+
+ assert_eq!(mapped.len(), 1);
+ let error = mapped.into_iter().next().unwrap().unwrap_err();
+ assert_eq!(error.to_string(), "invalid_request_error: Invalid request.");
+ }
+
+ #[test]
+ fn responses_stream_maps_refusal_events_to_refusal_stop() {
+ let delta = serde_json::from_value::<ResponsesStreamEvent>(json!({
+ "type": "response.refusal.delta",
+ "item_id": "msg_123",
+ "output_index": 0,
+ "content_index": 0,
+ "delta": "I can't help",
+ "sequence_number": 1
+ }))
+ .expect("documented refusal delta event");
+ let done = serde_json::from_value::<ResponsesStreamEvent>(json!({
+ "type": "response.refusal.done",
+ "item_id": "msg_123",
+ "output_index": 0,
+ "content_index": 0,
+ "refusal": "I can't help with that.",
+ "sequence_number": 2
+ }))
+ .expect("documented refusal done event");
+
+ let mapped = map_response_events(vec![
+ delta,
+ done,
+ ResponsesStreamEvent::Completed {
+ response: ResponseSummary::default(),
+ },
+ ]);
+
+ assert_eq!(mapped.len(), 1);
+ assert!(matches!(
+ mapped[0],
+ LanguageModelCompletionEvent::Stop(StopReason::Refusal)
+ ));
+ }
+
+ #[test]
+ fn responses_stream_maps_refusal_output_to_refusal_stop() {
+ let mapped = map_response_events(vec![ResponsesStreamEvent::Completed {
+ response: ResponseSummary {
+ output: vec![ResponseOutputItem::Message(ResponseOutputMessage {
+ id: Some("msg_123".into()),
+ role: Some("assistant".into()),
+ status: Some("completed".into()),
+ content: vec![json!({
+ "type": "refusal",
+ "refusal": "I can't help with that."
+ })],
+ phase: None,
+ })],
+ ..Default::default()
+ },
+ }]);
+
+ assert_eq!(mapped.len(), 1);
+ assert!(matches!(
+ mapped[0],
+ LanguageModelCompletionEvent::Stop(StopReason::Refusal)
+ ));
+ }
+
#[test]
fn responses_stream_handles_multiple_tool_calls() {
let events = vec![
@@ -1354,10 +2330,8 @@ mod tests {
},
ResponsesStreamEvent::Incomplete {
response: ResponseSummary {
- status_details: Some(ResponseStatusDetails {
- reason: Some("max_output_tokens".into()),
- r#type: Some("incomplete".into()),
- error: None,
+ incomplete_details: Some(ResponseIncompleteDetails {
+ reason: Some("max_tokens".into()),
}),
output: vec![response_item_function_call(
"item_fn",
@@ -1402,10 +2376,8 @@ mod tests {
},
ResponsesStreamEvent::Incomplete {
response: ResponseSummary {
- status_details: Some(ResponseStatusDetails {
- reason: Some("max_output_tokens".into()),
- r#type: Some("incomplete".into()),
- error: None,
+ incomplete_details: Some(ResponseIncompleteDetails {
+ reason: Some("max_tokens".into()),
}),
output: vec![response_item_function_call(
"item_fn",
@@ -1543,10 +2515,12 @@ mod tests {
ResponsesStreamEvent::OutputItemAdded {
output_index: 0,
sequence_number: None,
- item: ResponseOutputItem::Reasoning(ResponseReasoningItem {
- id: Some("rs_123".into()),
- summary: vec![],
- }),
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_123",
+ vec![],
+ None,
+ None,
+ )),
},
ResponsesStreamEvent::ReasoningSummaryPartAdded {
item_id: "rs_123".into(),
@@ -1596,9 +2570,9 @@ mod tests {
ResponsesStreamEvent::OutputItemDone {
output_index: 0,
sequence_number: None,
- item: ResponseOutputItem::Reasoning(ResponseReasoningItem {
- id: Some("rs_123".into()),
- summary: vec![
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_123",
+ vec![
ReasoningSummaryPart::SummaryText {
text: "Thinking about the answer".into(),
},
@@ -1606,7 +2580,9 @@ mod tests {
text: "Second part".into(),
},
],
- }),
+ None,
+ None,
+ )),
},
ResponsesStreamEvent::OutputItemAdded {
output_index: 1,
@@ -1661,20 +2637,24 @@ mod tests {
ResponsesStreamEvent::OutputItemAdded {
output_index: 0,
sequence_number: None,
- item: ResponseOutputItem::Reasoning(ResponseReasoningItem {
- id: Some("rs_789".into()),
- summary: vec![],
- }),
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_789",
+ vec![],
+ None,
+ None,
+ )),
},
ResponsesStreamEvent::OutputItemDone {
output_index: 0,
sequence_number: None,
- item: ResponseOutputItem::Reasoning(ResponseReasoningItem {
- id: Some("rs_789".into()),
- summary: vec![ReasoningSummaryPart::SummaryText {
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_789",
+ vec![ReasoningSummaryPart::SummaryText {
text: "Summary without deltas".into(),
}],
- }),
+ None,
+ None,
+ )),
},
ResponsesStreamEvent::Completed {
response: ResponseSummary::default(),
@@ -1690,6 +2670,252 @@ mod tests {
);
}
+ #[test]
+ fn responses_stream_preserves_encrypted_reasoning_details() {
+ let mut reasoning_item = response_reasoning_item(
+ "rs_123",
+ vec![ReasoningSummaryPart::SummaryText {
+ text: "Checked what information is needed.".into(),
+ }],
+ Some("ENC"),
+ Some("completed".into()),
+ );
+ reasoning_item.content = vec![json!({
+ "type": "reasoning_text",
+ "text": "Internal reasoning text."
+ })];
+
+ let events = vec![
+ ResponsesStreamEvent::OutputItemDone {
+ output_index: 0,
+ sequence_number: None,
+ item: ResponseOutputItem::Reasoning(reasoning_item),
+ },
+ ResponsesStreamEvent::Completed {
+ response: ResponseSummary::default(),
+ },
+ ];
+
+ let mapped = map_response_events(events);
+ let details = mapped
+ .iter()
+ .find_map(|event| match event {
+ LanguageModelCompletionEvent::ReasoningDetails(details) => Some(details),
+ _ => None,
+ })
+ .expect("reasoning details");
+
+ assert_eq!(
+ details,
+ &json!({
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "Checked what information is needed."
+ }
+ ],
+ "content": [
+ {
+ "type": "reasoning_text",
+ "text": "Internal reasoning text."
+ }
+ ],
+ "encrypted_content": "ENC",
+ "status": "completed",
+ }
+ ]
+ })
+ );
+ }
+
+ #[test]
+ fn responses_stream_replaces_reasoning_details_with_same_id() {
+ let events = vec![
+ ResponsesStreamEvent::OutputItemDone {
+ output_index: 0,
+ sequence_number: None,
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_123",
+ Vec::new(),
+ Some("ENC_OLD"),
+ Some("in_progress".into()),
+ )),
+ },
+ ResponsesStreamEvent::OutputItemDone {
+ output_index: 0,
+ sequence_number: None,
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_123",
+ vec![ReasoningSummaryPart::SummaryText {
+ text: "Finished reasoning.".into(),
+ }],
+ Some("ENC_NEW"),
+ Some("completed".into()),
+ )),
+ },
+ ResponsesStreamEvent::Completed {
+ response: ResponseSummary::default(),
+ },
+ ];
+
+ let mapped = map_response_events(events);
+ let details = mapped
+ .iter()
+ .filter_map(|event| match event {
+ LanguageModelCompletionEvent::ReasoningDetails(details) => Some(details),
+ _ => None,
+ })
+ .next_back()
+ .expect("reasoning details");
+
+ assert_eq!(
+ details,
+ &json!({
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [
+ {
+ "type": "summary_text",
+ "text": "Finished reasoning."
+ }
+ ],
+ "encrypted_content": "ENC_NEW",
+ "status": "completed"
+ }
+ ]
+ })
+ );
+ }
+
+ #[test]
+ fn responses_stream_reemits_reasoning_details_after_phase_less_message_start() {
+ let events = vec![
+ ResponsesStreamEvent::OutputItemDone {
+ output_index: 0,
+ sequence_number: None,
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_123",
+ Vec::new(),
+ Some("ENC"),
+ Some("completed".into()),
+ )),
+ },
+ ResponsesStreamEvent::OutputItemAdded {
+ output_index: 1,
+ sequence_number: None,
+ item: ResponseOutputItem::Message(ResponseOutputMessage {
+ id: Some("msg_123".into()),
+ role: Some("assistant".into()),
+ status: Some("in_progress".into()),
+ content: vec![],
+ phase: None,
+ }),
+ },
+ ResponsesStreamEvent::OutputTextDelta {
+ item_id: "msg_123".into(),
+ output_index: 1,
+ content_index: Some(0),
+ delta: "Hello".into(),
+ },
+ ResponsesStreamEvent::Completed {
+ response: ResponseSummary::default(),
+ },
+ ];
+
+ let mapped = map_response_events(events);
+ let start_message_index = mapped
+ .iter()
+ .position(|event| matches!(event, LanguageModelCompletionEvent::StartMessage { .. }))
+ .expect("start message");
+ let details = mapped
+ .iter()
+ .skip(start_message_index + 1)
+ .find_map(|event| match event {
+ LanguageModelCompletionEvent::ReasoningDetails(details) => Some(details),
+ _ => None,
+ })
+ .expect("reasoning details after start message");
+
+ assert_eq!(
+ details,
+ &json!({
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ }
+ ]
+ })
+ );
+ }
+
+ #[test]
+ fn responses_stream_preserves_assistant_phase_with_reasoning_details() {
+ let events = vec![
+ ResponsesStreamEvent::OutputItemAdded {
+ output_index: 0,
+ sequence_number: None,
+ item: ResponseOutputItem::Message(ResponseOutputMessage {
+ id: Some("msg_123".into()),
+ role: Some("assistant".into()),
+ status: Some("in_progress".into()),
+ content: vec![],
+ phase: Some("commentary".into()),
+ }),
+ },
+ ResponsesStreamEvent::OutputTextDelta {
+ item_id: "msg_123".into(),
+ output_index: 0,
+ content_index: Some(0),
+ delta: "I will inspect the workspace.".into(),
+ },
+ ResponsesStreamEvent::OutputItemDone {
+ output_index: 1,
+ sequence_number: None,
+ item: ResponseOutputItem::Reasoning(response_reasoning_item(
+ "rs_123",
+ Vec::new(),
+ Some("ENC"),
+ Some("completed".into()),
+ )),
+ },
+ ResponsesStreamEvent::Completed {
+ response: ResponseSummary::default(),
+ },
+ ];
+
+ let mapped = map_response_events(events);
+ let details = mapped
+ .iter()
+ .filter_map(|event| match event {
+ LanguageModelCompletionEvent::ReasoningDetails(details) => Some(details),
+ _ => None,
+ })
+ .next_back()
+ .expect("reasoning details");
+
+ assert_eq!(
+ details,
+ &json!({
+ "phase": "commentary",
+ "reasoning_items": [
+ {
+ "id": "rs_123",
+ "summary": [],
+ "encrypted_content": "ENC",
+ "status": "completed"
+ }
+ ]
+ })
+ );
+ }
+
#[test]
fn into_open_ai_interleaved_reasoning() {
let tool_use_id = LanguageModelToolUseId::from("call-1");
diff --git a/crates/open_ai/src/open_ai.rs b/crates/open_ai/src/open_ai.rs
index 009cdd897e2..b756e8a6122 100644
--- a/crates/open_ai/src/open_ai.rs
+++ b/crates/open_ai/src/open_ai.rs
@@ -58,26 +58,14 @@ impl From<Role> for String {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
pub enum Model {
- #[serde(rename = "gpt-3.5-turbo")]
- ThreePointFiveTurbo,
#[serde(rename = "gpt-4")]
Four,
- #[serde(rename = "gpt-4-turbo")]
- FourTurbo,
#[serde(rename = "gpt-4o-mini")]
FourOmniMini,
- #[serde(rename = "gpt-4.1-nano")]
- FourPointOneNano,
- #[serde(rename = "o1")]
- O1,
- #[serde(rename = "o3-mini")]
- O3Mini,
#[serde(rename = "o3")]
O3,
#[serde(rename = "gpt-5")]
Five,
- #[serde(rename = "gpt-5-codex")]
- FiveCodex,
#[serde(rename = "gpt-5-mini")]
#[default]
FiveMini,
@@ -87,10 +75,12 @@ pub enum Model {
FivePointOne,
#[serde(rename = "gpt-5.2")]
FivePointTwo,
- #[serde(rename = "gpt-5.2-codex")]
- FivePointTwoCodex,
#[serde(rename = "gpt-5.3-codex")]
FivePointThreeCodex,
+ #[serde(rename = "gpt-5.4-nano")]
+ FivePointFourNano,
+ #[serde(rename = "gpt-5.4-mini")]
+ FivePointFourMini,
#[serde(rename = "gpt-5.4")]
FivePointFour,
#[serde(rename = "gpt-5.4-pro")]
@@ -130,22 +120,17 @@ impl Model {
pub fn from_id(id: &str) -> Result<Self> {
match id {
- "gpt-3.5-turbo" => Ok(Self::ThreePointFiveTurbo),
"gpt-4" => Ok(Self::Four),
- "gpt-4-turbo-preview" => Ok(Self::FourTurbo),
"gpt-4o-mini" => Ok(Self::FourOmniMini),
- "gpt-4.1-nano" => Ok(Self::FourPointOneNano),
- "o1" => Ok(Self::O1),
- "o3-mini" => Ok(Self::O3Mini),
"o3" => Ok(Self::O3),
"gpt-5" => Ok(Self::Five),
- "gpt-5-codex" => Ok(Self::FiveCodex),
"gpt-5-mini" => Ok(Self::FiveMini),
"gpt-5-nano" => Ok(Self::FiveNano),
"gpt-5.1" => Ok(Self::FivePointOne),
"gpt-5.2" => Ok(Self::FivePointTwo),
- "gpt-5.2-codex" => Ok(Self::FivePointTwoCodex),
"gpt-5.3-codex" => Ok(Self::FivePointThreeCodex),
+ "gpt-5.4-nano" => Ok(Self::FivePointFourNano),
+ "gpt-5.4-mini" => Ok(Self::FivePointFourMini),
"gpt-5.4" => Ok(Self::FivePointFour),
"gpt-5.4-pro" => Ok(Self::FivePointFourPro),
"gpt-5.5" => Ok(Self::FivePointFive),
@@ -156,22 +141,17 @@ impl Model {
pub fn id(&self) -> &str {
match self {
- Self::ThreePointFiveTurbo => "gpt-3.5-turbo",
Self::Four => "gpt-4",
- Self::FourTurbo => "gpt-4-turbo",
Self::FourOmniMini => "gpt-4o-mini",
- Self::FourPointOneNano => "gpt-4.1-nano",
- Self::O1 => "o1",
- Self::O3Mini => "o3-mini",
Self::O3 => "o3",
Self::Five => "gpt-5",
- Self::FiveCodex => "gpt-5-codex",
Self::FiveMini => "gpt-5-mini",
Self::FiveNano => "gpt-5-nano",
Self::FivePointOne => "gpt-5.1",
Self::FivePointTwo => "gpt-5.2",
- Self::FivePointTwoCodex => "gpt-5.2-codex",
Self::FivePointThreeCodex => "gpt-5.3-codex",
+ Self::FivePointFourNano => "gpt-5.4-nano",
+ Self::FivePointFourMini => "gpt-5.4-mini",
Self::FivePointFour => "gpt-5.4",
Self::FivePointFourPro => "gpt-5.4-pro",
Self::FivePointFive => "gpt-5.5",
@@ -182,22 +162,17 @@ impl Model {
pub fn display_name(&self) -> &str {
match self {
- Self::ThreePointFiveTurbo => "gpt-3.5-turbo",
Self::Four => "gpt-4",
- Self::FourTurbo => "gpt-4-turbo",
Self::FourOmniMini => "gpt-4o-mini",
- Self::FourPointOneNano => "gpt-4.1-nano",
- Self::O1 => "o1",
- Self::O3Mini => "o3-mini",
Self::O3 => "o3",
Self::Five => "gpt-5",
- Self::FiveCodex => "gpt-5-codex",
Self::FiveMini => "gpt-5-mini",
Self::FiveNano => "gpt-5-nano",
Self::FivePointOne => "gpt-5.1",
Self::FivePointTwo => "gpt-5.2",
- Self::FivePointTwoCodex => "gpt-5.2-codex",
Self::FivePointThreeCodex => "gpt-5.3-codex",
+ Self::FivePointFourNano => "gpt-5.4-nano",
+ Self::FivePointFourMini => "gpt-5.4-mini",
Self::FivePointFour => "gpt-5.4",
Self::FivePointFourPro => "gpt-5.4-pro",
Self::FivePointFive => "gpt-5.5",
@@ -208,22 +183,17 @@ impl Model {
pub fn max_token_count(&self) -> u64 {
match self {
- Self::ThreePointFiveTurbo => 16_385,
Self::Four => 8_192,
- Self::FourTurbo => 128_000,
Self::FourOmniMini => 128_000,
- Self::FourPointOneNano => 1_047_576,
- Self::O1 => 200_000,
- Self::O3Mini => 200_000,
Self::O3 => 200_000,
Self::Five => 272_000,
- Self::FiveCodex => 272_000,
Self::FiveMini => 400_000,
Self::FiveNano => 400_000,
Self::FivePointOne => 400_000,
Self::FivePointTwo => 400_000,
- Self::FivePointTwoCodex => 400_000,
Self::FivePointThreeCodex => 400_000,
+ Self::FivePointFourNano => 400_000,
+ Self::FivePointFourMini => 400_000,
Self::FivePointFour => 1_050_000,
Self::FivePointFourPro => 1_050_000,
Self::FivePointFive => 1_050_000,
@@ -237,22 +207,17 @@ impl Model {
Self::Custom {
max_output_tokens, ..
} => *max_output_tokens,
- Self::ThreePointFiveTurbo => Some(4_096),
Self::Four => Some(8_192),
- Self::FourTurbo => Some(4_096),
Self::FourOmniMini => Some(16_384),
- Self::FourPointOneNano => Some(32_768),
- Self::O1 => Some(100_000),
- Self::O3Mini => Some(100_000),
Self::O3 => Some(100_000),
Self::Five => Some(128_000),
- Self::FiveCodex => Some(128_000),
Self::FiveMini => Some(128_000),
Self::FiveNano => Some(128_000),
Self::FivePointOne => Some(128_000),
Self::FivePointTwo => Some(128_000),
- Self::FivePointTwoCodex => Some(128_000),
Self::FivePointThreeCodex => Some(128_000),
+ Self::FivePointFourNano => Some(128_000),
+ Self::FivePointFourMini => Some(128_000),
Self::FivePointFour => Some(128_000),
Self::FivePointFourPro => Some(128_000),
Self::FivePointFive => Some(128_000),
@@ -265,18 +230,16 @@ impl Model {
Self::Custom {
reasoning_effort, ..
} => reasoning_effort.to_owned(),
- Self::O1
- | Self::O3
- | Self::O3Mini
+ Self::FivePointOne
+ | Self::FivePointTwo
+ | Self::FivePointFour
+ | Self::FivePointFourMini
+ | Self::FivePointFourNano => Some(ReasoningEffort::None),
+ Self::O3
| Self::Five
- | Self::FiveCodex
| Self::FiveMini
| Self::FiveNano
- | Self::FivePointOne
- | Self::FivePointTwo
- | Self::FivePointTwoCodex
| Self::FivePointThreeCodex
- | Self::FivePointFour
| Self::FivePointFourPro
| Self::FivePointFive
| Self::FivePointFivePro => Some(ReasoningEffort::Medium),
@@ -290,13 +253,20 @@ impl Model {
reasoning_effort: Some(effort),
..
} => match effort {
+ ReasoningEffort::None => &[ReasoningEffort::None],
ReasoningEffort::Minimal => &[ReasoningEffort::Minimal],
ReasoningEffort::Low => &[ReasoningEffort::Low],
ReasoningEffort::Medium => &[ReasoningEffort::Medium],
ReasoningEffort::High => &[ReasoningEffort::High],
ReasoningEffort::XHigh => &[ReasoningEffort::XHigh],
},
- Self::O1 | Self::O3 | Self::O3Mini | Self::FivePointOne => &[
+ Self::O3 => &[
+ ReasoningEffort::Low,
+ ReasoningEffort::Medium,
+ ReasoningEffort::High,
+ ],
+ Self::FivePointOne => &[
+ ReasoningEffort::None,
ReasoningEffort::Low,
ReasoningEffort::Medium,
ReasoningEffort::High,
@@ -307,10 +277,13 @@ impl Model {
ReasoningEffort::Medium,
ReasoningEffort::High,
],
- Self::FiveCodex
- | Self::FivePointTwoCodex
- | Self::FivePointThreeCodex
- | Self::FivePointFourPro => &[
+ Self::FivePointFourPro | Self::FivePointFivePro => &[
+ ReasoningEffort::Medium,
+ ReasoningEffort::High,
+ ReasoningEffort::XHigh,
+ ],
+ Self::FivePointThreeCodex => &[
+ ReasoningEffort::Low,
ReasoningEffort::Medium,
ReasoningEffort::High,
ReasoningEffort::XHigh,
@@ -318,7 +291,9 @@ impl Model {
Self::FivePointTwo
| Self::FivePointFour
| Self::FivePointFive
- | Self::FivePointFivePro => &[
+ | Self::FivePointFourMini
+ | Self::FivePointFourNano => &[
+ ReasoningEffort::None,
ReasoningEffort::Low,
ReasoningEffort::Medium,
ReasoningEffort::High,
@@ -343,24 +318,21 @@ impl Model {
/// If the model does not support the parameter, do not pass it up, or the API will return an error.
pub fn supports_parallel_tool_calls(&self) -> bool {
match self {
- Self::ThreePointFiveTurbo
- | Self::Four
- | Self::FourTurbo
+ Self::Four
| Self::FourOmniMini
- | Self::FourPointOneNano
| Self::Five
- | Self::FiveCodex
| Self::FiveMini
| Self::FivePointOne
| Self::FivePointTwo
- | Self::FivePointTwoCodex
| Self::FivePointThreeCodex
| Self::FivePointFour
+ | Self::FivePointFourMini
+ | Self::FivePointFourNano
| Self::FivePointFourPro
| Self::FivePointFive
| Self::FivePointFivePro
| Self::FiveNano => true,
- Self::O1 | Self::O3 | Self::O3Mini | Model::Custom { .. } => false,
+ Self::O3 | Model::Custom { .. } => false,
}
}
@@ -372,6 +344,81 @@ impl Model {
}
}
+#[cfg(test)]
+mod tests {
+ use super::{Model, ReasoningEffort};
+
+ #[test]
+ fn gpt_5_1_uses_none_reasoning_by_default() {
+ let expected_efforts = [
+ ReasoningEffort::None,
+ ReasoningEffort::Low,
+ ReasoningEffort::Medium,
+ ReasoningEffort::High,
+ ];
+
+ assert_eq!(
+ Model::FivePointOne.reasoning_effort(),
+ Some(ReasoningEffort::None)
+ );
+ assert_eq!(
+ Model::FivePointOne.supported_reasoning_efforts(),
+ expected_efforts.as_slice()
+ );
+ }
+
+ #[test]
+ fn newer_frontier_models_support_none_reasoning() {
+ let expected_efforts = [
+ ReasoningEffort::None,
+ ReasoningEffort::Low,
+ ReasoningEffort::Medium,
+ ReasoningEffort::High,
+ ReasoningEffort::XHigh,
+ ];
+
+ assert_eq!(
+ Model::FivePointTwo.reasoning_effort(),
+ Some(ReasoningEffort::None)
+ );
+ assert_eq!(
+ Model::FivePointTwo.supported_reasoning_efforts(),
+ expected_efforts.as_slice()
+ );
+ assert_eq!(
+ Model::FivePointFour.reasoning_effort(),
+ Some(ReasoningEffort::None)
+ );
+ assert_eq!(
+ Model::FivePointFour.supported_reasoning_efforts(),
+ expected_efforts.as_slice()
+ );
+ assert_eq!(
+ Model::FivePointFive.reasoning_effort(),
+ Some(ReasoningEffort::Medium)
+ );
+ assert_eq!(
+ Model::FivePointFive.supported_reasoning_efforts(),
+ expected_efforts.as_slice()
+ );
+ }
+
+ #[test]
+ fn newer_codex_models_support_low_reasoning_effort() {
+ let expected_efforts = [
+ ReasoningEffort::Low,
+ ReasoningEffort::Medium,
+ ReasoningEffort::High,
+ ReasoningEffort::XHigh,
+ ];
+
+ assert_eq!(
+ Model::FivePointThreeCodex.supported_reasoning_efforts(),
+ expected_efforts.as_slice()
+ );
+ }
+}
+
#[derive(Debug, Serialize, Deserialize)]
pub struct StreamOptions {
pub include_usage: bool,
diff --git a/crates/open_ai/src/responses.rs b/crates/open_ai/src/responses.rs
index 34dbd46c372..37335454c32 100644
--- a/crates/open_ai/src/responses.rs
+++ b/crates/open_ai/src/responses.rs
@@ -11,6 +11,9 @@ pub struct Request {
pub model: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub input: Vec<ResponseInputItem>,
+ pub store: bool,
+ #[serde(skip_serializing_if = "Vec::is_empty")]
+ pub include: Vec<ResponseIncludable>,
#[serde(default)]
pub stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -31,18 +34,28 @@ pub struct Request {
pub reasoning: Option<ReasoningConfig>,
}
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+pub enum ResponseIncludable {
+ #[serde(rename = "reasoning.encrypted_content")]
+ ReasoningEncryptedContent,
+}
+
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseInputItem {
Message(ResponseMessageItem),
FunctionCall(ResponseFunctionCallItem),
FunctionCallOutput(ResponseFunctionCallOutputItem),
+ Reasoning(ResponseReasoningInputItem),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ResponseMessageItem {
pub role: Role,
pub content: Vec<ResponseInputContent>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub phase: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@@ -58,6 +71,26 @@ pub struct ResponseFunctionCallOutputItem {
pub output: ResponseFunctionCallOutputContent,
}
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+pub struct ResponseReasoningInputItem {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub id: Option<String>,
+ #[serde(default)]
+ pub summary: Vec<ResponseReasoningSummaryPart>,
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub content: Vec<Value>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub encrypted_content: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub status: Option<String>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum ResponseReasoningSummaryPart {
+ SummaryText { text: String },
+}
+
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ResponseFunctionCallOutputContent {
@@ -111,9 +144,13 @@ pub enum ToolDefinition {
},
}
-#[derive(Deserialize, Debug)]
-pub struct Error {
+#[derive(Deserialize, Debug, Clone)]
+pub struct ResponseError {
+ #[serde(default)]
+ pub code: Option<String>,
pub message: String,
+ #[serde(default)]
+ pub param: Option<Value>,
}
#[derive(Deserialize, Debug)]
@@ -167,6 +204,24 @@ pub enum StreamEvent {
content_index: Option<usize>,
text: String,
},
+ #[serde(rename = "response.refusal.delta")]
+ RefusalDelta {
+ item_id: String,
+ output_index: usize,
+ content_index: usize,
+ delta: String,
+ #[serde(default)]
+ sequence_number: Option<u64>,
+ },
+ #[serde(rename = "response.refusal.done")]
+ RefusalDone {
+ item_id: String,
+ output_index: usize,
+ content_index: usize,
+ refusal: String,
+ #[serde(default)]
+ sequence_number: Option<u64>,
+ },
#[serde(rename = "response.reasoning_summary_part.added")]
ReasoningSummaryPartAdded {
item_id: String,
@@ -214,9 +269,12 @@ pub enum StreamEvent {
#[serde(rename = "response.failed")]
Failed { response: ResponseSummary },
#[serde(rename = "response.error")]
- Error { error: Error },
+ Error { error: ResponseError },
#[serde(rename = "error")]
- GenericError { error: Error },
+ GenericError {
+ #[serde(flatten)]
+ error: ResponseError,
+ },
#[serde(other)]
Unknown,
}
@@ -228,7 +286,9 @@ pub struct ResponseSummary {
#[serde(default)]
pub status: Option<String>,
#[serde(default)]
- pub status_details: Option<ResponseStatusDetails>,
+ pub incomplete_details: Option<ResponseIncompleteDetails>,
+ #[serde(default)]
+ pub error: Option<ResponseError>,
#[serde(default)]
pub usage: Option<ResponseUsage>,
#[serde(default)]
@@ -236,13 +296,9 @@ pub struct ResponseSummary {
}
#[derive(Deserialize, Debug, Default, Clone)]
-pub struct ResponseStatusDetails {
+pub struct ResponseIncompleteDetails {
#[serde(default)]
pub reason: Option<String>,
- #[serde(default)]
- pub r#type: Option<String>,
- #[serde(default)]
- pub error: Option<Value>,
}
#[derive(Deserialize, Debug, Default, Clone)]
@@ -250,11 +306,27 @@ pub struct ResponseUsage {
#[serde(default)]
pub input_tokens: Option<u64>,
#[serde(default)]
+ pub input_tokens_details: ResponseInputTokensDetails,
+ #[serde(default)]
pub output_tokens: Option<u64>,
#[serde(default)]
+ pub output_tokens_details: ResponseOutputTokensDetails,
+ #[serde(default)]
pub total_tokens: Option<u64>,
}
+#[derive(Deserialize, Debug, Default, Clone)]
+pub struct ResponseInputTokensDetails {
+ #[serde(default)]
+ pub cached_tokens: u64,
+}
+
+#[derive(Deserialize, Debug, Default, Clone)]
+pub struct ResponseOutputTokensDetails {
+ #[serde(default)]
+ pub reasoning_tokens: u64,
+}
+
#[derive(Deserialize, Debug, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseOutputItem {
@@ -271,6 +343,12 @@ pub struct ResponseReasoningItem {
pub id: Option<String>,
#[serde(default)]
pub summary: Vec<ReasoningSummaryPart>,
+ #[serde(default)]
+ pub content: Vec<Value>,
+ #[serde(default)]
+ pub encrypted_content: Option<String>,
+ #[serde(default)]
+ pub status: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
@@ -293,6 +371,8 @@ pub struct ResponseOutputMessage {
pub role: Option<String>,
#[serde(default)]
pub status: Option<String>,
+ #[serde(default)]
+ pub phase: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
@@ -441,8 +521,17 @@ pub async fn stream_response(
});
}
- all_events.push(StreamEvent::Completed {
- response: response_summary,
+ let status = response_summary.status.clone();
+ all_events.push(match status.as_deref() {
+ Some("incomplete") => StreamEvent::Incomplete {
+ response: response_summary,
+ },
+ Some("failed") => StreamEvent::Failed {
+ response: response_summary,
+ },
+ _ => StreamEvent::Completed {
+ response: response_summary,
+ },
});
Ok(futures::stream::iter(all_events.into_iter().map(Ok)).boxed())