fix(middleware): repair tool_choice string mode + legacy flat shape for /v1/chat/completions#9859
Open
Dennisadira wants to merge 1 commit into
Open
Conversation
…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>
fe74fe1 to
d9f5eaf
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #9508 — the remaining two bugs in the
ToolsChoiceblock ofmergeOpenAIRequestAndModelConfig(the/v1/chat/completionspath). The equivalent fix for/v1/responseslanded 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 setFunctionCallto{"name":""}, soSetFunctionCallNameString("")ran instead ofSetFunctionCallString("required"). Result:tool_choice: "required"had no effect; grammar-based forcing never engaged.Bug 2 — legacy flat shape
{type:function, name:...}not handled:functions.Toolhas no top-levelNamefield, so unmarshaling{"type":"function","name":"get_weather"}leftFunction.Nameempty. The specific-function name was silently dropped.Fix
Mirrors
MergeOpenResponsesConfig(from #9509):"required"and"none"through as strings so the downstreamFunctionCallswitch callsSetFunctionCallString; leave"auto"as a no-op (it's the default and must not populateFunctionToCall()).function.namefirst, fall back to the flat top-levelname, only setFunctionCallwhen a non-empty name is resolved.Also exports
mergeOpenAIRequestAndModelConfig→MergeOpenAIRequestConfig(same pattern asMergeOpenResponsesConfig) to allow direct unit testing.Test plan
MergeOpenAIRequestConfig tool_choice parsing:"required"(ShouldUseFunctions=true),"none"(ShouldUseFunctions=false),"auto"(no-op)go test ./core/http/middleware/... -count=1→ 39/39 ✓Assisted-by: Claude:claude-sonnet-4-6