fix(providers): request summarized thinking from Claude models (#13029)

Signed-off-by: Hayt <211b96e6a2b7f45fd4047988976c7bbbeeda0c15f3ae7b32eec20834b5a55118@buzz.block.builderlab.xyz>
Co-authored-by: Hayt <211b96e6a2b7f45fd4047988976c7bbbeeda0c15f3ae7b32eec20834b5a55118@buzz.block.builderlab.xyz>
Co-authored-by: Lifei Zhou <lifei@squareup.com>
This commit is contained in:
Will Pfleger
2026-10-23 05:44:28 +00:00
committed by GitHub
parent 3effaa5f15
commit e970eea8ac
4 changed files with 92 additions and 17 deletions
@@ -884,6 +884,17 @@ fn apply_thinking_config(
obj.insert("thinking".to_string(), json!({"type": "disabled"}));
}
// Newer Claude models default to `display: "omitted"` and return empty thinking
// blocks. Request summaries explicitly; other models sharing this format don't
// accept the field.
if model_config.model_name.to_lowercase().contains("claude") {
if let Some(thinking) = obj.get_mut("thinking").and_then(|t| t.as_object_mut()) {
if thinking.get("type").and_then(|t| t.as_str()) != Some("disabled") {
thinking.insert("display".to_string(), json!("summarized"));
}
}
}
// `block_binding` is only accepted alongside adaptive or enabled thinking.
if let Some(behavior) = options.prefix_mismatch_behavior {
if let Some(thinking) = obj.get_mut("thinking").and_then(|t| t.as_object_mut()) {
@@ -1833,9 +1844,11 @@ mod tests {
let messages = vec![Message::user().with_text("Hello")];
let payload = create_request_with_default_options(&config, "system", &messages, &[])?;
assert_eq!(payload["thinking"]["type"], "adaptive");
assert_eq!(
payload["thinking"],
json!({"type": "adaptive", "display": "summarized"})
);
assert_eq!(payload["output_config"]["effort"], "high");
assert!(payload["thinking"].get("budget_tokens").is_none());
Ok(())
}
@@ -1854,6 +1867,7 @@ mod tests {
let payload = create_request_with_default_options(&config, "system", &messages, &[])?;
assert_eq!(payload["thinking"]["type"], "enabled");
assert_eq!(payload["thinking"]["display"], "summarized");
let budget = payload["thinking"]["budget_tokens"].as_i64().unwrap();
assert!(budget > 0);
assert_eq!(payload["max_tokens"], 64000);
@@ -1912,6 +1926,27 @@ mod tests {
Ok(())
}
#[test]
fn test_create_request_non_claude_adaptive_omits_thinking_display() -> Result<()> {
let _guard = env_lock::lock_env([("GOOSE_THINKING_EFFORT", None::<&str>)]);
let mut config = cfg("muse-spark");
config.reasoning = Some(true);
let messages = vec![Message::user().with_text("Hello")];
let payload = create_request(
"muse_code",
&config,
"system",
&messages,
&[],
AnthropicFormatOptions::default(),
)?;
assert_eq!(payload["thinking"], json!({"type": "adaptive"}));
Ok(())
}
#[test]
fn test_create_request_preserves_thinking_context_for_compatible_models() -> Result<()> {
let _guard = env_lock::lock_env([
@@ -1943,6 +1978,7 @@ mod tests {
assert_eq!(payload["thinking"]["type"], "enabled");
assert!(payload["thinking"]["budget_tokens"].as_i64().unwrap() >= 1024);
assert_eq!(payload["thinking"]["clear_thinking"], false);
assert!(payload["thinking"].get("display").is_none());
assert_eq!(payload["max_tokens"], 64000);
assert_eq!(payload["messages"][0]["content"][0]["type"], "thinking");
assert_eq!(payload["messages"][0]["content"][0]["thinking"], "internal");
@@ -300,7 +300,10 @@ fn apply_claude_thinking_config(
match thinking_type_for_provider(provider_name, model_config) {
ThinkingType::Adaptive => {
obj.insert("thinking".to_string(), json!({ "type": "adaptive" }));
obj.insert(
"thinking".to_string(),
json!({ "type": "adaptive", "display": "summarized" }),
);
obj.insert(
"output_config".to_string(),
json!({ "effort": adaptive_output_effort(model_config).to_string() }),
@@ -318,7 +321,8 @@ fn apply_claude_thinking_config(
"thinking".to_string(),
json!({
"type": "enabled",
"budget_tokens": budget_tokens
"budget_tokens": budget_tokens,
"display": "summarized"
}),
);
obj.insert("temperature".to_string(), json!(2));
@@ -1527,7 +1531,10 @@ mod tests {
let request = create_request(&model_config, "system", &[], &[], &ImageFormat::OpenAi)?;
assert_eq!(request["thinking"]["type"], "adaptive");
assert_eq!(
request["thinking"],
json!({"type": "adaptive", "display": "summarized"})
);
assert_eq!(request["output_config"]["effort"], "low");
assert!(request.get("temperature").is_none());
assert_eq!(request["max_completion_tokens"], 4096);
@@ -1581,6 +1588,26 @@ mod tests {
Ok(())
}
#[test]
fn test_create_request_disabled_thinking_omits_display() -> anyhow::Result<()> {
let _guard = env_lock::lock_env([("GOOSE_THINKING_EFFORT", None::<&str>)]);
let mut params = std::collections::HashMap::new();
params.insert("thinking_effort".to_string(), serde_json::json!("off"));
let mut model_config = ModelConfig::new("databricks-claude-sonnet-4.5");
model_config.request_params = Some(params.clone());
let request = create_request(&model_config, "system", &[], &[], &ImageFormat::OpenAi)?;
assert!(request.get("thinking").is_none());
// Adaptive models need an explicit disable, which must not carry `display`.
let mut model_config = ModelConfig::new("databricks-claude-opus-4-7");
model_config.request_params = Some(params);
let request = create_request(&model_config, "system", &[], &[], &ImageFormat::OpenAi)?;
assert_eq!(request["thinking"], json!({"type": "disabled"}));
Ok(())
}
#[test]
fn test_create_request_enabled_thinking_with_budget() -> anyhow::Result<()> {
let mut model_config = ModelConfig::new("databricks-claude-sonnet-4.5");
@@ -1591,8 +1618,10 @@ mod tests {
let request = create_request(&model_config, "system", &[], &[], &ImageFormat::OpenAi)?;
assert_eq!(request["thinking"]["type"], "enabled");
assert_eq!(request["thinking"]["budget_tokens"], 16000);
assert_eq!(
request["thinking"],
json!({"type": "enabled", "budget_tokens": 16000, "display": "summarized"})
);
assert_eq!(request["max_tokens"], 20096);
assert_eq!(request["temperature"], 2);
assert!(request.get("max_completion_tokens").is_none());
+2 -1
View File
@@ -971,7 +971,8 @@ mod tests {
.and(path("/ai-gateway/anthropic/v1/messages"))
.and(body_partial_json(json!({
"model": model,
"system": [{"cache_control": {"type": "ephemeral"}}]
"system": [{"cache_control": {"type": "ephemeral"}}],
"thinking": {"type": "adaptive", "display": "summarized"}
})))
.respond_with(
ResponseTemplate::new(200)
+18 -9
View File
@@ -34,10 +34,13 @@ pub fn bedrock_anthropic_thinking_fields(model_config: &ModelConfig) -> Option<D
let anthropic_config = bedrock_anthropic_model_config(model_config)?;
let thinking_type = thinking_type_for_provider(ANTHROPIC_PROVIDER_NAME, &anthropic_config);
let thinking = match thinking_type {
ThinkingType::Adaptive => Document::Object(HashMap::from([(
"type".to_string(),
Document::String("adaptive".to_string()),
)])),
ThinkingType::Adaptive => Document::Object(HashMap::from([
("type".to_string(), Document::String("adaptive".to_string())),
(
"display".to_string(),
Document::String("summarized".to_string()),
),
])),
ThinkingType::Enabled => {
// Thinking tokens count against `maxTokens`, which `bedrock_inference_config`
// now sends when explicitly configured. Mirror the Anthropic formatter: clamp
@@ -57,6 +60,10 @@ pub fn bedrock_anthropic_thinking_fields(model_config: &ModelConfig) -> Option<D
"budget_tokens".to_string(),
Document::Number(Number::PosInt(budget_tokens as u64)),
),
(
"display".to_string(),
Document::String("summarized".to_string()),
),
]))
}
ThinkingType::Disabled => {
@@ -677,7 +684,8 @@ mod tests {
json!({
"thinking": {
"type": "enabled",
"budget_tokens": 4000
"budget_tokens": 4000,
"display": "summarized"
}
})
);
@@ -701,7 +709,8 @@ mod tests {
json!({
"thinking": {
"type": "enabled",
"budget_tokens": 3000 - 1024
"budget_tokens": 3000 - 1024,
"display": "summarized"
}
})
);
@@ -753,7 +762,7 @@ mod tests {
assert_eq!(
from_bedrock_json(&fields).unwrap(),
json!({
"thinking": {"type": "adaptive"},
"thinking": {"type": "adaptive", "display": "summarized"},
"output_config": {"effort": "high"}
})
);
@@ -772,7 +781,7 @@ mod tests {
assert_eq!(
from_bedrock_json(&fields).unwrap(),
json!({
"thinking": {"type": "adaptive"},
"thinking": {"type": "adaptive", "display": "summarized"},
"output_config": {"effort": "low"}
})
);
@@ -791,7 +800,7 @@ mod tests {
assert_eq!(
from_bedrock_json(&fields).unwrap(),
json!({
"thinking": {"type": "adaptive"},
"thinking": {"type": "adaptive", "display": "summarized"},
"output_config": {"effort": "low"}
})
);