Repository navigation
fix(core): don't turn truncated streamed tool-call arguments into a tool call - #11994
Open
Simin Yuan (simin-yuan) wants to merge 1 commit into
Open
Simin Yuan (simin-yuan) wants to merge 1 commit into
Simin Yuan (simin-yuan) wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 30869cc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Simin Yuan (simin-yuan)
force-pushed
the
fix/core-truncated-streamed-tool-calls
branch
from
November 1, 2026 15:17
eee12e1 to
16b2d41
Compare
Author
|
Rebased onto current The change ships with a test ( The checks here are sitting in |
…ool call Once a stream has ended, re-derive tool calls from tool_call_chunks with JSON.parse, the rule the non-streaming path already applies. A call whose arguments were cut off (output limit or a stream that just ends) lands in invalid_tool_calls with the raw string instead of being repaired into a complete-looking call. Covers aggregation in BaseChatModel and providers that stream inside _generate. Chunks seen while streaming keep the partial parse. Fixes langchain-ai#11469
Simin Yuan (simin-yuan)
force-pushed
the
fix/core-truncated-streamed-tool-calls
branch
from
November 4, 2026 20:02
16b2d41 to
30869cc
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #11469
Problem. When a stream ends partway through a tool call's arguments,
collapseToolCallChunksrepairs the partial JSON withparsePartialJson. The aggregated message then has a complete-looking entry intool_callsand nothing ininvalid_tool_calls, so agents built oninvoke()(e.g.createReactAgent) run the tool with half its input. This happens onfinish_reason: "length", and also when the stream simply ends (no finish reason, no[DONE]), which is what a dropped connection or proxy looks like.Change.
collapseToolCallChunks(chunks, { final: true })parses withJSON.parse, the rule the non-streaming path (parseToolCall) already applies. The default is unchanged.BaseChatModelapplies it once a stream has ended, in both places a streamed message is assembled: its own aggregation (streaming callbacks, e.g. LangGraph'smessagesmode) and the result of_generatefor providers that stream inside it (e.g.ChatOpenAIwithstreaming: true).It does not key on the finish reason, so it also covers the dropped-stream case, which #11494 (closed) could not.
Relation to #11471. #11471 addresses the same issue and has been open since August. Two differences worth checking against it, both in
libs/langchain-core/src/messages/utils.ts:truncatedflag gated onresponse_metadata.finish_reason === "length". A stream that simply ends (no finish reason, no[DONE]) never sets it, so that path still produces a repaired call.if (usesRawInputArgs && id)branch, soJSON.parse(argsStr)also applies to raw-input/custom tools whose arguments are deliberately not JSON. That is the question drew-chen left on fix(core): do not present repaired partial JSON as a finished tool call #11471 and it is still open. Here the strict parse happens inside the existingtry, which is reached only after that branch has alreadycontinued, so raw-input tools are untouched.If you would rather land #11471, I am glad to rebase this onto it or carry the dropped-stream and raw-input cases over there instead — either way the fix should not depend on the finish reason.
Tests.
truncated_tool_calls.test.ts: 7 tests, 3 fail before the change. They cover both aggregation paths, a complete call (with an escaped newline) still producing a tool call, mid-stream chunks staying lenient, and empty args still meaning{}. Full@langchain/coreunit suite: 1556 passed, 1 skipped; the 5 failures (count_tokens, the tracer snapshot) time out the same way without this change, because they need network access. oxlint, oxfmt and tsc are clean.End to end,
ChatOpenAI({ streaming: true }).invoke()against a fake endpoint (tool calls / invalid tool calls):finish_reason: "length"Not in scope. Code that iterates
.stream()and concatenates the chunks itself still gets the repaired call, because the chunks carry no end-of-stream marker. Python's core marks the final chunk withchunk_position="last"; a similar marker would be a follow-up, and I'm happy to do it if you want that direction.The Python side is tracked in langchain-ai/langchain#41285. Agent-level reproduction across frameworks: https://github.com/simin-yuan/agent-cutoff-bench