fix(core): merge callbacks without re-registering handlers both configs already hold - #2696
Open
Serhiy Bzhezytskyy (serhiy-bzhezytskyy) wants to merge 1 commit into
Conversation
…gs already hold At a nested-runnable boundary ensureLangGraphConfig merges the ambient AsyncLocalStorage config with the child config, and both carry the same handler instances; concatenating them re-registers every handler once per boundary, compounding per level. With a LangChainTracer attached, every on_chat_model_stream event fires three times two subgraph levels deep (langchain-ai#2570), LangSmith ingestion multiplies, and EventStreamCallbackHandler reports 'Run ID not found in run map'. The Python runtime does not duplicate: its add_handler checks membership. mergeCallbacks now adds a handler from the other side only when it is not already present; distinct instances merge as before. Red-first tests cover all four merge shapes plus the ambient path, and a guard pins the dedupe to instance identity.
🦋 Changeset detectedLatest commit: 0fa183e 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 |
@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: |
3 tasks
Author
|
Review context from this file's history: #2531 fixed the bound + invoke-time pair of this same defect (the "no double-firing" guard in |
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
Fixes the duplication half of #2570.
With a
LangChainTracerattached, a graph containing a subgraph emits everyon_chat_model_streamevent once per nesting level, somessages/partialconsumers see multiplied content and unparseable tool_call args with no provider involvement. The same duplication multiplies LangSmith ingestion and produces the reportedhandleChainEnd: Run ID … not found in run maperrors.Root cause
At every nested-runnable boundary
ensureLangGraphConfigmerges the ambient AsyncLocalStorage config with the child config, and both carry the same handler instances;mergeCallbacksconcatenates without checking, so the handlers compound per level (1 → 3 → 6). The Python runtime does not have the defect — itsadd_handlerchecks membership — and pure langchain-core with the same nesting does not duplicate either, so the fix belongs to this merge rather than to core'saddHandler.Changes
pregel/utils/config.ts—mergeCallbacksadds a handler from the other side only when it is not already present. Distinct instances merge as before.Test plan
pregel/utils/config.test.ts: the four merge shapes plus the ambient path, all red onmain, and a guard that two distinct instances sharing a name still both registeron_chat_model_stream6 → 2 per two chunks (12 → 2 three levels deep);.stream(streamMode: "messages")6 → 2; LangSmithcreateRunposts 12 → 8 for 7 runs; run-map errors 5 → 0; no-tracer behavior unchanged; no measurable wall-time costlanggraph-coresuite 1,644 passed, typecheck cleanRelated
langgraph-apimerge.createRun(8 for 7 runs) comes fromcopyWithTracingConfigcloning the tracer per level; distinct instances are deliberately kept.