mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4835] [Engine] /v1/responses: tool_choice "required" and a named function force a call, as on the chat route #5205
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5205
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4835
Author: @gdevenyi
Created: 10/31/2026
Status: 🔄 Open
Base:
main← Head:api/responses-tool-choice-force📝 Commits (2)
c9d1d40tool_choice "required" with several tools: write only "<tool_call>\n", because from a written "<function=" the model went on with names no tool has and the reply came back as textef090c7/v1/responses: tool_choice "required" and a named function force a call, as on the chat and Anthropic routes (#790's follow-up); anything it cannot honour stays auto, with a log line📊 Changes
7 files changed (+226 additions, -21 deletions)
View changed files
📝
docs/DETAILS.md(+1 -1)📝
serve/frontend.py(+2 -2)📝
serve/server.py(+8 -7)📝
serve/test_dsv41.py(+12 -0)📝
serve/test_frontend.py(+1 -1)📝
serve/test_responses.py(+195 -3)📝
serve/test_server.py(+7 -7)📄 Description
Stacked on #4828. Please merge that one first. Without it,
"required"with two or more tools gets worse here: on the real engine, 20/20 calls on the Paris prompt became 0/20, because of the opening that PR fixes. The diff of this PR is its second commit.Problem and result
On
/v1/responses,tool_choice: "required"and{"type": "function", "name": N}were echoed back but did nothing. The prompt had no forced opening, so the model could answer in plain text, and the reply was a 200 with statuscompletedand amessage. The same choices on/v1/chat/completionsand/v1/messagesdo write the opening.The OpenAI spec (openapi.yaml,
ToolChoiceOptions) says: "requiredmeans the model must call one or more tools." ForToolChoiceFunctionit says: "Use this option to force the model to call a specific function."Now both choices write the call's opening on this route too, after the thinking or at the end of the prompt when thinking is off. On the real engine, a named
get_weatheron a prompt about the time was ignored on the base (10/10get_time) and is honoured now (10/10get_weather). "Say hi." with"required"went from 0/15 calls to 15/15. There is no grammar, so what the model writes after the opening is not checked. See Limits.Three other behaviours change, all only for a forced choice or one the server cannot honour:
text.formatwith a forced choice, and a reply that goes on from the opening. On the base the prompt had no opening, so a reply that wrote plain text had to pass the format. Now it completes the call: 200. A whole call written by the model was already a 200 on the base.text.formatwith a named choice, and prose after the opening. The base gave 502. Now it is a 200 with an unfinished call (first item under Limits).tool_choicethe server cannot honour now printsforced_call's log line on this route. The base printed nothing. It still acts asauto, with a 200.Related discussion
tool_choicecan reuseforced_call()in a follow-up." This PR is that follow-up for Responses.Service._runand adds a grammar to Responses. It does not add force to this route. A merge conflict is possible; it is not a duplicate.#4828: this PR needs it; see the top. The other thread notes come from an earlier search; I did not read them again for this revision. I am an outside contributor, and nobody on the project has agreed the scope. I opened no separate issue because the change is small and reuses merged code.
Approach
The Responses route now does what the other two routes do: it computes
forcewithforced_callandforce_text, and passes it tosvc.prepare(both tries) and tosvc.run.docs/DETAILS.mdsaid the choice "cannot be forced", so that row is updated.Requests with
auto,noneor notool_choiceget the same prompt and the same reply as before (the comparison is below). Nodeclared_onlychange.Validation
Revision:
ef090c70(c9d1d402, the PR above, plus this change) on basec62d5e3a. Linux, Python 3.12,PYTHONDONTWRITEBYTECODE=1. The test lists below use a mock engine unless they say real engine.Real engine: RTX 5080 16 GB, engine 0.1.55 from release v0.2.9, Qwen3.8-Flash-Next IQ3_S, context 65536,
openaiSDK 3.42.0, toolsget_timeandget_weather. Thinking off (reasoning.effort: none) unless noted.Passed:
Real engine,
ef090c70againstc62d5e3a, the same script on both sides. Calls, offered tools only:"required", "What time is it in Paris right now?", wholeget_timeget_time"required", the Paris prompt, streamedget_timeget_time"required", "Say hi.", whole"required", "Say hi.", streamed"required", thinking on, the Paris prompt, wholeget_timeget_time"required", thinking on, "Say hi.", wholeget_weather, the Paris prompt, wholeget_timeget_weatherget_weather, the Paris prompt, streamedget_timeget_weatherget_weather, "Say hi.", wholeget_weatherget_weather, "Say hi.", streamedget_weather"required",json_object, the Paris prompt, whole"required",json_object, "Say hi.", wholemessageStreamed runs used
responses.create, with default temperature; whole runs used plain HTTP at temperature 0.7. On this PR: streamed item statuscompletedand arguments parse as JSON, and the server's "did not read as a call of a declared tool" line appeared 0 times.Real engine, on the earlier revision 7be9a546, before it was stacked on the PR above. The named path is the same code:
"required", one tool, the Paris prompt: 10/10 calls on both sides.autoandnone, amessage(base 1 whole run each, branch 1 whole and 1 streamed each). Notool_choice, amessage(branch only, 1 whole run).auto, the Paris prompt: 10/10 calls toget_timeon both sides.text.formatwith a named choice, "Say hi.", 1 whole and 1 streamed run. Base: 200 with JSON in amessage, no call. Branch: 200 with a completed call.python -m unittest serve.test_responses.ForcedToolChoice serve.test_dsv41.Api.test_forced_tool_choice_on_responses: 13 tests, OK. The tests also assert the log line, and pin the outcomes listed under Limits.The same tests on the base, to see them fail. Copy the two test files onto an export of
c62d5e3aand run them there:serve.test_responses.ForcedToolChoice: failures=28 counting subtests.serve.test_dsv41.Api.test_forced_tool_choice_on_responses: failures=2 counting subtests.python -m unittest serve.test_server serve.test_responses serve.test_anthropic_base_url serve.test_chat_tool_results serve.test_context_overflow serve.test_images serve.test_dsv41: Ran 514 tests, failures=1, skipped=2. The one failure is listed below.On 7be9a546, with a mock engine and local scripts that are not in this PR:
/v1/responses(26),/v1/chat/completions(6) and/v1/messages(2), 11 of them streamed. They cover notool_choice,auto,none, an unmatched name,allowed_toolsin modeauto,requiredwith no tools, andrequiredon chat. With ids, timestamps and timings normalized, the status, the wire body and a hash of the prompt were byte-identical on all 34. Not re-run onef090c70; the PR above changes therequiredrows on purpose.openaiPython SDK 3.42.0, named and required, whole andresponses.stream: statuscompleted, afunction_call, and nooutput_textdelta in the stream.Failed:
serve.test_server.GpuChoice.test_hip_ordinal:AssertionError: '0,1' != '1,0'. It fails the same way on an export ofc62d5e3a. Nothing here touches it.Not run:
On real hardware:
"required"with thinking on when streamed.json_objectwith"required"when streamed.custom_tool_call, and the log line for a choice the server cannot honour.responses.parseandresponses.stream.Elsewhere:
Limits
The opening is all the server writes. None of these three was seen in the real runs above. Their rates are not measured.
completed, afunction_callwith item statusincompleteand arguments{. The prose is dropped. The same reply on chat givescontent: nulland notool_calls. RealopenaiSDK 3.42.0,responses.parseandresponses.streamwith a strict tool: both raiseJSONDecodeErroron it. A model that writes this opening itself got the same shape on the base. I did not change it here."required", and the model writes prose or a name the request never offered: amessage, not a call. Withtext.formatset, that is a 502structured_output_failed.completed, reasoning only. Chat also gives no call here (content: null,finish_reason: stop).These choices stay as
auto, with a log line:tool_choiceof typeallowed_tools. It is not trivial: filtering changes the rendered prompt, and moderequiredneeds its own mapping.{"type": "custom", "name": N}, whichtool_choice_ofdoes not know."required"does reach custom tools.read). The fullmcp__fs.readworks."required"when the request has no tool or only hosted tools.🤖 Generated with Claude Code
https://claude.ai/code/session_01W53iURzvDQMjnYemag8uvv
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.