Skip to content

fix: handle Anthropic thinking blocks#787

Open
tomasvn20 wants to merge 1 commit into
repowise-dev:mainfrom
tomasvn20:fix/issue-750-anthropic-thinking-block
Open

fix: handle Anthropic thinking blocks#787
tomasvn20 wants to merge 1 commit into
repowise-dev:mainfrom
tomasvn20:fix/issue-750-anthropic-thinking-block

Conversation

@tomasvn20

Copy link
Copy Markdown

Summary

  • Updates AnthropicProvider to scan response.content for the first block carrying text instead of assuming that the first content block is always a TextBlock.
  • Prevents generation from crashing when Anthropic or Anthropic-compatible endpoints return a ThinkingBlock before the final text response.
  • Adds a regression test using a mocked [ThinkingBlock, TextBlock] response.

Related Issues

Fixes #750

Related to #740

Implementation Details

Anthropic responses can contain different content block types. When extended thinking is enabled, or when an Anthropic-compatible endpoint returns reasoning content, a ThinkingBlock may appear before the final TextBlock.

Previously, the provider accessed the first content block directly:

response.content[0].text

This caused an AttributeError when the first block did not expose a text attribute.

The provider now iterates through response.content and selects the first block that carries text:

text_content = ""
for block in response.content:
    if hasattr(block, "text"):
        text_content = block.text
        break

This preserves the existing behavior for standard Anthropic responses while supporting responses where thinking or reasoning blocks appear before the text.

Test Plan

  • Added a regression test with [ThinkingBlock, TextBlock] content.
  • Anthropic provider unit tests pass (13 passed).
  • Ruff lint passes for the modified files.
  • Ruff formatting check passes for the modified files.
  • git diff --check passes.
  • Web build is not applicable because there are no frontend changes.

The full make test-fast run completed with 7,069 passing tests and 11 failures in unrelated CLI, update, and code-rationale tests. Representative failures also reproduce when executed independently and do not exercise the Anthropic provider changes.

Checklist

@repowise-bot

repowise-bot Bot commented Jul 11, 2026

Copy link
Copy Markdown

✅ Health: 7.6 (unchanged)

📋 At a glance
1 file changed health · 2 hotspots touched · 1 new finding introduced · 5 co-change pairs left out · 2 files with recent fix history.

Files & modules (2)
  • packages (1 file)
    • .../llm/anthropic.py
  • tests (1 file)
    • .../test_providers/test_anthropic_provider.py

🩹 Review priority (files here with the most recent bug-fix history — defects cluster, so review these first)

File Score Δ Why
.../test_providers/test_anthropic_provider.py 7.9 → 7.4 ▼ -0.5 ✅ resolved dry violation, duplicated assertion block
🔎 More signals (2)

🔥 Hotspots touched (2)

  • .../test_providers/test_anthropic_provider.py — 1 commits/90d, 0 dependents · primary owner: Raghav Chamadiya (93%)
  • .../llm/anthropic.py — 6 commits/90d, 4 dependents · primary owner: Raghav Chamadiya (95%)

🔗 Hidden coupling (2 files)

  • .../llm/anthropic.py co-changes with these files (not in this PR):
    • .../llm/openai.py (6× — 🟢 routine)
    • .../llm/gemini.py (5× — 🟢 routine)
    • .../llm/litellm.py (5× — 🟢 routine)
    • .../llm/deepseek.py (3× — 🟢 routine)
  • .../test_providers/test_anthropic_provider.py co-changes with .../test_providers/test_openai_provider.py (3× — 🟢 routine) — not in this PR.

📊 Full report · ⭐ Star Repowise · 📥 Install bot · Last updated 2026-07-11 18:53 UTC
Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot

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.

AnthropicProvider crashes when the first content block is not a TextBlock (ThinkingBlock from Anthropic-compatible endpoints)

2 participants