feat(lib): add strip_thinking_blocks utility for conversation history recovery
Context
Multi-turn conversations with extended thinking accumulate ThinkingBlock entries in the assistant messages. When those messages are stored and replayed later — after session expiry, model upgrades, or history reconstruction from a database — the signatures become invalid and every subsequent call fails permanently with:
400 Invalid `signature` in `thinking` block
The API documentation says to pass thinking blocks back unmodified, but does not describe what happens when signatures expire or how to recover. The only escape is to strip the thinking blocks from history, but there is no SDK utility for this and no documented procedure. Tracked in #1598.
Change
src/anthropic/lib/_thinking.py — new module with:
strip_thinking_blocks(messages)— acceptsIterable[MessageParam], returnslist[MessageParam]with everytype: "thinking"andtype: "redacted_thinking"content block removed. String content is left unchanged. All other message-level keys (role, etc.) are copied verbatim. The function works with both plain dicts (TypedDict values) andBaseModelinstances returned by the SDK. Idempotent; does not mutate the input list.
src/anthropic/lib/__init__.py — exports strip_thinking_blocks alongside files_from_dir so callers can reach it via from anthropic.lib import strip_thinking_blocks.
Intended usage pattern:
try:
response = client.messages.create(model=model, messages=history, ...)
except anthropic.BadRequestError as exc:
if "Invalid `signature` in `thinking` block" in str(exc):
history = strip_thinking_blocks(history)
response = client.messages.create(model=model, messages=history, ...)
else:
raise
Why this approach
The fix lives in lib/ as a pure utility, symmetric with the existing files_from_dir helper: it has no I/O, no API calls, and no dependencies beyond the SDK's own type definitions. It does not add any automatic behaviour to the message-sending path — the caller decides when stripping is appropriate (e.g. only on error recovery, not on every call) — keeping the SDK's default behaviour unchanged for callers who never encounter expired signatures.
Alternative considered: automatically strip thinking blocks when a 400 "Invalid signature" is received and retry the request transparently. Rejected because silent retry changes observable behaviour (thinking blocks are dropped without the caller's knowledge), complicates streaming retry semantics, and may not be what callers want in all scenarios.
Tests
Added 14 tests in tests/lib/test_thinking.py covering:
- String-content messages pass through unchanged
thinkingblocks removed from assistant messagesredacted_thinkingblocks removed- Mixed content (
thinking+ text +tool_use): only thinking types removed - User messages without thinking: unchanged
- Non-thinking blocks preserved verbatim
- All-thinking content produces empty list (edge case)
- Empty messages list
- Multi-message conversations processed independently
BaseModelinstances (_FakeThinkingBlock,_FakeRedactedBlock): stripped viagetattr- Does not mutate the original input list
- Role and other message keys preserved
- Idempotent (apply twice → same result)
All 14 tests pass (uv run pytest tests/lib/test_thinking.py -v).
Note:
tests/api_resources/beta/test_webhooks.pyfails withModuleNotFoundError: No module named 'standardwebhooks'on the upstreammainbranch before any changes in this PR. Unrelated to this fix.
Adjacent gaps
- The
BadRequestErrormessage for expired thinking signatures does not suggeststrip_thinking_blocksas a recovery path. A follow-up could add a note in the exception handling layer or in the extended-thinking documentation. Left out of this PR to keep the diff focused. strip_thinking_blocksis not yet re-exported from the top-levelanthropicnamespace (from anthropic import strip_thinking_blocks). If maintainers prefer it to be a first-class export, that would be a one-line addition tosrc/anthropic/__init__.py.