[PR #231] [CLOSED] fix(serve): report a tool call cut off by the end of the output as unfinished #2517

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/231
Author: @alphastorm
Created: 9/30/2026
Status: ❌ Closed

Base: main ← Head: fix/211-unfinished-tool-call


📝 Commits (1)

  • 5325ba2 fix(serve): report a tool call cut off by the end of the output as unfinished

📊 Changes

4 files changed (+115 additions, -8 deletions)

View changed files

📝 serve/frontend.py (+4 -3)
📝 serve/server.py (+12 -5)
📝 serve/test_mcp.py (+13 -0)
📝 serve/test_server.py (+86 -0)

📄 Description

Fixes #211 (the second trigger of #210: the output ends inside a call).

When the model's output ended inside a tool call whose name had already streamed, OutputParser.finish() closed its JSON with _close_scan() and emitted tool_call, and the answer ended with finish_reason: "tool_calls" / stop_reason: "tool_use". A client could not tell the call was cut and ran it with truncated arguments. In the web app's chat, an unfinished MCP call ran with empty arguments.

Change

  • frontend.py, finish(): an announced call that never reached </function> stays unfinished. Its JSON is not closed and no tool_call follows its tool_start. A call missing only its </tool_call> is still whole.
  • server.py: the OpenAI and Anthropic streams say "tool_calls" / "tool_use" only when every streamed call got its tool_call; otherwise the engine's reason stands ("stop" / "end_turn"). anthropic_collect leaves out a tool_use block whose input never closed, since it has no input to give. The MCP loop needs no change: the call is not run, and the web app already shows a started call as "Not run" when the answer ends.
  • A cut at the token limit also keeps its arguments open now. Its finish reason is still "length" / "max_tokens"; the non-streamed Anthropic answer leaves out the cut tool_use block, where it used to carry the truncated input.

Tests (python -m unittest serve.test_server serve.test_mcp serve.test_detok)

  • UnfinishedToolCall: the parser (fed 1, 7 and all characters at a time), and the four API shapes for a cut call and, as a control, a whole call.
  • ToolLoop.test_a_call_the_output_ends_inside_is_not_run.

On bbaaabb (0.1.28) the new tests fail (5 failures: the cut-call cases and the MCP case; the whole-call control passes). With the change the suite passes (72 tests, 3 skipped) on Python 3.13.15 and 3.14.7, macOS arm64 with the mock engine. No GPU or real model was run, and the web app was not opened.


🔄 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/231 **Author:** [@alphastorm](https://github.com/alphastorm) **Created:** 9/30/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/211-unfinished-tool-call` --- ### 📝 Commits (1) - [`5325ba2`](https://github.com/Niko1221/Strata/commit/5325ba2efecf0362c1622110bbd684447ad5bfe2) fix(serve): report a tool call cut off by the end of the output as unfinished ### 📊 Changes **4 files changed** (+115 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+4 -3) 📝 `serve/server.py` (+12 -5) 📝 `serve/test_mcp.py` (+13 -0) 📝 `serve/test_server.py` (+86 -0) </details> ### 📄 Description Fixes #211 (the second trigger of #210: the output ends inside a call). When the model's output ended inside a tool call whose name had already streamed, `OutputParser.finish()` closed its JSON with `_close_scan()` and emitted `tool_call`, and the answer ended with `finish_reason: "tool_calls"` / `stop_reason: "tool_use"`. A client could not tell the call was cut and ran it with truncated arguments. In the web app's chat, an unfinished MCP call ran with empty arguments. **Change** - `frontend.py`, `finish()`: an announced call that never reached `</function>` stays unfinished. Its JSON is not closed and no `tool_call` follows its `tool_start`. A call missing only its `</tool_call>` is still whole. - `server.py`: the OpenAI and Anthropic streams say `"tool_calls"` / `"tool_use"` only when every streamed call got its `tool_call`; otherwise the engine's reason stands (`"stop"` / `"end_turn"`). `anthropic_collect` leaves out a `tool_use` block whose input never closed, since it has no input to give. The MCP loop needs no change: the call is not run, and the web app already shows a started call as "Not run" when the answer ends. - A cut at the token limit also keeps its arguments open now. Its finish reason is still `"length"` / `"max_tokens"`; the non-streamed Anthropic answer leaves out the cut `tool_use` block, where it used to carry the truncated input. **Tests** (`python -m unittest serve.test_server serve.test_mcp serve.test_detok`) - `UnfinishedToolCall`: the parser (fed 1, 7 and all characters at a time), and the four API shapes for a cut call and, as a control, a whole call. - `ToolLoop.test_a_call_the_output_ends_inside_is_not_run`. On `bbaaabb` (0.1.28) the new tests fail (5 failures: the cut-call cases and the MCP case; the whole-call control passes). With the change the suite passes (72 tests, 3 skipped) on Python 3.13.15 and 3.14.7, macOS arm64 with the mock engine. No GPU or real model was run, and the web app was not opened. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:00:54 +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#2517
No description provided.