hkfires commited on
Commit
36f3ffa
·
1 Parent(s): 0fe3454

fix(thinking): normalize effort mapping

Browse files

Route OpenAI reasoning effort through ThinkingEffortToBudget for Claude
translators, preserve "minimal" when translating OpenAI Responses, and
treat blank/unknown efforts as no-ops for Gemini thinking configs.

Also map budget -1 to "auto" and expand cross-protocol thinking tests.

internal/translator/claude/openai/chat-completions/claude_openai_request.go CHANGED
@@ -67,19 +67,22 @@ func ConvertOpenAIRequestToClaude(modelName string, inputRawJSON []byte, stream
67
  root := gjson.ParseBytes(rawJSON)
68
 
69
  if v := root.Get("reasoning_effort"); v.Exists() && util.ModelSupportsThinking(modelName) && !util.ModelUsesThinkingLevels(modelName) {
70
- out, _ = sjson.Set(out, "thinking.type", "enabled")
71
-
72
- switch v.String() {
73
- case "none":
74
- out, _ = sjson.Set(out, "thinking.type", "disabled")
75
- case "low":
76
- out, _ = sjson.Set(out, "thinking.budget_tokens", 1024)
77
- case "medium":
78
- out, _ = sjson.Set(out, "thinking.budget_tokens", 8192)
79
- case "high":
80
- out, _ = sjson.Set(out, "thinking.budget_tokens", 24576)
81
- case "xhigh":
82
- out, _ = sjson.Set(out, "thinking.budget_tokens", 32768)
 
 
 
83
  }
84
  }
85
 
 
67
  root := gjson.ParseBytes(rawJSON)
68
 
69
  if v := root.Get("reasoning_effort"); v.Exists() && util.ModelSupportsThinking(modelName) && !util.ModelUsesThinkingLevels(modelName) {
70
+ effort := strings.ToLower(strings.TrimSpace(v.String()))
71
+ if effort != "" {
72
+ budget, ok := util.ThinkingEffortToBudget(modelName, effort)
73
+ if ok {
74
+ switch budget {
75
+ case 0:
76
+ out, _ = sjson.Set(out, "thinking.type", "disabled")
77
+ case -1:
78
+ out, _ = sjson.Set(out, "thinking.type", "enabled")
79
+ default:
80
+ if budget > 0 {
81
+ out, _ = sjson.Set(out, "thinking.type", "enabled")
82
+ out, _ = sjson.Set(out, "thinking.budget_tokens", budget)
83
+ }
84
+ }
85
+ }
86
  }
87
  }
88
 
internal/translator/claude/openai/responses/claude_openai-responses_request.go CHANGED
@@ -54,21 +54,22 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
54
  root := gjson.ParseBytes(rawJSON)
55
 
56
  if v := root.Get("reasoning.effort"); v.Exists() && util.ModelSupportsThinking(modelName) && !util.ModelUsesThinkingLevels(modelName) {
57
- out, _ = sjson.Set(out, "thinking.type", "enabled")
58
-
59
- switch v.String() {
60
- case "none":
61
- out, _ = sjson.Set(out, "thinking.type", "disabled")
62
- case "minimal":
63
- out, _ = sjson.Set(out, "thinking.budget_tokens", 512)
64
- case "low":
65
- out, _ = sjson.Set(out, "thinking.budget_tokens", 1024)
66
- case "medium":
67
- out, _ = sjson.Set(out, "thinking.budget_tokens", 8192)
68
- case "high":
69
- out, _ = sjson.Set(out, "thinking.budget_tokens", 24576)
70
- case "xhigh":
71
- out, _ = sjson.Set(out, "thinking.budget_tokens", 32768)
 
72
  }
73
  }
74
 
 
54
  root := gjson.ParseBytes(rawJSON)
55
 
56
  if v := root.Get("reasoning.effort"); v.Exists() && util.ModelSupportsThinking(modelName) && !util.ModelUsesThinkingLevels(modelName) {
57
+ effort := strings.ToLower(strings.TrimSpace(v.String()))
58
+ if effort != "" {
59
+ budget, ok := util.ThinkingEffortToBudget(modelName, effort)
60
+ if ok {
61
+ switch budget {
62
+ case 0:
63
+ out, _ = sjson.Set(out, "thinking.type", "disabled")
64
+ case -1:
65
+ out, _ = sjson.Set(out, "thinking.type", "enabled")
66
+ default:
67
+ if budget > 0 {
68
+ out, _ = sjson.Set(out, "thinking.type", "enabled")
69
+ out, _ = sjson.Set(out, "thinking.budget_tokens", budget)
70
+ }
71
+ }
72
+ }
73
  }
74
  }
75
 
internal/translator/openai/openai/responses/openai_openai-responses_request.go CHANGED
@@ -195,7 +195,7 @@ func ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName string, inpu
195
  case "auto":
196
  out, _ = sjson.Set(out, "reasoning_effort", "auto")
197
  case "minimal":
198
- out, _ = sjson.Set(out, "reasoning_effort", "low")
199
  case "low":
200
  out, _ = sjson.Set(out, "reasoning_effort", "low")
201
  case "medium":
 
195
  case "auto":
196
  out, _ = sjson.Set(out, "reasoning_effort", "auto")
197
  case "minimal":
198
+ out, _ = sjson.Set(out, "reasoning_effort", "minimal")
199
  case "low":
200
  out, _ = sjson.Set(out, "reasoning_effort", "low")
201
  case "medium":
internal/util/gemini_thinking.go CHANGED
@@ -167,20 +167,26 @@ var ReasoningEffortBudgetMapping = map[string]int{
167
  // for standard Gemini API format (generationConfig.thinkingConfig path).
168
  // Returns the modified body with thinkingBudget and include_thoughts set.
169
  func ApplyReasoningEffortToGemini(body []byte, effort string) []byte {
170
- budget, ok := ReasoningEffortBudgetMapping[effort]
171
- if !ok {
172
- budget = -1 // default to auto
173
  }
174
 
175
  budgetPath := "generationConfig.thinkingConfig.thinkingBudget"
176
  includePath := "generationConfig.thinkingConfig.include_thoughts"
177
 
178
- if effort == "none" {
179
  body, _ = sjson.DeleteBytes(body, "generationConfig.thinkingConfig")
180
- } else {
181
- body, _ = sjson.SetBytes(body, budgetPath, budget)
182
- body, _ = sjson.SetBytes(body, includePath, true)
 
 
 
183
  }
 
 
 
184
  return body
185
  }
186
 
@@ -188,20 +194,26 @@ func ApplyReasoningEffortToGemini(body []byte, effort string) []byte {
188
  // for Gemini CLI API format (request.generationConfig.thinkingConfig path).
189
  // Returns the modified body with thinkingBudget and include_thoughts set.
190
  func ApplyReasoningEffortToGeminiCLI(body []byte, effort string) []byte {
191
- budget, ok := ReasoningEffortBudgetMapping[effort]
192
- if !ok {
193
- budget = -1 // default to auto
194
  }
195
 
196
  budgetPath := "request.generationConfig.thinkingConfig.thinkingBudget"
197
  includePath := "request.generationConfig.thinkingConfig.include_thoughts"
198
 
199
- if effort == "none" {
200
  body, _ = sjson.DeleteBytes(body, "request.generationConfig.thinkingConfig")
201
- } else {
202
- body, _ = sjson.SetBytes(body, budgetPath, budget)
203
- body, _ = sjson.SetBytes(body, includePath, true)
 
 
 
204
  }
 
 
 
205
  return body
206
  }
207
 
 
167
  // for standard Gemini API format (generationConfig.thinkingConfig path).
168
  // Returns the modified body with thinkingBudget and include_thoughts set.
169
  func ApplyReasoningEffortToGemini(body []byte, effort string) []byte {
170
+ normalized := strings.ToLower(strings.TrimSpace(effort))
171
+ if normalized == "" {
172
+ return body
173
  }
174
 
175
  budgetPath := "generationConfig.thinkingConfig.thinkingBudget"
176
  includePath := "generationConfig.thinkingConfig.include_thoughts"
177
 
178
+ if normalized == "none" {
179
  body, _ = sjson.DeleteBytes(body, "generationConfig.thinkingConfig")
180
+ return body
181
+ }
182
+
183
+ budget, ok := ReasoningEffortBudgetMapping[normalized]
184
+ if !ok {
185
+ return body
186
  }
187
+
188
+ body, _ = sjson.SetBytes(body, budgetPath, budget)
189
+ body, _ = sjson.SetBytes(body, includePath, true)
190
  return body
191
  }
192
 
 
194
  // for Gemini CLI API format (request.generationConfig.thinkingConfig path).
195
  // Returns the modified body with thinkingBudget and include_thoughts set.
196
  func ApplyReasoningEffortToGeminiCLI(body []byte, effort string) []byte {
197
+ normalized := strings.ToLower(strings.TrimSpace(effort))
198
+ if normalized == "" {
199
+ return body
200
  }
201
 
202
  budgetPath := "request.generationConfig.thinkingConfig.thinkingBudget"
203
  includePath := "request.generationConfig.thinkingConfig.include_thoughts"
204
 
205
+ if normalized == "none" {
206
  body, _ = sjson.DeleteBytes(body, "request.generationConfig.thinkingConfig")
207
+ return body
208
+ }
209
+
210
+ budget, ok := ReasoningEffortBudgetMapping[normalized]
211
+ if !ok {
212
+ return body
213
  }
214
+
215
+ body, _ = sjson.SetBytes(body, budgetPath, budget)
216
+ body, _ = sjson.SetBytes(body, includePath, true)
217
  return body
218
  }
219
 
internal/util/openai_thinking.go CHANGED
@@ -5,15 +5,18 @@ package util
5
  //
6
  // Ranges:
7
  // - 0 -> "none"
 
8
  // - 1..1024 -> "low"
9
  // - 1025..8192 -> "medium"
10
  // - 8193..24576 -> "high"
11
  // - 24577.. -> highest supported level for the model (defaults to "xhigh")
12
  //
13
- // Negative values (except the dynamic -1 handled elsewhere) are treated as unsupported.
14
  func OpenAIThinkingBudgetToEffort(model string, budget int) (string, bool) {
15
  switch {
16
- case budget < 0:
 
 
17
  return "", false
18
  case budget == 0:
19
  return "none", true
 
5
  //
6
  // Ranges:
7
  // - 0 -> "none"
8
+ // - -1 -> "auto"
9
  // - 1..1024 -> "low"
10
  // - 1025..8192 -> "medium"
11
  // - 8193..24576 -> "high"
12
  // - 24577.. -> highest supported level for the model (defaults to "xhigh")
13
  //
14
+ // Negative values other than -1 are treated as unsupported.
15
  func OpenAIThinkingBudgetToEffort(model string, budget int) (string, bool) {
16
  switch {
17
+ case budget == -1:
18
+ return "auto", true
19
+ case budget < -1:
20
  return "", false
21
  case budget == 0:
22
  return "none", true
test/thinking_conversion_test.go CHANGED
@@ -66,6 +66,37 @@ func registerCoreModels(t *testing.T) func() {
66
  }
67
  }
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  func buildRawPayload(fromProtocol, modelWithSuffix string) []byte {
70
  switch fromProtocol {
71
  case "gemini":
@@ -101,19 +132,30 @@ func applyReasoningEffortMetadataLocal(payload []byte, metadata map[string]any,
101
  if field == "" {
102
  return payload
103
  }
104
- if !util.ModelSupportsThinking(model) && !allowCompat {
 
 
 
 
105
  return payload
106
  }
107
  if effort, ok := util.ReasoningEffortFromMetadata(metadata); ok && effort != "" {
108
- if util.ModelUsesThinkingLevels(model) || allowCompat {
109
  if updated, err := sjson.SetBytes(payload, field, effort); err == nil {
110
  return updated
111
  }
112
  }
113
  }
114
- if util.ModelUsesThinkingLevels(model) || allowCompat {
 
115
  if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil {
116
- if effort, ok := util.OpenAIThinkingBudgetToEffort(model, *budget); ok && effort != "" {
 
 
 
 
 
 
117
  if updated, err := sjson.SetBytes(payload, field, effort); err == nil {
118
  return updated
119
  }
@@ -320,79 +362,46 @@ func TestThinkingConversionsAcrossProtocolsAndModels(t *testing.T) {
320
  cleanup := registerCoreModels(t)
321
  defer cleanup()
322
 
323
- models := []string{
324
- "gpt-5", // supports levels (level-based thinking)
325
- "gemini-2.5-pro", // supports numeric budget
326
- "qwen3-code-plus", // no thinking support
327
- "openai-compat", // openai-compatible channel (allowCompat=true)
328
- }
329
- fromProtocols := []string{"openai", "claude", "gemini", "openai-response"}
330
- toProtocols := []string{"gemini", "claude", "openai", "codex"}
331
-
332
  type scenario struct {
333
  name string
334
  modelSuffix string
335
- expectFn func(info *registry.ModelInfo) (present bool, budget int64)
336
  }
337
 
338
- buildBudgetFn := func(raw int) func(info *registry.ModelInfo) (bool, int64) {
339
- return func(info *registry.ModelInfo) (bool, int64) {
340
- if info == nil || info.Thinking == nil {
341
- return false, 0
342
- }
343
- return true, int64(util.NormalizeThinkingBudget(info.ID, raw))
344
- }
345
- }
346
-
347
- levelBudgetFn := func(level string) func(info *registry.ModelInfo) (bool, int64) {
348
- return func(info *registry.ModelInfo) (bool, int64) {
349
- if info == nil || info.Thinking == nil {
350
- return false, 0
351
- }
352
- if b, ok := util.ThinkingEffortToBudget(info.ID, level); ok {
353
- return true, int64(b)
354
- }
355
- return false, 0
356
  }
 
357
  }
358
 
359
- for _, model := range models {
360
  _ = registry.GetGlobalRegistry().GetModelInfo(model)
361
 
362
- for _, from := range fromProtocols {
363
  // Scenario selection follows protocol semantics:
364
  // - OpenAI-style protocols (openai/openai-response) express thinking as levels.
365
  // - Claude/Gemini-style protocols express thinking as numeric budgets.
366
  cases := []scenario{
367
- {name: "no-suffix", modelSuffix: model, expectFn: func(_ *registry.ModelInfo) (bool, int64) { return false, 0 }},
368
  }
369
  if from == "openai" || from == "openai-response" {
370
- // Level-based test cases: auto, none, minimal, low, medium, high, xhigh, foo(invalid)
371
- // Maps to numeric: -1, 0, 512, 1024, 8192, 24576, 32768, invalid
372
- cases = append(cases,
373
- scenario{name: "level-auto", modelSuffix: fmt.Sprintf("%s(auto)", model), expectFn: levelBudgetFn("auto")},
374
- scenario{name: "level-none", modelSuffix: fmt.Sprintf("%s(none)", model), expectFn: levelBudgetFn("none")},
375
- scenario{name: "level-minimal", modelSuffix: fmt.Sprintf("%s(minimal)", model), expectFn: levelBudgetFn("minimal")},
376
- scenario{name: "level-low", modelSuffix: fmt.Sprintf("%s(low)", model), expectFn: levelBudgetFn("low")},
377
- scenario{name: "level-medium", modelSuffix: fmt.Sprintf("%s(medium)", model), expectFn: levelBudgetFn("medium")},
378
- scenario{name: "level-high", modelSuffix: fmt.Sprintf("%s(high)", model), expectFn: levelBudgetFn("high")},
379
- scenario{name: "level-xhigh", modelSuffix: fmt.Sprintf("%s(xhigh)", model), expectFn: levelBudgetFn("xhigh")},
380
- scenario{name: "level-invalid", modelSuffix: fmt.Sprintf("%s(invalid)", model), expectFn: levelBudgetFn("invalid")},
381
- )
382
  } else { // claude or gemini
383
- // Numeric test cases: -1, 0, 1023, 1025, 8193, 24577
384
- // Maps to levels: auto, none, low, medium, high, xhigh
385
- cases = append(cases,
386
- scenario{name: "numeric-neg1", modelSuffix: fmt.Sprintf("%s(-1)", model), expectFn: buildBudgetFn(-1)},
387
- scenario{name: "numeric-0", modelSuffix: fmt.Sprintf("%s(0)", model), expectFn: buildBudgetFn(0)},
388
- scenario{name: "numeric-1023", modelSuffix: fmt.Sprintf("%s(1023)", model), expectFn: buildBudgetFn(1023)},
389
- scenario{name: "numeric-1025", modelSuffix: fmt.Sprintf("%s(1025)", model), expectFn: buildBudgetFn(1025)},
390
- scenario{name: "numeric-8193", modelSuffix: fmt.Sprintf("%s(8193)", model), expectFn: buildBudgetFn(8193)},
391
- scenario{name: "numeric-24577", modelSuffix: fmt.Sprintf("%s(24577)", model), expectFn: buildBudgetFn(24577)},
392
- )
393
  }
394
 
395
- for _, to := range toProtocols {
396
  if from == to {
397
  continue
398
  }
@@ -585,7 +594,7 @@ func buildRawPayloadWithThinking(fromProtocol, model string, thinkingParam any)
585
  return []byte(base)
586
  case "claude":
587
  base := fmt.Sprintf(`{"model":"%s","messages":[{"role":"user","content":"hi"}]}`, model)
588
- if budget, ok := thinkingParam.(int); ok && budget > 0 {
589
  base, _ = sjson.Set(base, "thinking.type", "enabled")
590
  base, _ = sjson.Set(base, "thinking.budget_tokens", budget)
591
  }
@@ -636,55 +645,50 @@ func TestRawPayloadThinkingConversions(t *testing.T) {
636
  cleanup := registerCoreModels(t)
637
  defer cleanup()
638
 
639
- models := []string{
640
- "gpt-5", // supports levels (level-based thinking)
641
- "gemini-2.5-pro", // supports numeric budget
642
- "qwen3-code-plus", // no thinking support
643
- "openai-compat", // openai-compatible channel (allowCompat=true)
644
- }
645
- fromProtocols := []string{"openai", "claude", "gemini", "openai-response"}
646
- toProtocols := []string{"gemini", "claude", "openai", "codex"}
647
-
648
  type scenario struct {
649
  name string
650
  thinkingParam any // int for budget, string for effort level
651
  }
652
 
653
- for _, model := range models {
 
 
 
 
 
 
 
654
  supportsThinking := util.ModelSupportsThinking(model)
655
  usesLevels := util.ModelUsesThinkingLevels(model)
656
  allowCompat := isOpenAICompatModel(model)
657
 
658
- for _, from := range fromProtocols {
659
  var cases []scenario
660
  switch from {
661
  case "openai", "openai-response":
662
- // Level-based test cases: auto, none, minimal, low, medium, high, xhigh, foo(invalid)
663
  cases = []scenario{
664
  {name: "no-thinking", thinkingParam: nil},
665
- {name: "effort-auto", thinkingParam: "auto"},
666
- {name: "effort-none", thinkingParam: "none"},
667
- {name: "effort-minimal", thinkingParam: "minimal"},
668
- {name: "effort-low", thinkingParam: "low"},
669
- {name: "effort-medium", thinkingParam: "medium"},
670
- {name: "effort-high", thinkingParam: "high"},
671
- {name: "effort-xhigh", thinkingParam: "xhigh"},
672
- {name: "effort-invalid", thinkingParam: "invalid"},
673
  }
674
  case "gemini", "claude":
675
- // Numeric test cases: -1, 0, 1023, 1025, 8193, 24577
676
  cases = []scenario{
677
  {name: "no-thinking", thinkingParam: nil},
678
- {name: "budget-neg1", thinkingParam: -1},
679
- {name: "budget-0", thinkingParam: 0},
680
- {name: "budget-1023", thinkingParam: 1023},
681
- {name: "budget-1025", thinkingParam: 1025},
682
- {name: "budget-8193", thinkingParam: 8193},
683
- {name: "budget-24577", thinkingParam: 24577},
 
684
  }
685
  }
686
 
687
- for _, to := range toProtocols {
688
  if from == to {
689
  continue
690
  }
@@ -727,8 +731,8 @@ func TestRawPayloadThinkingConversions(t *testing.T) {
727
  // ThinkingEffortToBudget already returns normalized budget
728
  return true, fmt.Sprintf("%d", budget), false
729
  }
730
- // Invalid effort maps to default auto (-1)
731
- return true, "-1", false
732
  }
733
  return false, "", false
734
  case "claude":
@@ -758,14 +762,8 @@ func TestRawPayloadThinkingConversions(t *testing.T) {
758
  case "openai":
759
  if allowCompat {
760
  if effort, ok := cs.thinkingParam.(string); ok && strings.TrimSpace(effort) != "" {
761
- // For allowCompat models, invalid effort values are normalized to "auto"
762
  normalized := strings.ToLower(strings.TrimSpace(effort))
763
- switch normalized {
764
- case "none", "auto", "low", "medium", "high", "xhigh":
765
- return true, normalized, false
766
- default:
767
- return true, "auto", false
768
- }
769
  }
770
  if budget, ok := cs.thinkingParam.(int); ok {
771
  if mapped, okM := util.OpenAIThinkingBudgetToEffort(model, budget); okM && mapped != "" {
@@ -891,6 +889,7 @@ func TestOpenAIThinkingBudgetToEffortRanges(t *testing.T) {
891
  want string
892
  ok bool
893
  }{
 
894
  {name: "zero-none", model: "gpt-5", budget: 0, want: "none", ok: true},
895
  {name: "low-min", model: "gpt-5", budget: 1, want: "low", ok: true},
896
  {name: "low-max", model: "gpt-5", budget: 1024, want: "low", ok: true},
@@ -898,7 +897,7 @@ func TestOpenAIThinkingBudgetToEffortRanges(t *testing.T) {
898
  {name: "medium-max", model: "gpt-5", budget: 8192, want: "medium", ok: true},
899
  {name: "high-min", model: "gpt-5", budget: 8193, want: "high", ok: true},
900
  {name: "high-max", model: "gpt-5", budget: 24576, want: "high", ok: true},
901
- {name: "over-max-clamps-to-highest", model: "gpt-5", budget: 24577, want: "high", ok: true},
902
  {name: "over-max-xhigh-model", model: "gpt-5.2", budget: 50000, want: "xhigh", ok: true},
903
  {name: "negative-unsupported", model: "gpt-5", budget: -5, want: "", ok: false},
904
  }
 
66
  }
67
  }
68
 
69
+ var (
70
+ thinkingTestModels = []string{
71
+ "gpt-5", // level-based thinking model
72
+ "gemini-2.5-pro", // numeric-budget thinking model
73
+ "qwen3-code-plus", // no thinking support
74
+ "openai-compat", // allowCompat=true (OpenAI-compatible channel)
75
+ }
76
+ thinkingTestFromProtocols = []string{"openai", "claude", "gemini", "openai-response"}
77
+ thinkingTestToProtocols = []string{"gemini", "claude", "openai", "codex"}
78
+
79
+ // Numeric budgets and their level equivalents:
80
+ // -1 -> auto
81
+ // 0 -> none
82
+ // 1..1024 -> low
83
+ // 1025..8192 -> medium
84
+ // 8193..24576 -> high
85
+ // >24576 -> model highest level (right-most in Levels)
86
+ thinkingNumericSamples = []int{-1, 0, 1023, 1025, 8193, 64000}
87
+
88
+ // Levels and their numeric equivalents:
89
+ // auto -> -1
90
+ // none -> 0
91
+ // minimal -> 512
92
+ // low -> 1024
93
+ // medium -> 8192
94
+ // high -> 24576
95
+ // xhigh -> 32768
96
+ // invalid -> invalid (no mapping)
97
+ thinkingLevelSamples = []string{"auto", "none", "minimal", "low", "medium", "high", "xhigh", "invalid"}
98
+ )
99
+
100
  func buildRawPayload(fromProtocol, modelWithSuffix string) []byte {
101
  switch fromProtocol {
102
  case "gemini":
 
132
  if field == "" {
133
  return payload
134
  }
135
+ baseModel := util.ResolveOriginalModel(model, metadata)
136
+ if baseModel == "" {
137
+ baseModel = model
138
+ }
139
+ if !util.ModelSupportsThinking(baseModel) && !allowCompat {
140
  return payload
141
  }
142
  if effort, ok := util.ReasoningEffortFromMetadata(metadata); ok && effort != "" {
143
+ if util.ModelUsesThinkingLevels(baseModel) || allowCompat {
144
  if updated, err := sjson.SetBytes(payload, field, effort); err == nil {
145
  return updated
146
  }
147
  }
148
  }
149
+ // Fallback: numeric thinking_budget suffix for level-based (OpenAI-style) models.
150
+ if util.ModelUsesThinkingLevels(baseModel) || allowCompat {
151
  if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil {
152
+ if effort, ok := util.OpenAIThinkingBudgetToEffort(baseModel, *budget); ok && effort != "" {
153
+ if *budget == 0 && effort == "none" && util.ModelUsesThinkingLevels(baseModel) {
154
+ if _, supported := util.NormalizeReasoningEffortLevel(baseModel, effort); !supported {
155
+ return stripThinkingFieldsLocal(payload, false)
156
+ }
157
+ }
158
+
159
  if updated, err := sjson.SetBytes(payload, field, effort); err == nil {
160
  return updated
161
  }
 
362
  cleanup := registerCoreModels(t)
363
  defer cleanup()
364
 
 
 
 
 
 
 
 
 
 
365
  type scenario struct {
366
  name string
367
  modelSuffix string
 
368
  }
369
 
370
+ numericName := func(budget int) string {
371
+ if budget < 0 {
372
+ return "numeric-neg1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  }
374
+ return fmt.Sprintf("numeric-%d", budget)
375
  }
376
 
377
+ for _, model := range thinkingTestModels {
378
  _ = registry.GetGlobalRegistry().GetModelInfo(model)
379
 
380
+ for _, from := range thinkingTestFromProtocols {
381
  // Scenario selection follows protocol semantics:
382
  // - OpenAI-style protocols (openai/openai-response) express thinking as levels.
383
  // - Claude/Gemini-style protocols express thinking as numeric budgets.
384
  cases := []scenario{
385
+ {name: "no-suffix", modelSuffix: model},
386
  }
387
  if from == "openai" || from == "openai-response" {
388
+ for _, lvl := range thinkingLevelSamples {
389
+ cases = append(cases, scenario{
390
+ name: "level-" + lvl,
391
+ modelSuffix: fmt.Sprintf("%s(%s)", model, lvl),
392
+ })
393
+ }
 
 
 
 
 
 
394
  } else { // claude or gemini
395
+ for _, budget := range thinkingNumericSamples {
396
+ budget := budget
397
+ cases = append(cases, scenario{
398
+ name: numericName(budget),
399
+ modelSuffix: fmt.Sprintf("%s(%d)", model, budget),
400
+ })
401
+ }
 
 
 
402
  }
403
 
404
+ for _, to := range thinkingTestToProtocols {
405
  if from == to {
406
  continue
407
  }
 
594
  return []byte(base)
595
  case "claude":
596
  base := fmt.Sprintf(`{"model":"%s","messages":[{"role":"user","content":"hi"}]}`, model)
597
+ if budget, ok := thinkingParam.(int); ok {
598
  base, _ = sjson.Set(base, "thinking.type", "enabled")
599
  base, _ = sjson.Set(base, "thinking.budget_tokens", budget)
600
  }
 
645
  cleanup := registerCoreModels(t)
646
  defer cleanup()
647
 
 
 
 
 
 
 
 
 
 
648
  type scenario struct {
649
  name string
650
  thinkingParam any // int for budget, string for effort level
651
  }
652
 
653
+ numericName := func(budget int) string {
654
+ if budget < 0 {
655
+ return "budget-neg1"
656
+ }
657
+ return fmt.Sprintf("budget-%d", budget)
658
+ }
659
+
660
+ for _, model := range thinkingTestModels {
661
  supportsThinking := util.ModelSupportsThinking(model)
662
  usesLevels := util.ModelUsesThinkingLevels(model)
663
  allowCompat := isOpenAICompatModel(model)
664
 
665
+ for _, from := range thinkingTestFromProtocols {
666
  var cases []scenario
667
  switch from {
668
  case "openai", "openai-response":
 
669
  cases = []scenario{
670
  {name: "no-thinking", thinkingParam: nil},
671
+ }
672
+ for _, lvl := range thinkingLevelSamples {
673
+ cases = append(cases, scenario{
674
+ name: "effort-" + lvl,
675
+ thinkingParam: lvl,
676
+ })
 
 
677
  }
678
  case "gemini", "claude":
 
679
  cases = []scenario{
680
  {name: "no-thinking", thinkingParam: nil},
681
+ }
682
+ for _, budget := range thinkingNumericSamples {
683
+ budget := budget
684
+ cases = append(cases, scenario{
685
+ name: numericName(budget),
686
+ thinkingParam: budget,
687
+ })
688
  }
689
  }
690
 
691
+ for _, to := range thinkingTestToProtocols {
692
  if from == to {
693
  continue
694
  }
 
731
  // ThinkingEffortToBudget already returns normalized budget
732
  return true, fmt.Sprintf("%d", budget), false
733
  }
734
+ // Invalid effort does not map to a budget
735
+ return false, "", false
736
  }
737
  return false, "", false
738
  case "claude":
 
762
  case "openai":
763
  if allowCompat {
764
  if effort, ok := cs.thinkingParam.(string); ok && strings.TrimSpace(effort) != "" {
 
765
  normalized := strings.ToLower(strings.TrimSpace(effort))
766
+ return true, normalized, false
 
 
 
 
 
767
  }
768
  if budget, ok := cs.thinkingParam.(int); ok {
769
  if mapped, okM := util.OpenAIThinkingBudgetToEffort(model, budget); okM && mapped != "" {
 
889
  want string
890
  ok bool
891
  }{
892
+ {name: "dynamic-auto", model: "gpt-5", budget: -1, want: "auto", ok: true},
893
  {name: "zero-none", model: "gpt-5", budget: 0, want: "none", ok: true},
894
  {name: "low-min", model: "gpt-5", budget: 1, want: "low", ok: true},
895
  {name: "low-max", model: "gpt-5", budget: 1024, want: "low", ok: true},
 
897
  {name: "medium-max", model: "gpt-5", budget: 8192, want: "medium", ok: true},
898
  {name: "high-min", model: "gpt-5", budget: 8193, want: "high", ok: true},
899
  {name: "high-max", model: "gpt-5", budget: 24576, want: "high", ok: true},
900
+ {name: "over-max-clamps-to-highest", model: "gpt-5", budget: 64000, want: "high", ok: true},
901
  {name: "over-max-xhigh-model", model: "gpt-5.2", budget: 50000, want: "xhigh", ok: true},
902
  {name: "negative-unsupported", model: "gpt-5", budget: -5, want: "", ok: false},
903
  }