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:
1 parent
c7846eeab9
commit
445614757b
6 files changed
+144
-560
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -2,10 +2,15 @@ package gemini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/genai"
|
||||
"github.com/sausheong/harness/llm"
|
||||
@@ -213,9 +218,25 @@ func (p *GeminiProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
if err != nil {
|
||||
argsJSON = []byte("{}")
|
||||
}
|
||||
// Gemini's FunctionCall.ID is optional and is
|
||||
// frequently empty (single, non-parallel calls
|
||||
// commonly omit it). Falling back to fc.Name is
|
||||
// wrong on two counts: it isn't a valid
|
||||
// provider-neutral tool-call ID shape, and it
|
||||
// isn't unique — two calls to the same tool in
|
||||
// one turn, or the same tool called again later
|
||||
// in the conversation (routine for a coding
|
||||
// agent re-reading files), collide on the same
|
||||
// ID. When that history is later replayed to
|
||||
// Anthropic on a routing switch, duplicate
|
||||
// tool_use ids across the conversation are
|
||||
// rejected with a 400. Mint a synthetic
|
||||
// toolu_-shaped ID instead, matching Anthropic's
|
||||
// own ID shape so a later replay to Anthropic
|
||||
// looks like any other tool_use id.
|
||||
id := fc.ID
|
||||
if id == "" {
|
||||
id = fc.Name
|
||||
id = syntheticToolCallID()
|
||||
}
|
||||
events <- llm.ChatEvent{
|
||||
Type: llm.EventToolCallStart,
|
||||
@@ -251,6 +272,24 @@ func updateUsage(_ *llm.Usage, prompt, candidates int) *llm.Usage {
|
||||
return &llm.Usage{InputTokens: prompt, OutputTokens: candidates}
|
||||
}
|
||||
|
||||
// syntheticToolCallID mints a unique, Anthropic-shaped tool-call ID
|
||||
// ("toolu_" + 16 hex chars) for Gemini function calls that arrive with
|
||||
// no ID of their own. Uniqueness (not just shape) is the point: a
|
||||
// name-based fallback collides whenever the same tool is called more
|
||||
// than once in a conversation, which produces duplicate tool_use ids if
|
||||
// that history is later replayed to Anthropic. Falls back to a
|
||||
// time+PID-derived suffix if crypto/rand is unavailable — still unique
|
||||
// within one process lifetime, which is all a single conversation needs.
|
||||
func syntheticToolCallID() string {
|
||||
b := make([]byte, 8)
|
||||
if _, err := rand.Read(b); err == nil {
|
||||
return "toolu_" + hex.EncodeToString(b)
|
||||
}
|
||||
mix := uint64(time.Now().UnixNano()) ^ uint64(os.Getpid())
|
||||
binary.BigEndian.PutUint64(b, mix)
|
||||
return "toolu_" + hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// geminiUnsupportedFields are JSON Schema fields Gemini's "OpenAPI 3.0
|
||||
// subset" rejects. This is the broadest strip set across the four
|
||||
// providers — most of the cross-provider portability gap shows up here.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -33,3 +34,22 @@ func TestGeminiUsageBuffer_KeepsLast(t *testing.T) {
|
||||
require.Equal(t, 20, last.InputTokens)
|
||||
require.Equal(t, 9, last.OutputTokens)
|
||||
}
|
||||
|
||||
// TestSyntheticToolCallID_UniqueAndAnthropicShaped guards against the bug
|
||||
// where an empty Gemini FunctionCall.ID fell back to the function name.
|
||||
// That fallback isn't a valid provider-neutral tool-call ID shape, and
|
||||
// worse, it isn't unique: two calls to the same tool (parallel calls, or
|
||||
// the same tool called again later in a conversation — routine for a
|
||||
// coding agent re-reading files) collided on one ID. When that history
|
||||
// was later replayed to Anthropic on a routing switch, the duplicate
|
||||
// tool_use ids were rejected with a 400.
|
||||
func TestSyntheticToolCallID_UniqueAndAnthropicShaped(t *testing.T) {
|
||||
seen := make(map[string]bool)
|
||||
for range 100 {
|
||||
id := syntheticToolCallID()
|
||||
require.True(t, strings.HasPrefix(id, "toolu_"),
|
||||
"synthetic ID must be shaped like an Anthropic tool_use id so a later replay to Anthropic is accepted")
|
||||
require.False(t, seen[id], "synthetic IDs must be unique across calls in the same process")
|
||||
seen[id] = true
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user