feat: add additional callbacks to LangChain/LangGraph#12281
Merged
lgrammel merged 2 commits intovercel:mainfrom Feb 12, 2026
Merged
feat: add additional callbacks to LangChain/LangGraph#12281lgrammel merged 2 commits intovercel:mainfrom
lgrammel merged 2 commits intovercel:mainfrom
Conversation
lgrammel
approved these changes
Feb 12, 2026
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.
Background
When using LangGraph with
toUIMessageStream, there's no way to access the final graph state after stream completion. The existingonFinalcallback only provides aggregated text, not the full state object containing tool calls, custom fields, or other graph-specific data.Currently, consumers must wrap the raw LangGraph stream with custom logic before passing to
toUIMessageStream:Summary
Extends
StreamCallbackswith three new callbacks:onFinish(state)undefinedfor model/streamEvents)onError(error)ErrorobjectonAbort()Callback lifecycle:
onFinalonFinishonErroronAbortOther changes:
parseLangGraphEventhelper toutils.ts(extracts duplicated logic)Design rationale:
This aligns with the core SDK's
streamTextcallback pattern:onFinishonFinishonErroronErroronAbortonAbortonFinalThe naming overlap between
onFinalandonFinishexists becauseonFinalwas added to the langchain package before the core SDK standardized ononFinish. We preserveonFinalfor backward compatibility.Trade-offs:
onFinishreceivesundefinedfor non-LangGraph streams (consistent lifecycle vs. conditional invocation)AbortError; edge cases go toonErroronFinishcurrently only receivesstate, not a rich event object like core SDKManual Verification
Verified callback behavior using the
next-langchainexample with a local LangGraph agent. Confirmed:onFinishreceives complete state after successful streamonErrorfires on thrown errorsonAbortfires when request is cancelled via AbortControllerFuture Work
Consolidate
onFinalintoonFinish(breaking change):The ideal API would deprecate
onFinaland haveonFinishreceive a unified result object:This would:
onFinalvsonFinishnamingonFinish(success, error, or abort)Enrich
onFinishevent data:The core SDK's
onFinishreceives rich metadata (usage,finishReason,steps, etc.). We could expand ours:Other improvements:
onStepStart/onStepFinishcallbacks for LangGraph step transitionsECONNRESET) if users report issues