[PR #790] [CLOSED] serve: honour OpenAI tool_choice "required" and a named function #2831

Closed
opened 2026-11-04 02:03:26 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/790
Author: @aguindehi
Created: 10/4/2026
Status: ❌ Closed

Base: main ← Head: serve-tool-choice


📝 Commits (1)

  • 7d67a0b serve: honour OpenAI tool_choice "required" and a named function

📊 Changes

3 files changed (+214 additions, -16 deletions)

View changed files

📝 serve/frontend.py (+21 -0)
📝 serve/server.py (+48 -16)
📝 serve/test_server.py (+145 -0)

📄 Description

What

/v1/chat/completions now honours a forced tool_choice:

  • "required": the reply is a call to one of the request's tools.
  • {"type": "function", "function": {"name": N}}: the reply is a call to N.

Strata has no grammar decoder, so the server writes the call's opening itself:
<tool_call>\n<function= (plus N>\n for a named choice). The model can then
only continue with a call. Where the opening goes:

  • thinking off: at the end of the prompt;
  • thinking on: where the thinking ends, at a clean point (no tag held back, no
    character split across tokens), the same way the reasoning_budget_tokens
    wrap-up continues from the prefix the engine already holds;
  • budget wrap-up: right after REASONING_WRAP_UP.

"auto" and no tool_choice are unchanged. "none" offers no tools, as the
Responses route already does. An unknown name, a forced choice without tools,
other values, and a forced choice together with MCP tools are a 400 instead of
being silently ignored.

Why

Agents force a tool on specific turns. Hindsight's reflect agent, for example,
sends tool_choice: "required" on its first turns and on its done call.
Until now Strata ignored tool_choice, so those turns could come back as text.

Limits

  • Only the call is forced. The arguments are not constrained (the same level
    as response_format today).
  • A reply cut by max_tokens stays finish_reason: "length" without
    tool_calls, as before.
  • Chat Completions only. The Responses route and Anthropic tool_choice can
    reuse forced_call() in a follow-up.

Tests

serve/test_server.py gets ForcedToolChoice (6 tests, with MockEngine and
ByteTokenizer): "required" and named, thinking on and off, streamed and
whole replies, budget wrap-up, "auto"/absent/"none", a call cut by max
tokens, and the refused values. python -m pytest serve passes: 267 passed,
7 skipped.

The same change, on v0.1.38 (99f3dbd), has been deployed behind LiteLLM
since 2026-10-03 for Hindsight's forced reflect turns.

🤖 Generated with Claude Code

https://claude.ai/code/session_013Dd7NbnghR6cgQdQ2rTkUZ


🔄 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/790 **Author:** [@aguindehi](https://github.com/aguindehi) **Created:** 10/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `serve-tool-choice` --- ### 📝 Commits (1) - [`7d67a0b`](https://github.com/Niko1221/Strata/commit/7d67a0b129b26e824b54cb4aec90e70245d1e641) serve: honour OpenAI tool_choice "required" and a named function ### 📊 Changes **3 files changed** (+214 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+21 -0) 📝 `serve/server.py` (+48 -16) 📝 `serve/test_server.py` (+145 -0) </details> ### 📄 Description ## What `/v1/chat/completions` now honours a forced `tool_choice`: - `"required"`: the reply is a call to one of the request's tools. - `{"type": "function", "function": {"name": N}}`: the reply is a call to `N`. Strata has no grammar decoder, so the server writes the call's opening itself: `<tool_call>\n<function=` (plus `N>\n` for a named choice). The model can then only continue with a call. Where the opening goes: - thinking off: at the end of the prompt; - thinking on: where the thinking ends, at a clean point (no tag held back, no character split across tokens), the same way the `reasoning_budget_tokens` wrap-up continues from the prefix the engine already holds; - budget wrap-up: right after `REASONING_WRAP_UP`. `"auto"` and no `tool_choice` are unchanged. `"none"` offers no tools, as the Responses route already does. An unknown name, a forced choice without tools, other values, and a forced choice together with MCP tools are a 400 instead of being silently ignored. ## Why Agents force a tool on specific turns. Hindsight's reflect agent, for example, sends `tool_choice: "required"` on its first turns and on its `done` call. Until now Strata ignored `tool_choice`, so those turns could come back as text. ## Limits - Only the call is forced. The arguments are not constrained (the same level as `response_format` today). - A reply cut by `max_tokens` stays `finish_reason: "length"` without `tool_calls`, as before. - Chat Completions only. The Responses route and Anthropic `tool_choice` can reuse `forced_call()` in a follow-up. ## Tests `serve/test_server.py` gets `ForcedToolChoice` (6 tests, with `MockEngine` and `ByteTokenizer`): `"required"` and named, thinking on and off, streamed and whole replies, budget wrap-up, `"auto"`/absent/`"none"`, a call cut by max tokens, and the refused values. `python -m pytest serve` passes: 267 passed, 7 skipped. The same change, on `v0.1.38` (`99f3dbd`), has been deployed behind LiteLLM since 2026-10-03 for Hindsight's forced reflect turns. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_013Dd7NbnghR6cgQdQ2rTkUZ --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:03:26 +07:00
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#2831
No description provided.