fix(core): return a matched RunnableBranch's falsy output instead of the default - #11617
Open
Khizar Chaudhry (Khizarc) wants to merge 1 commit into
Open
Khizar Chaudhry (Khizarc) wants to merge 1 commit into
Khizar Chaudhry (Khizarc) wants to merge 1 commit into
Conversation
…the default RunnableBranch._invoke decided whether a branch had run by testing its output with `if (!result)`. A branch that matched and returned 0, "", false, null or undefined is indistinguishable from no branch matching under that test, so its output was discarded and the default branch ran instead, silently. Track whether a branch matched rather than inferring it from the output. The streaming path already avoids this by testing `stream === undefined`, which is safe there because a stream object is never falsy; an explicit flag is used here because a branch may legitimately return undefined. Fixes langchain-ai#11583 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013Pfaw9PDrV9hnhQCMdYvTR
|
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 #11583
Problem
RunnableBranch._invokedecided whether a branch had run by testing its output:A branch that matched and returned a falsy value is indistinguishable from no branch
matching at all, so its output was discarded and the default ran instead. No error is
raised, so the call silently returns the wrong value:
Fix
Track whether a branch matched instead of inferring it from the output.
The streaming path at
_streamIteratoralready avoids this withstream === undefined,which is safe there because a stream object is never falsy. I used an explicit flag
here rather than mirroring that check, because a branch may legitimately return
undefined—result === undefinedwould still fall through in that case. There is atest covering it.
Tests
Added to
runnable_branch.test.ts:0,"",falseornullis returned, not the defaultundefinedis returned, not the defaultbatchpreserves falsy branch outputsVerified these fail before the change and pass after:
runnable_branch.test.tsFull
src/runnables/tests/suite: 170 passed, with the same 2 pre-existingstreamEventsfailures present on a clean checkout. No type errors.