[GH-ISSUE #211] A tool call cut off by the end of the turn is returned as complete (closed JSON, finish_reason "tool_calls") #109

Closed
opened 2026-11-04 01:40:02 +07:00 by bsakaguchi · 5 comments
Owner

Originally created by @alphastorm on GitHub (Sep 30, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/211

When the model ends its turn in the middle of a tool call (the call's name has already streamed, but </parameter> / </function> never come), the server returns the call as if it were complete:

  • OutputParser.finish() (serve/frontend.py#L473-L482) handles "the output ended inside a call that was already announced" by calling _close_scan() (#L389-L402), which closes the partial JSON so that "clients can still parse it", and then emits a tool_call event;
  • the finish reason becomes tool_calls (serve/server.py#L1087).

The arguments are valid JSON and the finish reason says the call is complete, so a client cannot tell that it was cut and runs the tool with truncated arguments. We hit this with an agent client: it ran a write call whose content ended mid-word and saved the partial file.

A cut at the token limit is reported correctly (finish_reason: "length").

Reproduce (v0.1.27, a790805, mock engine, no GPU)

python serve/server.py --engine mock --port 18096 --script $'</think>\n\n<tool_call>\n<function=write>\n<parameter=path>notes.txt</parameter>\n<parameter=content>first half of the fi'
curl -s http://127.0.0.1:18096/v1/chat/completions -H 'Content-Type: application/json' -d '{"model": "x", "max_tokens": 1024,
  "messages": [{"role": "user", "content": "save my notes"}],
  "tools": [{"type": "function", "function": {"name": "write", "parameters": {"type": "object",
    "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}}}]}'

choices[0] of the answer:

{"index": 0, "message": {"role": "assistant", "content": null, "tool_calls": [{"id": "call_3136388e273447dbaec5bcf0", "type": "function", "function": {"name": "write", "arguments": "{\"path\":\"notes.txt\",\"content\":\"first half of the fi\"}"}}]}, "finish_reason": "tool_calls"}

With "stream": true the streamed arguments add up to the same JSON, and the stream ends with finish_reason: "tool_calls".

Expected

A call the model did not finish should not be reported as a finished one, so that clients can drop it instead of running it: for example, leave its arguments unclosed and end with a finish reason other than tool_calls, as a token-limit cut does. (finish()'s docstring says "an unterminated tool call is returned as content"; today that happens only when the cut comes before the call's name was streamed.)

Originally created by @alphastorm on GitHub (Sep 30, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/211 When the model ends its turn in the middle of a tool call (the call's name has already streamed, but `</parameter>` / `</function>` never come), the server returns the call as if it were complete: - `OutputParser.finish()` ([serve/frontend.py#L473-L482](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/frontend.py#L473-L482)) handles "the output ended inside a call that was already announced" by calling `_close_scan()` ([#L389-L402](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/frontend.py#L389-L402)), which closes the partial JSON so that "clients can still parse it", and then emits a `tool_call` event; - the finish reason becomes `tool_calls` ([serve/server.py#L1087](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1087)). The arguments are valid JSON and the finish reason says the call is complete, so a client cannot tell that it was cut and runs the tool with truncated arguments. We hit this with an agent client: it ran a `write` call whose `content` ended mid-word and saved the partial file. A cut at the token limit is reported correctly (`finish_reason: "length"`). ### Reproduce (v0.1.27, `a790805`, mock engine, no GPU) ```sh python serve/server.py --engine mock --port 18096 --script $'</think>\n\n<tool_call>\n<function=write>\n<parameter=path>notes.txt</parameter>\n<parameter=content>first half of the fi' ``` ```sh curl -s http://127.0.0.1:18096/v1/chat/completions -H 'Content-Type: application/json' -d '{"model": "x", "max_tokens": 1024, "messages": [{"role": "user", "content": "save my notes"}], "tools": [{"type": "function", "function": {"name": "write", "parameters": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}}}]}' ``` `choices[0]` of the answer: ```json {"index": 0, "message": {"role": "assistant", "content": null, "tool_calls": [{"id": "call_3136388e273447dbaec5bcf0", "type": "function", "function": {"name": "write", "arguments": "{\"path\":\"notes.txt\",\"content\":\"first half of the fi\"}"}}]}, "finish_reason": "tool_calls"} ``` With `"stream": true` the streamed arguments add up to the same JSON, and the stream ends with `finish_reason: "tool_calls"`. ### Expected A call the model did not finish should not be reported as a finished one, so that clients can drop it instead of running it: for example, leave its arguments unclosed and end with a finish reason other than `tool_calls`, as a token-limit cut does. (`finish()`'s docstring says "an unterminated tool call is returned as content"; today that happens only when the cut comes before the call's name was streamed.)
Author
Owner

@Interpause commented on GitHub (Sep 30, 2026):

dupe of #210, in fact #210 is likely the root cause. what youre suggesting is to report when it happens better

<!-- gh-comment-id:5906920977 --> @Interpause commented on GitHub (Sep 30, 2026): dupe of #210, in fact #210 is likely the root cause. what youre suggesting is to report when it happens better
Author
Owner

@alphastorm commented on GitHub (Sep 30, 2026):

Thanks, you're right: #210 covers this. Its second trigger is this case (the turn ends inside a call and _close_scan closes it), and its fail-loud fix is what this issue asks for. #210 went up a minute before this one, so our duplicate search missed it. Closing as a duplicate of #210.

One detail for that fix: the OpenAI stream counts a call as soon as its name is streamed (server.py#L1072-L1074), and L1087 turns stop plus any counted call into finish_reason: "tool_calls"; the non-streamed answer takes its finish reason from the same stream. So a fix in finish() / _close_scan() alone would still report tool_calls; the finish reason also has to follow whether the call was completed.

<!-- gh-comment-id:5907006636 --> @alphastorm commented on GitHub (Sep 30, 2026): Thanks, you're right: #210 covers this. Its second trigger is this case (the turn ends inside a call and `_close_scan` closes it), and its fail-loud fix is what this issue asks for. #210 went up a minute before this one, so our duplicate search missed it. Closing as a duplicate of #210. One detail for that fix: the OpenAI stream counts a call as soon as its name is streamed ([server.py#L1072-L1074](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1072-L1074)), and [L1087](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1087) turns `stop` plus any counted call into `finish_reason: "tool_calls"`; the non-streamed answer takes its finish reason from the same stream. So a fix in `finish()` / `_close_scan()` alone would still report `tool_calls`; the finish reason also has to follow whether the call was completed.
Author
Owner

@alphastorm commented on GitHub (Sep 30, 2026):

Reopening: 0.1.28 fixed #210's first trigger (</parameter> / </tool_call> inside a value), not this one. When the output ends inside a call, finish() (frontend.py#L532-L535) still closes it and emits tool_call, and the answer still ends with finish_reason: "tool_calls".

At bbaaabb (0.1.28), no model:

from serve.frontend import OutputParser

body = ("<tool_call>\n<function=write>\n"
        "<parameter=path>\na.txt\n</parameter>\n"
        "<parameter=content>\npartial content")  # the turn ends here
p = OutputParser(thinking=False, stream_tools=True)
for ev in p.feed(body) + p.finish():
    print(ev.kind, "|", ev.text)
tool_start |
tool_args | {
tool_args | "path":
tool_args | "a.txt"
tool_args | ,"content":
tool_args | "partial content"}
tool_call |

Through python -m serve.server --engine mock --script "</think>\n\n<the text above>", a streamed and a non-streamed OpenAI request with a write tool both end with finish_reason: "tool_calls" and the arguments {"path":"a.txt","content":"partial content"}. In the web app's MCP loop, the unfinished call runs with empty arguments.

#231 proposes a fix.

<!-- gh-comment-id:5911090716 --> @alphastorm commented on GitHub (Sep 30, 2026): Reopening: 0.1.28 fixed #210's first trigger (`</parameter>` / `</tool_call>` inside a value), not this one. When the output ends inside a call, `finish()` ([frontend.py#L532-L535](https://github.com/Niko1221/Strata/blob/bbaaabb4643bb7873d4cef9d48b5dcf96e6cbff4/serve/frontend.py#L532-L535)) still closes it and emits `tool_call`, and the answer still ends with `finish_reason: "tool_calls"`. At `bbaaabb` (0.1.28), no model: ```python from serve.frontend import OutputParser body = ("<tool_call>\n<function=write>\n" "<parameter=path>\na.txt\n</parameter>\n" "<parameter=content>\npartial content") # the turn ends here p = OutputParser(thinking=False, stream_tools=True) for ev in p.feed(body) + p.finish(): print(ev.kind, "|", ev.text) ``` ``` tool_start | tool_args | { tool_args | "path": tool_args | "a.txt" tool_args | ,"content": tool_args | "partial content"} tool_call | ``` Through `python -m serve.server --engine mock --script "</think>\n\n<the text above>"`, a streamed and a non-streamed OpenAI request with a `write` tool both end with `finish_reason: "tool_calls"` and the arguments `{"path":"a.txt","content":"partial content"}`. In the web app's MCP loop, the unfinished call runs with empty arguments. #231 proposes a fix.
Author
Owner

@Niko1221 commented on GitHub (Sep 30, 2026):

Confirmed. #231 goes into 0.1.31, plus one more change: a non-streamed OpenAI answer also drops tool calls whose arguments don't parse when the answer was cut, since many SDKs run any tool calls present.

<!-- gh-comment-id:5917510877 --> @Niko1221 commented on GitHub (Sep 30, 2026): Confirmed. #231 goes into 0.1.31, plus one more change: a non-streamed OpenAI answer also drops tool calls whose arguments don't parse when the answer was cut, since many SDKs run any tool calls present.
Author
Owner

@Niko1221 commented on GitHub (Oct 1, 2026):

Fixed in 0.1.31 (#231 plus the non-streamed follow-up). Thanks!

<!-- gh-comment-id:5925209468 --> @Niko1221 commented on GitHub (Oct 1, 2026): Fixed in [0.1.31](https://github.com/Niko1221/Strata/releases/tag/v0.1.31) (#231 plus the non-streamed follow-up). Thanks!
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#109
No description provided.