Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
open_ai: Handle Responses reasoning text events (#65938)
Bennet Bo Fenner566f893ee3b5
diff --git a/crates/open_ai/src/completion.rs b/crates/open_ai/src/completion.rsindex def7cde5aa8..69bb669ecba 100644--- a/crates/open_ai/src/completion.rs+++ b/crates/open_ai/src/completion.rs@@ -881,7 +881,8 @@ impl OpenAiResponseEventMapper {events}}- ResponsesStreamEvent::ReasoningDelta { delta, .. } => {+ ResponsesStreamEvent::ReasoningDelta { delta, .. }+ | ResponsesStreamEvent::ReasoningTextDelta { delta, .. } => {if delta.is_empty() {Vec::new()} else {@@ -1085,6 +1086,7 @@ impl OpenAiResponseEventMapper {| ResponsesStreamEvent::ContentPartAdded { .. }| ResponsesStreamEvent::ContentPartDone { .. }| ResponsesStreamEvent::ReasoningSummaryTextDone { .. }+ | ResponsesStreamEvent::ReasoningTextDone { .. }| ResponsesStreamEvent::ReasoningSummaryPartDone { .. }| ResponsesStreamEvent::ReasoningDone { .. }| ResponsesStreamEvent::Created { .. }@@ -1510,7 +1512,12 @@ fn response_reasoning_input_item_from_output(ResponseReasoningInputItem {id: reasoning.id.clone(),summary,- content: reasoning.content.clone(),+ content: reasoning+ .content+ .iter()+ .filter(|part| !matches!(part, crate::responses::ReasoningContentPart::Other))+ .cloned()+ .collect(),encrypted_content,status: reasoning.status.clone(),}@@ -3928,10 +3935,9 @@ mod tests {Some("ENC"),Some("completed".into()),);- reasoning_item.content = vec![json!({- "type": "reasoning_text",- "text": "Internal reasoning text."- })];+ reasoning_item.content = vec![crate::responses::ReasoningContentPart::ReasoningText {+ text: "Internal reasoning text.".into(),+ }];let events = vec![ResponsesStreamEvent::OutputItemDone {diff --git a/crates/open_ai/src/responses.rs b/crates/open_ai/src/responses.rsindex 374a9490e5f..4037b40b72f 100644--- a/crates/open_ai/src/responses.rs+++ b/crates/open_ai/src/responses.rs@@ -328,7 +328,7 @@ pub struct ResponseReasoningInputItem {#[serde(default)]pub summary: Vec<ResponseReasoningSummaryPart>,#[serde(default, skip_serializing_if = "Vec::is_empty")]- pub content: Vec<Value>,+ pub content: Vec<ReasoningContentPart>,#[serde(default, skip_serializing_if = "Option::is_none")]pub encrypted_content: Option<String>,#[serde(default, skip_serializing_if = "Option::is_none")]@@ -557,6 +557,20 @@ pub enum StreamEvent {output_index: usize,text: String,},+ #[serde(rename = "response.reasoning_text.delta")]+ ReasoningTextDelta {+ item_id: String,+ output_index: usize,+ content_index: usize,+ delta: String,+ },+ #[serde(rename = "response.reasoning_text.done")]+ ReasoningTextDone {+ item_id: String,+ output_index: usize,+ content_index: usize,+ text: String,+ },#[serde(rename = "response.reasoning_summary_part.done")]ReasoningSummaryPartDone {item_id: String,@@ -699,13 +713,23 @@ pub struct ResponseReasoningItem {#[serde(default)]pub summary: Vec<ReasoningSummaryPart>,#[serde(default)]- pub content: Vec<Value>,+ pub content: Vec<ReasoningContentPart>,#[serde(default)]pub encrypted_content: Option<String>,#[serde(default)]pub status: Option<String>,}+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum ReasoningContentPart {+ ReasoningText {+ text: String,+ },+ #[serde(other)]+ Other,+}+#[derive(Serialize, Deserialize, Debug, Clone)]#[serde(tag = "type", rename_all = "snake_case")]pub enum ReasoningSummaryPart {@@ -993,6 +1017,18 @@ pub async fn stream_response();}}+ for (content_index, part) in+ reasoning.content.iter().enumerate()+ {+ if let ReasoningContentPart::ReasoningText { text } = part {+ all_events.push(StreamEvent::ReasoningTextDelta {+ item_id: item_id.clone(),+ output_index,+ content_index,+ delta: text.to_owned(),+ });+ }+ }}}// No synthesized deltas; the `OutputItemDone`@@ -1064,6 +1100,117 @@ mod tests {use serde_json::json;use std::sync::{Arc, Mutex};+ #[test]+ fn reasoning_content_accepts_unknown_parts() {+ let content = json!([+ {"type": "reasoning_text", "text": "Thinking"},+ {"type": "future_reasoning", "payload": {"opaque": true}}+ ]);+ let item: ResponseReasoningItem = serde_json::from_value(json!({+ "content": content+ }))+ .expect("reasoning item");+ assert_eq!(+ item.content,+ vec![+ ReasoningContentPart::ReasoningText {+ text: "Thinking".into(),+ },+ ReasoningContentPart::Other,+ ]+ );+ }++ #[test]+ fn reasoning_text_maps_to_thinking_in_streaming_and_json_responses() {+ use crate::completion::OpenAiResponseEventMapper;+ use language_model_core::LanguageModelCompletionEvent;++ for streaming in [true, false] {+ let reasoning = json!({+ "type": "reasoning",+ "id": "rs_123",+ "summary": [],+ "content": [{"type": "reasoning_text", "text": "Let me think."}],+ "status": "completed"+ });+ let response = json!({+ "id": "resp_123",+ "status": "completed",+ "output": [reasoning]+ });+ let body = if streaming {+ let events = [+ json!({+ "type": "response.reasoning_text.delta",+ "item_id": "rs_123", "output_index": 0, "content_index": 0,+ "delta": ""+ }),+ json!({+ "type": "response.reasoning_text.delta",+ "item_id": "rs_123", "output_index": 0, "content_index": 0,+ "delta": "Let me "+ }),+ json!({+ "type": "response.reasoning_text.delta",+ "item_id": "rs_123", "output_index": 0, "content_index": 0,+ "delta": "think."+ }),+ json!({+ "type": "response.reasoning_text.done",+ "item_id": "rs_123", "output_index": 0, "content_index": 0,+ "text": "Let me think."+ }),+ json!({"type": "response.output_item.done", "output_index": 0, "item": reasoning}),+ json!({"type": "response.completed", "response": response}),+ ];+ events+ .iter()+ .map(|event| format!("data: {event}\n\n"))+ .collect::<String>()+ } else {+ response.to_string()+ };+ let client = FakeHttpClient::create(move |_| {+ let body = body.clone();+ async move {+ Ok(http_client::Response::builder()+ .status(200)+ .body(AsyncBody::from(body))?)+ }+ });+ let mut request = response_test_request();+ request.stream = streaming;+ let events = block_on(async {+ stream_response(+ client.as_ref(),+ "Custom",+ "https://example.com/v1",+ "test-key",+ request,+ &CustomHeaders::default(),+ )+ .await+ .expect("response stream")+ .collect::<Vec<_>>()+ .await+ });+ let mut mapper = OpenAiResponseEventMapper::new(OPEN_AI_PROVIDER_ID);+ let mut thinking = Vec::new();+ for event in events {+ for event in mapper.map_event(event.expect("decoded event")) {+ if let LanguageModelCompletionEvent::Thinking { text, .. } =+ event.expect("mapped event")+ {+ thinking.push(text);+ }+ }+ }+ assert_eq!(thinking.concat(), "Let me think.");+ assert!(thinking.iter().all(|text| !text.is_empty()));+ }+ }+#[test]fn count_input_tokens_preserves_structured_input_without_generation_fields() {let input = json!([{