Repository navigation
fix(streaming): reject incomplete streams in final message helpers - #2087
Open
bowenzhu21 wants to merge 2 commits into
Open
bowenzhu21 wants to merge 2 commits into
bowenzhu21 wants to merge 2 commits into
Conversation
Raise AnthropicError when a final result is requested before message_stop. Keep partial events and snapshots available to callers that inspect interrupted streams.
bowenzhu21
marked this pull request as ready for review
November 26, 2026 19:49
This branch has not been deployed
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
When a successful HTTP response ends before the final
message_stopevent,get_final_text()can return a partial answer. For example, interrupting"First second."after the first text delta returned"First"as the final text. The default streaming tool runner could also execute a tool and send its result after receiving atool_usestop reason on an incomplete stream.The stable and beta final-result helpers now raise
anthropic.AnthropicErrorif the stream ends beforemessage_stop. The stop reason can arrive one event earlier. Callers that intentionally use partial output can iterate the events or inspectcurrent_message_snapshotafter a message has started. The change leaves the wire protocol and method signatures intact.The TypeScript SDK already records the message on
message_stopand rejects a final-result request if no message was recorded. The Go beta streaming tool runner checks the stream error before handling the accumulated response. Its behavior on a clean early EOF has not been tested here.Tests
8dac3eba, the full Linux suite passed with Python 3.10 and 3.14, including Pydantic v1 on Python 3.10 and deferred builds on 3.14. The Python 3.10/Pydantic v2 run had 15,178 passes and 41 skips.735a45de), all three affected test files passed again. On Python 3.10 and 3.14 with Pydantic v2, 256 passed on each. With Pydantic v1, 92 passed and 164 were skipped. Lint, type checks, and package verification also passed.Run the new cases from the repository root.
./scripts/test tests/lib/streaming/test_messages.py tests/lib/tools/test_runners.py -k 'final_getters_require_message_stop or requires_message_stop_before_running_a_tool' -n0Release note
Final-message and final-text helpers raise
AnthropicErrorwhen a stream ends beforemessage_stop.