Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix structured content in compatible chat completion streams (#65915)
Bennet Bo Fenner3762891a55e2
diff --git a/crates/language_model_core/src/chat_completion.rs b/crates/language_model_core/src/chat_completion.rsindex c2570f75abc..e3f2049cb17 100644--- a/crates/language_model_core/src/chat_completion.rs+++ b/crates/language_model_core/src/chat_completion.rs@@ -111,7 +111,7 @@ where#[derive(Serialize, Deserialize, Debug, Default, Eq, PartialEq)]pub struct ResponseMessageDelta {- pub content: Option<String>,+ pub content: Option<ResponseContent>,/// Reasoning text as sent by OpenRouter and compatible providers.#[serde(default, skip_serializing_if = "Option::is_none")]pub reasoning: Option<String>,@@ -126,6 +126,47 @@ pub struct ResponseMessageDelta {pub reasoning_details: Option<Value>,}+/// Incremental content, which may change representation between chunks.+#[derive(Clone, Serialize, Debug, Eq, PartialEq)]+#[serde(untagged)]+pub enum ResponseContent {+ Text(String),+ Parts(Vec<ResponseContentPart>),+}++impl<'de> Deserialize<'de> for ResponseContent {+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ let value = Value::deserialize(deserializer)?;+ if value.is_array() {+ Vec::<ResponseContentPart>::deserialize(value)+ .map(Self::Parts)+ .map_err(serde::de::Error::custom)+ } else {+ String::deserialize(value)+ .map(Self::Text)+ .map_err(serde::de::Error::custom)+ }+ }+}++/// Text and reasoning blocks carried by compatible streaming APIs.+#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum ResponseContentPart {+ Text { text: String },+ Thinking { thinking: Vec<ResponseThinkingPart> },+}++/// A text fragment within a structured reasoning block.+#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum ResponseThinkingPart {+ Text { text: String },+}+#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]pub struct ToolCallChunk {/// `None` when the provider omits the index, sends `null`, or sends a@@ -478,9 +519,31 @@ impl ChatCompletionEventMapper {if let Some(reasoning_content) = delta.reasoning_content.clone() {push_thinking_event(reasoning_content, &mut events);}- if let Some(content) = delta.content.clone() {- if !content.is_empty() {- events.push(Ok(LanguageModelCompletionEvent::Text(content)));+ if let Some(content) = delta.content.as_ref() {+ match content {+ ResponseContent::Text(text) => {+ if !text.is_empty() {+ events.push(Ok(LanguageModelCompletionEvent::Text(text.clone())));+ }+ }+ ResponseContent::Parts(parts) => {+ for part in parts {+ match part {+ ResponseContentPart::Text { text } => {+ if !text.is_empty() {+ events.push(Ok(LanguageModelCompletionEvent::Text(+ text.clone(),+ )));+ }+ }+ ResponseContentPart::Thinking { thinking } => {+ for ResponseThinkingPart::Text { text } in thinking {+ push_thinking_event(text.clone(), &mut events);+ }+ }+ }+ }+ }}}@@ -599,6 +662,54 @@ mod tests {use pretty_assertions::assert_eq;use serde_json::json;+ #[test]+ fn maps_mistral_mixed_content_stream() -> anyhow::Result<()> {+ let chunks = [+ json!({"choices":[{"delta":{"content":""}}]}),+ json!({"choices":[{"delta":{"content":[{"type":"thinking","thinking":[{"type":"text","text":"Hello"}]}]}}]}),+ json!({"choices":[{"delta":{"content":[{"type":"thinking","thinking":[{"type":"text","text":" there friend.\""}],"closed":true}]}}]}),+ json!({"choices":[{"delta":{"content":[{"type":"thinking","thinking":[]},{"type":"text","text":"Hello there friend"}]}}]}),+ json!({"choices":[{"delta":{"content":"."},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":347,"total_tokens":356}}),+ ];+ let mut mapper = ChatCompletionEventMapper::new();+ let mut events = Vec::new();+ for chunk in chunks {+ let ResponseStreamResult::Ok(chunk) =+ serde_json::from_value::<ResponseStreamResult>(chunk)?+ else {+ anyhow::bail!("expected a completion chunk");+ };+ events.extend(+ mapper+ .map_event(chunk)+ .into_iter()+ .collect::<Result<Vec<_>, _>>()?,+ );+ }+ assert_eq!(+ events,+ vec![+ LanguageModelCompletionEvent::Thinking {+ text: "Hello".into(),+ signature: None,+ },+ LanguageModelCompletionEvent::Thinking {+ text: " there friend.\"".into(),+ signature: None,+ },+ LanguageModelCompletionEvent::Text("Hello there friend".into()),+ LanguageModelCompletionEvent::UsageUpdate(TokenUsage {+ input_tokens: 9,+ output_tokens: 347,+ ..Default::default()+ }),+ LanguageModelCompletionEvent::Text(".".into()),+ LanguageModelCompletionEvent::Stop(StopReason::EndTurn),+ ]+ );+ Ok(())+ }+fn parse(chunk: &str) -> ResponseStreamResult {serde_json::from_str::<ResponseStreamResult>(chunk).unwrap()}@@ -737,8 +848,8 @@ mod tests {fn null_error_field_is_not_an_error() {let event = expect_event(r#"{"error":null,"choices":[{"delta":{"content":"hi"}}]}"#);assert_eq!(- event.choices[0].delta.as_ref().unwrap().content.as_deref(),- Some("hi")+ event.choices[0].delta.as_ref().unwrap().content.as_ref(),+ Some(&ResponseContent::Text("hi".into())));}@@ -854,7 +965,7 @@ mod tests {choices: vec![ChoiceDelta {index: 0,delta: Some(ResponseMessageDelta {- content: Some("Hello!".to_string()),+ content: Some(ResponseContent::Text("Hello!".to_string())),..Default::default()}),finish_reason: Some("stop".to_string()),