fix: preserve native Anthropic inference controls

This commit is contained in:
sausheong committed 2026-08-17 03:43:30 +08:00
1 parent 8e92125fb6
commit 986c233d39
4 files changed
+137 -11

No files matched your search

+46 -2
View File
@@ -98,10 +98,54 @@ func (p *AnthropicProvider) buildMessageParams(req llm.ChatRequest) anthropic.Me
if sys := buildAnthropicSystem(req); len(sys) > 0 {
params.System = sys
}
if req.Temperature > 0 && !anthropicAdaptiveThinkingOnly(model) {
if req.TemperatureSet {
// Protocol-proxy requests must preserve even an explicit zero. Let the
// upstream API reject an incompatible client combination unchanged.
params.Temperature = anthropic.Float(req.Temperature)
} else if req.Temperature > 0 && !anthropicAdaptiveThinkingOnly(model) {
// Portable Harness callers retain the provider's compatibility guard:
// adaptive-only models reject sampling parameters while thinking.
params.Temperature = anthropic.Float(req.Temperature)
}
if anthropicAdaptiveThinkingOnly(model) {
if native := req.NativeReasoning; native != nil {
// Proxy mode preserves exactly what the client requested. Do not infer a
// model default, silently remove sampling controls, or enlarge max_tokens:
// the upstream should accept or reject the same semantics as a direct call.
switch native.Type {
case "adaptive":
params.Thinking = anthropic.ThinkingConfigParamUnion{
OfAdaptive: &anthropic.ThinkingConfigAdaptiveParam{
Display: anthropic.ThinkingConfigAdaptiveDisplay(native.Display),
},
}
case "enabled":
params.Thinking = anthropic.ThinkingConfigParamUnion{
OfEnabled: &anthropic.ThinkingConfigEnabledParam{
BudgetTokens: native.BudgetTokens,
Display: anthropic.ThinkingConfigEnabledDisplay(native.Display),
},
}
case "disabled":
params.Thinking = anthropic.ThinkingConfigParamUnion{
OfDisabled: &anthropic.ThinkingConfigDisabledParam{},
}
}
if native.Effort != "" {
params.OutputConfig = anthropic.OutputConfigParam{
Effort: anthropic.OutputConfigEffort(native.Effort),
}
}
if len(native.OutputFormat) > 0 {
var format anthropic.JSONOutputFormatParam
if err := json.Unmarshal(native.OutputFormat, &format); err == nil {
params.OutputConfig.Format = format
} else {
// Decoders are expected to validate this before constructing the
// request. Keep this defensive diagnostic for direct Harness callers.
slog.Error("invalid native Anthropic output format", "err", err)
}
}
} else if anthropicAdaptiveThinkingOnly(model) {
// 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
+51
View File
@@ -668,6 +668,57 @@ func TestAdaptiveThinkingModels_ReasoningOffOmitsThinking(t *testing.T) {
assert.NotContains(t, got, `"output_config"`)
}
func TestNativeReasoningConfigPreservesInboundProxySemantics(t *testing.T) {
p := &AnthropicProvider{}
tests := []struct {
name string
config *llm.NativeReasoningConfig
want []string
}{
{
name: "adaptive effort max",
config: &llm.NativeReasoningConfig{Type: "adaptive", Effort: "max"},
want: []string{`"thinking":{"type":"adaptive"}`, `"output_config":{"effort":"max"}`},
},
{
name: "enabled exact budget",
config: &llm.NativeReasoningConfig{Type: "enabled", BudgetTokens: 3072},
want: []string{`"thinking":{"budget_tokens":3072,"type":"enabled"}`},
},
{
name: "explicit disabled",
config: &llm.NativeReasoningConfig{Type: "disabled"},
want: []string{`"thinking":{"type":"disabled"}`},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
params := p.buildMessageParams(llm.ChatRequest{
Model: "claude-opus-4-8-global", MaxTokens: 2048,
Messages: []llm.Message{{Role: "user", Content: "hello"}},
NativeReasoning: test.config,
})
raw, err := json.Marshal(params)
require.NoError(t, err)
for _, want := range test.want {
assert.Contains(t, string(raw), want)
}
assert.Contains(t, string(raw), `"max_tokens":2048`, "proxy mode must not enlarge max_tokens")
})
}
}
func TestNativeProxyPreservesExplicitZeroTemperature(t *testing.T) {
p := &AnthropicProvider{}
params := p.buildMessageParams(llm.ChatRequest{
Model: "claude-opus-4-8-global", MaxTokens: 32, Temperature: 0, TemperatureSet: true,
Messages: []llm.Message{{Role: "user", Content: "hello"}},
})
raw, err := json.Marshal(params)
require.NoError(t, err)
assert.Contains(t, string(raw), `"temperature":0`)
}
func TestAdaptiveThinkingModels_TemperatureDropped(t *testing.T) {
p := NewAnthropicProvider("test-key", "")
for _, model := range []string{