fix: accumulate all text blocks in Anthropic non-streaming response - #9786
Open
AmirF194 wants to merge 1 commit into
Open
fix: accumulate all text blocks in Anthropic non-streaming response#9786AmirF194 wants to merge 1 commit into
AmirF194 wants to merge 1 commit into
Conversation
The non-streaming _query loop set llm_response.completion_text on every text content block instead of appending to it, so only the last text block in a multi-block Message.content survived; earlier blocks were silently dropped. The streaming path already accumulates correctly with final_text +=. A Message can carry more than one text block (the Citations API and extended-thinking flows both produce this shape). Adds a regression test that fails on the old overwrite behavior and passes with the fix.
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.
ProviderAnthropic._query(the non-streaming path inanthropic_source.py) setsllm_response.completion_textinside a loop overcompletion.content, once per text content block, instead of appending. When aMessagecarries more than one text block, only the last one survives; every earlier text block is silently dropped from the reply and from conversation history. This can happen with the Citations API and with extended-thinking flows, both of which can split a response into multiple text blocks. The streaming sibling,_query_stream, already accumulates correctly withfinal_text += event.delta.text.Modifications / 改动点
Collect each text block into a list during the loop and join them once after the loop, instead of overwriting
completion_textper block.thinkingandtool_usehandling are unchanged.Screenshots or Test Results / 运行截图或测试结果
Added
test_query_accumulates_multiple_text_blocks, which feedsProviderAnthropic._querya fakeMessagewith two text blocks. It fails on unmodifiedmaster(AssertionError, only the second block's text survives) and passes with this fix. The fulltests/test_anthropic_kimi_code_provider.pyfile: 28 passed, 1 pre-existing failure unrelated to this change (test_create_http_client_uses_anthropic_httpx_module, an SDK version drift already being fixed in open PR #9769).ruff format --checkandruff checkare clean on both changed files.Not verified: a live call to the provider's API. This was reproduced and tested against the SDK's
Message/content-block shape directly, not against a real multi-block response from the API.Checklist / 检查清单
If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"。
I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Accumulate every text block returned by Anthropic’s non-streaming API so complete responses and conversation history are preserved.
Bug Fixes:
Tests: