fix: support Claude Opus 5 / Sonnet 5 adaptive thinking; unique Gemini tool-call IDs

Anthropic: add claude-opus-5 and claude-sonnet-5 to the adaptive-thinking-only
model list. These models also think by default when the thinking param is
omitted (unlike Fable 5/Mythos/Opus 4.7/4.8), so ReasoningOff now sends an
explicit {type:"disabled"} on them instead of relying on omission.

Gemini: replace the empty-FunctionCall.ID fallback (function name) with a
synthetic toolu_-shaped random ID. The name-based fallback wasn't unique —
repeated calls to the same tool in one conversation collided on one ID,
which produced duplicate tool_use ids if that history was later replayed to
a different (Anthropic) model on a routing switch, rejected with a 400.
This commit is contained in:
sausheong committed 2026-07-28 13:40:22 +08:00
1 parent c7846eeab9
commit 445614757b
6 files changed
+144 -560

No files matched your search

+45 -10
View File
@@ -102,12 +102,10 @@ func (p *AnthropicProvider) buildMessageParams(req llm.ChatRequest) anthropic.Me
params.Temperature = anthropic.Float(req.Temperature)
}
if anthropicAdaptiveThinkingOnly(model) {
// Fable 5 / Mythos 5 / Opus 4.7+ reject the legacy
// enabled+budget_tokens config (400 "thinking.type.enabled is
// not supported"). They take {type:"adaptive"} plus
// Fable 5 / Mythos 5 / Opus 4.7+ / Opus 5 / Sonnet 5 reject the
// legacy enabled+budget_tokens config (400 "thinking.type.enabled
// is not supported"). They take {type:"adaptive"} plus
// output_config.effort; sampling params are also removed.
// Reasoning off → omit thinking entirely (Fable 5 rejects an
// explicit {type:"disabled"} too).
if req.Reasoning != llm.ReasoningOff {
params.Thinking = anthropic.ThinkingConfigParamUnion{
OfAdaptive: &anthropic.ThinkingConfigAdaptiveParam{},
@@ -118,7 +116,22 @@ func (p *AnthropicProvider) buildMessageParams(req llm.ChatRequest) anthropic.Me
slog.Info("anthropic adaptive thinking",
"model", model,
"effort", string(anthropicEffort(req.Reasoning)))
} else if anthropicThinksByDefault(model) {
// Opus 5 and Sonnet 5 think by default when the thinking
// param is omitted entirely (unlike Opus 4.7/4.8, Fable 5,
// and Mythos, where omitting it means no thinking) — so
// expressing "reasoning off" here requires an explicit
// {type:"disabled"}. Accepted at effort high or below;
// our ReasoningMode never asks for more than high.
params.Thinking = anthropic.ThinkingConfigParamUnion{
OfDisabled: &anthropic.ThinkingConfigDisabledParam{},
}
}
// Else (Fable 5 / Mythos / Opus 4.7/4.8, reasoning off): omit
// thinking entirely. Fable 5 and Mythos reject an explicit
// {type:"disabled"} at any effort; Opus 4.7/4.8 already default
// to no thinking when the param is omitted, so there's nothing
// to disable.
} else if cfg, ok := p.BuildThinkingConfig(model, req.Reasoning); ok {
required := cfg.BudgetTokens + 4096
if maxTokens < required {
@@ -456,11 +469,12 @@ func (p *AnthropicProvider) BuildThinkingConfig(model string, mode llm.Reasoning
}
// anthropicAdaptiveThinkingOnly reports whether the model accepts ONLY
// adaptive thinking: Claude Fable 5, Claude Mythos 5, and Opus 4.7+.
// On these the legacy {type:"enabled", budget_tokens:N} config and
// sampling params (temperature/top_p/top_k) return HTTP 400. Matched by
// substring so gateway model-group names (claude-fable-5-global) and
// Bedrock-style IDs (us.anthropic.claude-opus-4-8-v1) are covered.
// adaptive thinking: Claude Fable 5, Claude Mythos 5, Opus 4.7+, Opus 5,
// and Sonnet 5. On these the legacy {type:"enabled", budget_tokens:N}
// config and sampling params (temperature/top_p/top_k) return HTTP 400.
// Matched by substring so gateway model-group names
// (claude-fable-5-global) and Bedrock-style IDs
// (us.anthropic.claude-opus-4-8-v1) are covered.
func anthropicAdaptiveThinkingOnly(model string) bool {
adaptiveOnly := []string{
"claude-fable-5",
@@ -469,6 +483,8 @@ func anthropicAdaptiveThinkingOnly(model string) bool {
"claude-opus-4-7",
"claude-opus-4-8",
"claude-opus-4-9", // future-proof within the 4.x line
"claude-opus-5",
"claude-sonnet-5",
}
for _, marker := range adaptiveOnly {
if strings.Contains(model, marker) {
@@ -478,6 +494,25 @@ func anthropicAdaptiveThinkingOnly(model string) bool {
return false
}
// anthropicThinksByDefault reports whether the model runs adaptive
// thinking when the thinking param is omitted entirely — true for Opus 5
// and Sonnet 5 only. Fable 5, Mythos, and Opus 4.7/4.8 do NOT think by
// default (omitting thinking means no thinking on those), so expressing
// "reasoning off" there is a no-op omission; on Opus 5 / Sonnet 5 it
// requires an explicit {type:"disabled"} or the model thinks anyway.
func anthropicThinksByDefault(model string) bool {
thinksByDefault := []string{
"claude-opus-5",
"claude-sonnet-5",
}
for _, marker := range thinksByDefault {
if strings.Contains(model, marker) {
return true
}
}
return false
}
// anthropicEffort maps the unified ReasoningMode onto output_config.effort
// for adaptive-thinking-only models.
func anthropicEffort(mode llm.ReasoningMode) anthropic.OutputConfigEffort {
+39 -7
View File
@@ -584,12 +584,18 @@ data: {"type":"message_stop"}
// --- Adaptive-thinking model mapping ---------------------------------
//
// Claude Fable 5, Claude Mythos 5, and Opus 4.7/4.8 reject the legacy
// thinking config: `thinking: {type:"enabled", budget_tokens:N}` returns
// HTTP 400 ("thinking.type.enabled is not supported for this model").
// They also removed sampling params — sending `temperature` is a 400.
// For these models the provider must emit `thinking: {type:"adaptive"}`
// plus `output_config.effort`, and drop temperature.
// Claude Fable 5, Claude Mythos 5, Opus 4.7/4.8, Opus 5, and Sonnet 5
// reject the legacy thinking config: `thinking: {type:"enabled",
// budget_tokens:N}` returns HTTP 400 ("thinking.type.enabled is not
// supported for this model"). They also removed sampling params —
// sending `temperature` is a 400. For these models the provider must
// emit `thinking: {type:"adaptive"}` plus `output_config.effort`, and
// drop temperature.
//
// Opus 5 and Sonnet 5 additionally think BY DEFAULT when the thinking
// param is omitted — unlike Fable 5/Mythos/Opus 4.7/4.8, where omitting
// it means no thinking. So "reasoning off" on Opus 5/Sonnet 5 must send
// an explicit {type:"disabled"} rather than omit the param.
func wireJSON(t *testing.T, p *AnthropicProvider, req llm.ChatRequest) string {
t.Helper()
@@ -608,6 +614,8 @@ func TestAdaptiveThinkingModels_EmitAdaptiveNotEnabled(t *testing.T) {
"claude-opus-4-7",
"claude-opus-4-8",
"us.anthropic.claude-opus-4-8-v1", // Bedrock-style ID
"claude-opus-5",
"claude-sonnet-5",
}
for _, model := range models {
t.Run(model, func(t *testing.T) {
@@ -662,7 +670,10 @@ func TestAdaptiveThinkingModels_ReasoningOffOmitsThinking(t *testing.T) {
func TestAdaptiveThinkingModels_TemperatureDropped(t *testing.T) {
p := NewAnthropicProvider("test-key", "")
for _, model := range []string{"claude-fable-5", "claude-opus-4-7", "claude-opus-4-8"} {
for _, model := range []string{
"claude-fable-5", "claude-opus-4-7", "claude-opus-4-8",
"claude-opus-5", "claude-sonnet-5",
} {
t.Run(model, func(t *testing.T) {
got := wireJSON(t, p, llm.ChatRequest{
Model: model,
@@ -676,6 +687,27 @@ func TestAdaptiveThinkingModels_TemperatureDropped(t *testing.T) {
}
}
// TestOpus5AndSonnet5_ThinkByDefault_ReasoningOffSendsDisabled reproduces
// the bug this test guards against: Opus 5 and Sonnet 5 think adaptively
// when `thinking` is omitted (unlike every other adaptive-only model),
// so simply omitting the param to express "reasoning off" leaves
// thinking on. The provider must send an explicit {type:"disabled"}.
func TestOpus5AndSonnet5_ThinkByDefault_ReasoningOffSendsDisabled(t *testing.T) {
p := NewAnthropicProvider("test-key", "")
for _, model := range []string{"claude-opus-5", "claude-sonnet-5"} {
t.Run(model, func(t *testing.T) {
got := wireJSON(t, p, llm.ChatRequest{
Model: model,
Messages: []llm.Message{{Role: "user", Content: "hi"}},
})
assert.Contains(t, got, `"thinking":{"type":"disabled"}`,
"Opus 5/Sonnet 5 think by default when thinking is omitted; "+
"reasoning off must send an explicit disabled config")
assert.NotContains(t, got, `"output_config"`)
})
}
}
func TestLegacyThinkingModels_KeepBudgetTokens(t *testing.T) {
p := NewAnthropicProvider("test-key", "")
got := wireJSON(t, p, llm.ChatRequest{