fix(langgraph-api): recover tool_call args from cumulative-args providers at end of call - #2697
Open
Serhiy Bzhezytskyy (serhiy-bzhezytskyy) wants to merge 2 commits into
Conversation
…ders at end of call A provider that re-sends the cumulative args string per chunk instead of a delta (langchain-ai#2570) leaves messages/partial with a concatenation that never parses. Keep concatenating during the stream; once the model run ends, an args string that does not parse, whose chunks form a growing prefix chain and whose last chunk parses on its own, is replaced by that last chunk in a final messages/partial frame. Anything whose concatenation parses is untouched, so compliant streams never change — including chunks that happen to chain. Output that was already invalid and chains may be read as its only parsing interpretation instead of surfacing as Malformed args; that trade is pinned by a dedicated test.
🦋 Changeset detectedLatest commit: b8f0b5c The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
…isfy oxfmt The build tsc rejects tool_call_chunks on BaseMessageChunk — the recording loop now runs only for AIMessageChunk, matching the guard the recovery itself already had. Formatting aligned with oxfmt.
@langchain/langgraph-checkpoint
@langchain/langgraph-checkpoint-mongodb
@langchain/langgraph-checkpoint-postgres
@langchain/langgraph-checkpoint-redis
@langchain/langgraph-checkpoint-sqlite
@langchain/langgraph-checkpoint-validation
create-langgraph
@langchain/langgraph-api
@langchain/langgraph-cli
@langchain/langgraph
@langchain/langgraph-cua
@langchain/langgraph-supervisor
@langchain/langgraph-swarm
@langchain/langgraph-ui
@langchain/langgraph-sdk
@langchain/angular
@langchain/react
@langchain/svelte
@langchain/vue
commit: |
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.
Summary
Implements the end-of-call recovery proposed in #2570 (comment) for providers that re-send the cumulative tool_call args string per chunk instead of a delta: the concatenation never parses, and
messages/partialends withMalformed args./invalid_tool_callseven though the model's own final message is clean.Changes
src/stream.mts— keep concatenating during the stream; when the model run ends and an args string does not parse, and its chunks form a growing prefix chain whose last chunk parses on its own, a finalmessages/partialframe carries that last chunk instead. Anything whose concatenation parses is untouched; mid-stream frames are unchanged.Test plan
tests/stream.test.mts: cumulative snapshots recovered; compliant deltas untouched, including chunks that happen to chain (self-similar args split at the seam); broken output without a chain preserved; the design trade pinned — already-invalid output that chains is read as its only parsing interpretation ({{"a":1}split as{+{"a":1}); a real compiled graph confirms the productionon_chat_model_endshape fires the recovery and the rebuilt message carries parsedtool_calls; two parallel tool calls stay isolated@langchain/langgraph-api; package suite 108 passed (3 file-load failures identical on cleanmain)Related