Repository navigation
fix(streaming): keep a text delta that equals the text streamed so far - #471
Closed
pcbeingused33 wants to merge 3 commits into
Closed
pcbeingused33 wants to merge 3 commits into
pcbeingused33 wants to merge 3 commits into
Conversation
`merge_dicts()` had an equality check (`left_v == right_v → continue`) before the str/dict/list branches. When a streamed text delta happened to equal the full accumulation so far (e.g. "\n" + "\n"), the second delta was silently dropped — both at the string-leaf level and at the container level (an equal dict skipped recursion entirely). Move the str/dict/list branches before the equality check, matching langchain's original ordering. Strings always concatenate, containers always recurse, and the equality check now only applies to remaining scalar types (int, float, bool). Closes posit-dev#435
Collaborator
|
Duplicate of #436 |
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 #435.
merge_dictsskipped a value equal to the accumulated one (left_v == right_v: continue) before the branch that concatenates strings. That is what keeps repeated identifiers such asidormodelfrom being doubled, but it also dropped a streamed text delta that happened to equal everything streamed so far, and, becausedeltais compared as a whole dict, a whole delta that repeated the previous one. The"\n","\n","Here is the answer."stream from #435 was stored as"\nHere is the answer.".OpenAICompletionsProviderand every provider built on it (ChatOllama(),ChatGroq(),ChatDeepSeek(),ChatOpenRouter(), ...) merge chunks through it.What changed
content,arguments,refusal,reasoning,reasoning_content,text) is always appended, equal or not.index(choices,tool_calls), are merged instead of skipped, so the equality check no longer hides a text key nested in them. Other equal values (ids, labels, numbers, lists such as Gemini's safety ratings) are skipped as before.The choice to make. The key list is the most conservative rule found: nothing that was skipped before is appended now except those text keys, so a provider that repeats
id,model,role,typeor a tool'sfunction.nameon each chunk still works. The alternative is to drop the equality skip for all strings, which also fixes unknown text keys but doubles any repeated identifier unless those are listed instead (id,model,object,role,type,name,finish_reason, ...). If you prefer that direction it is a small change in the same place; I kept the list of text keys because a new provider's repeated label would silently corrupt a turn the other way round.Tests.
test_provider_openai_completions.pydrives the repro from the issue throughstream_merge_chunksandstream_turn;test_utils_merge.pycovers the equal text delta,["ab", "c", "abc"], nestedarguments, and that repeated identifiers are not appended. The two new tests fail on main and the whole suite (1347 passed, 49 skipped) passes here. No request was made to a live API.This change was made with Claude Code.
🤖 Generated with Claude Code