Skip to content

fix(middleware): repair tool_choice string mode + legacy flat shape for /v1/chat/completions#9859

Open
Dennisadira wants to merge 1 commit into
mudler:masterfrom
Dennisadira:fix/chat-completions-tool-choice
Open

fix(middleware): repair tool_choice string mode + legacy flat shape for /v1/chat/completions#9859
Dennisadira wants to merge 1 commit into
mudler:masterfrom
Dennisadira:fix/chat-completions-tool-choice

Conversation

@Dennisadira
Copy link
Copy Markdown
Contributor

Summary

Fixes #9508 — the remaining two bugs in the ToolsChoice block of mergeOpenAIRequestAndModelConfig (the /v1/chat/completions path). The equivalent fix for /v1/responses landed in #9509; the three other call sites (anthropic, realtime, openresponses) were already corrected in a prior commit.

Bug 1 — string mode silently dropped:
json.Unmarshal([]byte("required"), &toolChoice) always fails (bare string is not valid JSON). The code then unconditionally set FunctionCall to {"name":""}, so SetFunctionCallNameString("") ran instead of SetFunctionCallString("required"). Result: tool_choice: "required" had no effect; grammar-based forcing never engaged.

Bug 2 — legacy flat shape {type:function, name:...} not handled:
functions.Tool has no top-level Name field, so unmarshaling {"type":"function","name":"get_weather"} left Function.Name empty. The specific-function name was silently dropped.

Fix

Mirrors MergeOpenResponsesConfig (from #9509):

  • String case: route "required" and "none" through as strings so the downstream FunctionCall switch calls SetFunctionCallString; leave "auto" as a no-op (it's the default and must not populate FunctionToCall()).
  • Map case: walk the nested OpenAI shape function.name first, fall back to the flat top-level name, only set FunctionCall when a non-empty name is resolved.

Also exports mergeOpenAIRequestAndModelConfigMergeOpenAIRequestConfig (same pattern as MergeOpenResponsesConfig) to allow direct unit testing.

Test plan

  • 11 new Ginkgo specs under MergeOpenAIRequestConfig tool_choice parsing:
    • string modes: "required" (ShouldUseFunctions=true), "none" (ShouldUseFunctions=false), "auto" (no-op)
    • OpenAI nested shape → ShouldCallSpecificFunction=true, FunctionToCall="get_weather"
    • nested shape prefers function.name over stray top-level name
    • legacy flat shape → ShouldCallSpecificFunction=true, FunctionToCall="get_weather"
    • malformed: missing type, wrong type, missing name, empty name → all no-ops
    • nil → no-op
  • go test ./core/http/middleware/... -count=1 → 39/39 ✓

Assisted-by: Claude:claude-sonnet-4-6

…or /v1/chat/completions

The ToolsChoice block in mergeOpenAIRequestAndModelConfig had two bugs:

1. String mode ("required", "none"): json.Unmarshal([]byte("required"), &tool)
   always fails, leaving toolChoice zero-valued. The code then unconditionally
   set FunctionCall to {"name":""}, so SetFunctionCallNameString("") ran instead
   of SetFunctionCallString("required") — the mode was silently dropped.

2. Legacy flat tool_choice shape {"type":"function","name":"..."}: functions.Tool
   has no top-level Name field, so json.Unmarshal produced an empty Function.Name,
   and the specific-function name was never forwarded.

Fix mirrors the approach from MergeOpenResponsesConfig (landed in mudler#9509):
- String case: pass "required"/"none" through as a string so the downstream
  FunctionCall switch routes to SetFunctionCallString; leave "auto" as a no-op.
- Map case: walk the nested OpenAI shape first, fall back to the flat shape,
  only set FunctionCall when a non-empty name is found.

Also exports mergeOpenAIRequestAndModelConfig as MergeOpenAIRequestConfig so it
can be tested directly (same pattern as MergeOpenResponsesConfig).

Adds 11 Ginkgo specs covering all modes, both map shapes, malformed inputs, and nil.

Closes mudler#9508.

Assisted-by: Claude:claude-sonnet-4-6
Signed-off-by: Adira Denis Muhando <dennisadira@gmail.com>
@Dennisadira Dennisadira force-pushed the fix/chat-completions-tool-choice branch from fe74fe1 to d9f5eaf Compare May 17, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tool_choice parsing: wrong setter used in 4 sites; /v1/chat/completions silently drops string mode

2 participants