Skip to content

Fix AttributeError when Anthropic response leads with a thinking block - #186

Closed
potsju wants to merge 1 commit into
alexzhang13:mainfrom
potsju:fix/anthropic-thinking-block-text-extraction
Closed

potsju wants to merge 1 commit into
alexzhang13:mainfrom
potsju:fix/anthropic-thinking-block-text-extraction

Conversation

@potsju

@potsju potsju commented Sep 20, 2026

Copy link
Copy Markdown

Summary

  • AnthropicClient.completion()/acompletion() indexed response.content[0].text directly, assuming the first content block is always text.
  • When extended thinking is enabled, Anthropic returns a ThinkingBlock first, which has no .text attribute, so the call crashes with AttributeError: 'ThinkingBlock' object has no attribute 'text'. Hit this in a live run against claude-sonnet-5.
  • Adds a small _extract_text() helper that scans response.content for the first type == "text" block and raises a clear ValueError if none is present (fail-loud rather than a silent empty-string fallback), and uses it in both completion() and acompletion().

Test plan

  • Added tests/clients/test_anthropic.py (none existed before), including a regression test reproducing the exact crash shape (leading ThinkingBlock + trailing text block, for both sync and async).
  • uv run ruff check / uv run ruff format --check clean.
  • Full uv run pytest suite passes: 336 passed, 9 skipped.

🤖 Generated with Claude Code

AnthropicClient.completion()/acompletion() indexed response.content[0]
directly, assuming it was always the text block. When extended thinking
is enabled, Anthropic returns a ThinkingBlock first, which has no .text
attribute and crashes the call. Extract the first block with type=="text"
instead, and raise a clear error if none is present.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@potsju potsju closed this Sep 20, 2026
@potsju
potsju deleted the fix/anthropic-thinking-block-text-extraction branch September 20, 2026 02:56
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