From 85740ddaa438037e1c3a7b87e6787214354f2d54 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Thu, 27 Mar 2025 18:20:10 -0400 Subject: [PATCH] Make serialization backwards-compatible for collab server (#27626) Sets up the collab server to accept the format of system message that we'll introduce later for [prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching). Release Notes: - N/A --- crates/anthropic/src/anthropic.rs | 9 ++++++++- crates/language_models/src/provider/anthropic.rs | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/anthropic/src/anthropic.rs b/crates/anthropic/src/anthropic.rs index 2c5acfc0f2b7051f5309d28049f75b3c8809c15b..b4dd343d07fcba7e9ed3723ecaefb69f10afc57b 100644 --- a/crates/anthropic/src/anthropic.rs +++ b/crates/anthropic/src/anthropic.rs @@ -585,6 +585,13 @@ pub enum Thinking { Enabled { budget_tokens: Option }, } +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum StringOrContents { + String(String), + Content(Vec), +} + #[derive(Debug, Serialize, Deserialize)] pub struct Request { pub model: String, @@ -597,7 +604,7 @@ pub struct Request { #[serde(default, skip_serializing_if = "Option::is_none")] pub tool_choice: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - pub system: Option, + pub system: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub metadata: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] diff --git a/crates/language_models/src/provider/anthropic.rs b/crates/language_models/src/provider/anthropic.rs index 7d2660571de0e4ff50b0bdc5d5f92d7dd27f094a..b3daaf2ecf7cf8029eafcf0cb3981f4439712cff 100644 --- a/crates/language_models/src/provider/anthropic.rs +++ b/crates/language_models/src/provider/anthropic.rs @@ -586,7 +586,11 @@ pub fn into_anthropic( model, messages: new_messages, max_tokens: max_output_tokens, - system: Some(system_message), + system: if system_message.is_empty() { + None + } else { + Some(anthropic::StringOrContents::String(system_message)) + }, thinking: if let AnthropicModelMode::Thinking { budget_tokens } = mode { Some(anthropic::Thinking::Enabled { budget_tokens }) } else {