Uh oh!
There was an error while loading. Please reload this page.
fix(opencode): recover from truncated tool calls instead of failing silently - #21688
fix(opencode): recover from truncated tool calls instead of failing silently#21688nickveenhof wants to merge 2 commits into
Conversation
…ilently When a model's output hits the token limit mid-tool-call, the JSON arguments are truncated and tool parsing fails with 'expected string, received undefined'. Two fixes: 1. experimental_repairToolCall now detects truncation: when the tool name is a valid registered tool but args failed to parse, return an actionable error telling the model to split its operation into smaller pieces. Previously all parse failures were routed to a generic 'invalid tool' handler with no recovery guidance. 2. The session loop now handles finishReason 'length': instead of silently exiting when the model is cut off by the token limit, inject a synthetic continuation message so the model can resume where it left off. Fixesanomalyco#18108Fixesanomalyco#17471 Refs anomalyco#14519, anomalyco#13102
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
rekram1-node
commented
May 15, 2026
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes#18108
Closes#17471
Refs #14519, #13102, #18151, #18131
Type of change
What does this PR do?
When a model's output hits
max_tokensmid-tool-call, the streamed JSON arguments get truncated. The tool parser receivesundefinedfor required fields, producing:Two fixes:
1. Truncation detection in
experimental_repairToolCall(llm.ts)repairToolCallcurrently only handles tool name case-sensitivity. All other parse failures route to the genericinvalidtool. But whentoolNameis a valid registered tool and the args failed to parse, the cause is truncation, not an invalid tool. This check distinguishes the two cases and returns an actionable error telling the model to split its operation into smaller pieces.2. Auto-continue on
finishReason: "length"(prompt.ts)The session loop exits when
finishReasonis anything other than"tool-calls". This means"length"(token limit hit) causes the session to stop silently. Now the loop detects"length", injects a synthetic continuation message, and keeps going so the model can resume.How did you verify your code works?
bun run --cwd packages/opencode tsc --noEmitpasses with no new type errorsOUTPUT_TOKEN_MAXand verifying the model receives the truncation error message and retries with smaller operationsScreenshots / recordings
N/A - no UI changes
Checklist