[PR #4835] [Engine] /v1/responses: tool_choice "required" and a named function force a call, as on the chat route #5205

Open
opened 2026-11-07 16:33:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • c9d1d40 tool_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 text
  • ef090c7 /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 status completed and a message. The same choices on /v1/chat/completions and /v1/messages do write the opening.

The OpenAI spec (openapi.yaml, ToolChoiceOptions) says: "required means the model must call one or more tools." For ToolChoiceFunction it 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_weather on a prompt about the time was ignored on the base (10/10 get_time) and is honoured now (10/10 get_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.format with 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.format with 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).
  • A tool_choice the server cannot honour now prints forced_call's log line on this route. The base printed nothing. It still acts as auto, with a 200.
  • #790 (closed): forcing on chat. Its body says "The Responses route and Anthropic tool_choice can reuse forced_call() in a follow-up." This PR is that follow-up for Responses.
  • #4222 (open): touches the force handling in Service._run and adds a grammar to Responses. It does not add force to this route. A merge conflict is possible; it is not a duplicate.
  • #4700 (open): changes the force calls on the Anthropic route only. 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 force with forced_call and force_text, and passes it to svc.prepare (both tries) and to svc.run. docs/DETAILS.md said the choice "cannot be forced", so that row is updated.

Requests with auto, none or no tool_choice get the same prompt and the same reply as before (the comparison is below). No declared_only change.

Validation

Revision: ef090c70 (c9d1d402, the PR above, plus this change) on base c62d5e3a. 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, openai SDK 3.42.0, tools get_time and get_weather. Thinking off (reasoning.effort: none) unless noted.

Passed:

  • Real engine, ef090c70 against c62d5e3a, the same script on both sides. Calls, offered tools only:

    Request Base This PR
    "required", "What time is it in Paris right now?", whole 10/10, get_time 10/10, get_time
    "required", the Paris prompt, streamed 5/5, get_time 5/5, get_time
    "required", "Say hi.", whole 0/10 10/10
    "required", "Say hi.", streamed 0/5 5/5
    "required", thinking on, the Paris prompt, whole 5/5, get_time 5/5, get_time
    "required", thinking on, "Say hi.", whole 0/5 5/5
    Named get_weather, the Paris prompt, whole 5/5, get_time 5/5, get_weather
    Named get_weather, the Paris prompt, streamed 5/5, get_time 5/5, get_weather
    Named get_weather, "Say hi.", whole 0/5 5/5, get_weather
    Named get_weather, "Say hi.", streamed 0/5 5/5, get_weather
    "required", json_object, the Paris prompt, whole 3/3 3/3
    "required", json_object, "Say hi.", whole 0/3: one 502, two 200 with a message 3/3

    Streamed runs used responses.create, with default temperature; whole runs used plain HTTP at temperature 0.7. On this PR: streamed item status completed and 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.
    • Guards on "Say hi.": auto and none, a message (base 1 whole run each, branch 1 whole and 1 streamed each). No tool_choice, a message (branch only, 1 whole run).
    • auto, the Paris prompt: 10/10 calls to get_time on both sides.
    • text.format with a named choice, "Say hi.", 1 whole and 1 streamed run. Base: 200 with JSON in a message, 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 c62d5e3a and 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:

    • Comparison of base and branch: 34 request and script pairs on /v1/responses (26), /v1/chat/completions (6) and /v1/messages (2), 11 of them streamed. They cover no tool_choice, auto, none, an unmatched name, allowed_tools in mode auto, required with no tools, and required on 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 on ef090c70; the PR above changes the required rows on purpose.
    • Real openai Python SDK 3.42.0, named and required, whole and responses.stream: status completed, a function_call, and no output_text delta in the stream.

Failed:

  • serve.test_server.GpuChoice.test_hip_ordinal: AssertionError: '0,1' != '1,0'. It fails the same way on an export of c62d5e3a. Nothing here touches it.

Not run:

On real hardware:

  • Any other model, quantization or card. DeepSeek V4.1-Flash and Qwen3.6 were not run.
  • A named choice with thinking on, and "required" with thinking on when streamed.
  • json_object with "required" when streamed.
  • The named-choice reply that writes prose or stops after the opening. It did not happen in the named runs, and forcing it would need a patch.
  • A reply that ends inside its thinking.
  • custom_tool_call, and the log line for a choice the server cannot honour.
  • The SDK helpers responses.parse and responses.stream.

Elsewhere:

  • Windows and macOS.
  • HIP, SYCL, CUDA and Metal builds. No build, because the change is Python only.
  • The OpenAI Agents SDK, the client most likely to send these choices on this route, and Codex.
  • The full test set on the base. There I ran only the new tests and the failing test.

Limits

The opening is all the server writes. None of these three was seen in the real runs above. Their rates are not measured.

  • A named choice, and the model writes prose or stops after the opening: 200, status completed, a function_call with item status incomplete and arguments {. The prose is dropped. The same reply on chat gives content: null and no tool_calls. Real openai SDK 3.42.0, responses.parse and responses.stream with a strict tool: both raise JSONDecodeError on 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: a message, not a call. With text.format set, that is a 502 structured_output_failed.
  • A reply that ends inside its thinking gets no call: 200, status completed, reasoning only. Chat also gives no call here (content: null, finish_reason: stop).

These choices stay as auto, with a log line:

  • tool_choice of type allowed_tools. It is not trivial: filtering changes the rendered prompt, and mode required needs its own mapping.
  • {"type": "custom", "name": N}, which tool_choice_of does not know. "required" does reach custom tools.
  • A bare name for a namespaced tool (read). The full mcp__fs.read works.
  • "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.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4835 **Author:** [@gdevenyi](https://github.com/gdevenyi) **Created:** 10/31/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `api/responses-tool-choice-force` --- ### 📝 Commits (2) - [`c9d1d40`](https://github.com/Niko1221/Strata/commit/c9d1d402993c731cf2c38d341bc71d024d7c60b9) tool_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 text - [`ef090c7`](https://github.com/Niko1221/Strata/commit/ef090c703cf8b6ecfedef944e45ea00682b11085) /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) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 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 status `completed` and a `message`. The same choices on `/v1/chat/completions` and `/v1/messages` do write the opening. The OpenAI spec (openapi.yaml, `ToolChoiceOptions`) says: "`required` means the model must call one or more tools." For `ToolChoiceFunction` it 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_weather` on a prompt about the time was ignored on the base (10/10 `get_time`) and is honoured now (10/10 `get_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.format` with 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.format` with 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). - A `tool_choice` the server cannot honour now prints `forced_call`'s log line on this route. The base printed nothing. It still acts as `auto`, with a 200. ## Related discussion - #790 (closed): forcing on chat. Its body says "The Responses route and Anthropic `tool_choice` can reuse `forced_call()` in a follow-up." This PR is that follow-up for Responses. - #4222 (open): touches the force handling in `Service._run` and adds a grammar to Responses. It does not add force to this route. A merge conflict is possible; it is not a duplicate. - #4700 (open): changes the force calls on the Anthropic route only. 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 `force` with `forced_call` and `force_text`, and passes it to `svc.prepare` (both tries) and to `svc.run`. `docs/DETAILS.md` said the choice "cannot be forced", so that row is updated. Requests with `auto`, `none` or no `tool_choice` get the same prompt and the same reply as before (the comparison is below). No `declared_only` change. ## Validation Revision: ef090c70 (c9d1d402, the PR above, plus this change) on base c62d5e3a. 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, `openai` SDK 3.42.0, tools `get_time` and `get_weather`. Thinking off (`reasoning.effort: none`) unless noted. **Passed:** - Real engine, ef090c70 against c62d5e3a, the same script on both sides. Calls, offered tools only: | Request | Base | This PR | | --- | --- | --- | | `"required"`, "What time is it in Paris right now?", whole | 10/10, `get_time` | 10/10, `get_time` | | `"required"`, the Paris prompt, streamed | 5/5, `get_time` | 5/5, `get_time` | | `"required"`, "Say hi.", whole | 0/10 | 10/10 | | `"required"`, "Say hi.", streamed | 0/5 | 5/5 | | `"required"`, thinking on, the Paris prompt, whole | 5/5, `get_time` | 5/5, `get_time` | | `"required"`, thinking on, "Say hi.", whole | 0/5 | 5/5 | | Named `get_weather`, the Paris prompt, whole | 5/5, **`get_time`** | 5/5, `get_weather` | | Named `get_weather`, the Paris prompt, streamed | 5/5, **`get_time`** | 5/5, `get_weather` | | Named `get_weather`, "Say hi.", whole | 0/5 | 5/5, `get_weather` | | Named `get_weather`, "Say hi.", streamed | 0/5 | 5/5, `get_weather` | | `"required"`, `json_object`, the Paris prompt, whole | 3/3 | 3/3 | | `"required"`, `json_object`, "Say hi.", whole | 0/3: one 502, two 200 with a `message` | 3/3 | Streamed runs used `responses.create`, with default temperature; whole runs used plain HTTP at temperature 0.7. On this PR: streamed item status `completed` and 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. - Guards on "Say hi.": `auto` and `none`, a `message` (base 1 whole run each, branch 1 whole and 1 streamed each). No `tool_choice`, a `message` (branch only, 1 whole run). - `auto`, the Paris prompt: 10/10 calls to `get_time` on both sides. - `text.format` with a named choice, "Say hi.", 1 whole and 1 streamed run. Base: 200 with JSON in a `message`, 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 c62d5e3a and 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: - Comparison of base and branch: 34 request and script pairs on `/v1/responses` (26), `/v1/chat/completions` (6) and `/v1/messages` (2), 11 of them streamed. They cover no `tool_choice`, `auto`, `none`, an unmatched name, `allowed_tools` in mode `auto`, `required` with no tools, and `required` on 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 on ef090c70; the PR above changes the `required` rows on purpose. - Real `openai` Python SDK 3.42.0, named and required, whole and `responses.stream`: status `completed`, a `function_call`, and no `output_text` delta in the stream. **Failed:** - `serve.test_server.GpuChoice.test_hip_ordinal`: `AssertionError: '0,1' != '1,0'`. It fails the same way on an export of c62d5e3a. Nothing here touches it. **Not run:** On real hardware: - Any other model, quantization or card. DeepSeek V4.1-Flash and Qwen3.6 were not run. - A named choice with thinking on, and `"required"` with thinking on when streamed. - `json_object` with `"required"` when streamed. - The named-choice reply that writes prose or stops after the opening. It did not happen in the named runs, and forcing it would need a patch. - A reply that ends inside its thinking. - `custom_tool_call`, and the log line for a choice the server cannot honour. - The SDK helpers `responses.parse` and `responses.stream`. Elsewhere: - Windows and macOS. - HIP, SYCL, CUDA and Metal builds. No build, because the change is Python only. - The OpenAI Agents SDK, the client most likely to send these choices on this route, and Codex. - The full test set on the base. There I ran only the new tests and the failing test. ## Limits The opening is all the server writes. None of these three was seen in the real runs above. Their rates are not measured. - A named choice, and the model writes prose or stops after the opening: 200, status `completed`, a `function_call` with item status `incomplete` and arguments `{`. The prose is dropped. The same reply on chat gives `content: null` and no `tool_calls`. Real `openai` SDK 3.42.0, `responses.parse` and `responses.stream` with a strict tool: both raise `JSONDecodeError` on 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: a `message`, not a call. With `text.format` set, that is a 502 `structured_output_failed`. - A reply that ends inside its thinking gets no call: 200, status `completed`, reasoning only. Chat also gives no call here (`content: null`, `finish_reason: stop`). These choices stay as `auto`, with a log line: - `tool_choice` of type `allowed_tools`. It is not trivial: filtering changes the rendered prompt, and mode `required` needs its own mapping. - `{"type": "custom", "name": N}`, which `tool_choice_of` does not know. `"required"` does reach custom tools. - A bare name for a namespaced tool (`read`). The full `mcp__fs.read` works. - `"required"` when the request has no tool or only hosted tools. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01W53iURzvDQMjnYemag8uvv --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5205
No description provided.