Skip to content

fix(streaming): reject incomplete streams in final message helpers - #2087

Open
bowenzhu21 wants to merge 2 commits into
anthropics:mainfrom
bowenzhu21:fix/streaming-reject-incomplete-final-message
Open

bowenzhu21 wants to merge 2 commits into
anthropics:mainfrom
bowenzhu21:fix/streaming-reject-incomplete-final-message

Conversation

@bowenzhu21

Copy link
Copy Markdown

Summary

When a successful HTTP response ends before the final message_stop event, 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 a tool_use stop reason on an incomplete stream.

The stable and beta final-result helpers now raise anthropic.AnthropicError if the stream ends before message_stop. The stop reason can arrive one event earlier. Callers that intentionally use partial output can iterate the events or inspect current_message_snapshot after a message has started. The change leaves the wire protocol and method signatures intact.

The TypeScript SDK already records the message on message_stop and 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

  • Added public-client tests for stable and beta APIs, sync and async. They cover an empty stream, a cut in the middle of text, a cut after the stop reason, and a completed message.
  • Added sync and async default tool-runner tests. An interrupted tool-use response must produce an error, zero tool calls, and just the original HTTP request.
  • Fixed the test fixture reader to emit the blank line that dispatches the last SSE event. The success cases now exercise a complete wire response.
  • On 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.
  • After switching the regression tests to the shared client fixtures and documenting the completion flag (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' -n0

Release note

Final-message and final-text helpers raise AnthropicError when a stream ends before message_stop.

Raise AnthropicError when a final result is requested before message_stop. Keep partial events and snapshots available to callers that inspect interrupted streams.
@bowenzhu21
bowenzhu21 marked this pull request as ready for review November 26, 2026 19:49
@bowenzhu21
bowenzhu21 requested a review from a team as a code owner November 26, 2026 19:49

This branch has not been deployed

No deployments
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.

1 participant