Skip to content

fix(streaming): support trailing-strings mode and error handling in non-beta messages - #1930

Open
Christian-Sidak wants to merge 2 commits into
anthropics:mainfrom
Christian-Sidak:fix/issue-1925
Open

Christian-Sidak wants to merge 2 commits into
anthropics:mainfrom
Christian-Sidak:fix/issue-1925

Conversation

@Christian-Sidak

@Christian-Sidak Christian-Sidak commented Sep 13, 2026

Copy link
Copy Markdown

Summary\n\nThe non-beta accumulate_event in _messages.py was missing two features that _beta_messages.py already had:\n\n- Support for partial_mode="trailing-strings" when the fine-grained-tool-streaming-2025-05-14 beta header is present. Without this, incomplete trailing strings in streaming tool inputs were silently dropped from intermediate snapshots rather than preserved as partial values.\n- A ValueError with a helpful message when the partial JSON is genuinely invalid, consistent with the beta path.\n\nThis PR adds request_headers to accumulate_event, threads the headers from both sync and async MessageStream.__stream__, and mirrors the conditional logic already in _beta_messages.py. This is a standard/beta streaming parity fix.\n\n## Test plan\n\n- [x] All existing streaming tests pass (tests/lib/streaming/)\n- [x] New TestAccumulateEvent tests cover: complete JSON with and without headers, trailing-strings mode activated by the beta header, and ValueError raised for invalid JSON in both modes

@Christian-Sidak
Christian-Sidak requested a review from a team as a code owner September 13, 2026 04:44
…havior

The non-beta `accumulate_event` in `_messages.py` was not passing request
headers to the partial JSON parser, so it could not activate
`trailing-strings` mode for the `fine-grained-tool-streaming-2025-05-14`
beta when used through the standard messages endpoint. It also lacked the
error handling added to the beta path.

This change adds `request_headers` to `accumulate_event`, threads the
headers from both sync and async `MessageStream`, and mirrors the
conditional `partial_mode="trailing-strings"` and `ValueError` handling
already present in `_beta_messages.py`.

Fixes anthropics#1925

Signed-off-by: Christian-Sidak <61099993+Christian-Sidak@users.noreply.github.com>
@00200200

Copy link
Copy Markdown

Thanks for bringing the standard messages path in line with the beta path. One scope issue before calling this Fixes #1925: the reported issue is a completed strict tool call where the outer arguments JSON is valid, but translations is a string containing nested JSON.

partial_mode="trailing-strings" only changes how incomplete JSON is represented while accumulating a stream. It cannot turn that completed string into an array or validate the tool schema, so get_final_message() would still expose ToolUseBlock.input["translations"] as a str for the payload in #1925. Nothing in this diff changes that behavior.

Could you either remove the Fixes #1925 link (and frame this as standard/beta streaming parity), or add a test that demonstrates the reported completed-response case is actually addressed? As written, the implementation looks useful, but the issue link suggests a fix for a server-side/schema-conformance problem it does not cover.

@00200200 00200200 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a blocking import mismatch in the new code and tests.

This repository imports the renamed client as httpx2, but the PR uses httpx.Headers at src/anthropic/lib/streaming/_messages.py:448 and throughout the new tests. Because of that:

  • python -m pytest -q -n0 tests/lib/streaming/test_messages.py fails with 4 NameError: name 'httpx' is not defined failures.
  • mypy reports src/anthropic/lib/streaming/_messages.py:448: error: Name "httpx" is not defined.

Please use httpx2.Headers consistently (or import the correct alias) before merging. The focused tests pass after that import mismatch is corrected; the current PR head is not green locally.

Co-Authored-By: Christian Sidak <csidak@gmail.com>
@Christian-Sidak

Copy link
Copy Markdown
Author

Fixed in abe59d6:

  • httpx2 alias: replaced httpx.Headers with httpx2.Headers throughout accumulate_event and the new tests to match the rest of the codebase.
  • Fixes link: removed the Fixes #1925 link. The PR is framed as standard/beta streaming parity (bringing the non-beta accumulate_event in line with the beta path) rather than a fix for the server-side schema issue in Sonnet 5 strict tool use returns a string for an array-typed property #1925.

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.

2 participants