Skip to content

fix(streaming): keep a text delta that equals the text streamed so far - #471

Closed
pcbeingused33 wants to merge 3 commits into
posit-dev:mainfrom
pcbeingused33:fix/merge-dicts-equal-delta
Closed

pcbeingused33 wants to merge 3 commits into
posit-dev:mainfrom
pcbeingused33:fix/merge-dicts-equal-delta

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Fixes #435.

merge_dicts skipped 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 as id or model from being doubled, but it also dropped a streamed text delta that happened to equal everything streamed so far, and, because delta is 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.". OpenAICompletionsProvider and every provider built on it (ChatOllama(), ChatGroq(), ChatDeepSeek(), ChatOpenRouter(), ...) merge chunks through it.

What changed

  • A string value under one of the streamed text keys (content, arguments, refusal, reasoning, reasoning_content, text) is always appended, equal or not.
  • Equal dicts, and equal lists whose items carry an int 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, type or a tool's function.name on 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.py drives the repro from the issue through stream_merge_chunks and stream_turn; test_utils_merge.py covers the equal text delta, ["ab", "c", "abc"], nested arguments, 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

cpsievert and others added 3 commits September 14, 2026 11:58
`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
@cpsievert

Copy link
Copy Markdown
Collaborator

Duplicate of #436

@cpsievert cpsievert marked this as a duplicate of #436 Oct 21, 2026
@cpsievert cpsievert closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A streaming text delta that repeats everything streamed so far is missing from the recorded turn

2 participants