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: Support reasoning content (#43662)

Aero7bd3075d532c
diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs
index fde221f86f2..f421da7a975 100644
--- a/crates/language_models/src/provider/open_ai.rs
+++ b/crates/language_models/src/provider/open_ai.rs
@@ -426,7 +426,14 @@ pub fn into_open_ai(
for content in message.content {
match content {
MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
- if !text.trim().is_empty() {
+ let should_add = if message.role == Role::User {
+ // Including whitespace-only user messages can cause error with OpenAI compatible APIs
+ // See https://github.com/zed-industries/zed/issues/40097
+ !text.trim().is_empty()
+ } else {
+ !text.is_empty()
+ };
+ if should_add {
add_message_content_part(
open_ai::MessagePart::Text { text },
message.role,
@@ -792,8 +799,18 @@ impl OpenAiEventMapper {
};
if let Some(delta) = choice.delta.as_ref() {
+ if let Some(reasoning_content) = delta.reasoning_content.clone() {
+ if !reasoning_content.is_empty() {
+ events.push(Ok(LanguageModelCompletionEvent::Thinking {
+ text: reasoning_content,
+ signature: None,
+ }));
+ }
+ }
if let Some(content) = delta.content.clone() {
- events.push(Ok(LanguageModelCompletionEvent::Text(content)));
+ if !content.is_empty() {
+ events.push(Ok(LanguageModelCompletionEvent::Text(content)));
+ }
}
if let Some(tool_calls) = delta.tool_calls.as_ref() {
diff --git a/crates/open_ai/src/open_ai.rs b/crates/open_ai/src/open_ai.rs
index 0f88aaddc60..e05865e079c 100644
--- a/crates/open_ai/src/open_ai.rs
+++ b/crates/open_ai/src/open_ai.rs
@@ -463,6 +463,8 @@ pub struct ResponseMessageDelta {
pub content: Option<String>,
#[serde(default, skip_serializing_if = "is_none_or_empty")]
pub tool_calls: Option<Vec<ToolCallChunk>>,
+ #[serde(default, skip_serializing_if = "is_none_or_empty")]
+ pub reasoning_content: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]