Skip to content

fix(threads): preserve iterator-backed message content during replay - #2086

Open
bowenzhu21 wants to merge 2 commits into
anthropics:mainfrom
bowenzhu21:fix/threads-replay-message-content
Open

bowenzhu21 wants to merge 2 commits into
anthropics:mainfrom
bowenzhu21:fix/threads-replay-message-content

Conversation

@bowenzhu21

Copy link
Copy Markdown

Summary

On SDK 2.14.0, a beta message thread sends both text blocks from iter(blocks) on its first request. If the server loses the thread or rejects threading, the SDK replays the message with "content": []. With this change, the replay sends the same two blocks as the original request.

The thread consumes a one-shot content iterator when it stages the message and saves the blocks in a shallow copy of the message dictionary. This also lets a fork replay a deferred message. Ordinary lists keep their existing behavior, including edits after staging.

Related PR #2072 addresses iterators passed as tool definitions. This change covers message content. The equivalent inputs in TypeScript and Go use arrays and slices.

Tests

The regression tests inspect the outgoing HTTP bodies for sync lost-thread recovery, async unthreaded fallback, and a fork created while a streamed turn has a deferred message. On upstream f1edcd7d, all three iterator cases failed and both list controls passed. All five pass with this change.

./scripts/test tests/lib/threads -k 'original_message_blocks or deferred_message_blocks' -n0

On 8429e950 the full Linux suite passed on Python 3.10 with Pydantic v2 (15,165 passed, 41 skipped). The thread tests also passed on Python 3.14 and with Pydantic v1. Formatting, lint, type, package, and compatibility checks passed.

Release note

Fix a bug that could drop iterator-backed message content when replaying or forking a message thread.

Retain one-shot content blocks when messages are staged, so a recovered thread or a fork can send the original blocks again.
@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