Skip to content

fix: handle non-text first content block in AnthropicProvider#753

Open
TemRevil wants to merge 1 commit into
repowise-dev:mainfrom
TemRevil:fix/anthropic-non-text-first-block
Open

fix: handle non-text first content block in AnthropicProvider#753
TemRevil wants to merge 1 commit into
repowise-dev:mainfrom
TemRevil:fix/anthropic-non-text-first-block

Conversation

@TemRevil

Copy link
Copy Markdown

Fixes #750

AnthropicProvider assumed response.content[0] is always a TextBlock and read .text from it. Some Anthropic-compatible endpoints (and Anthropic itself with extended thinking) put a thinking/reasoning block first, so that access raised 'ThinkingBlock' object has no attribute 'text' and crashed every generation, as reported in #740 against Kimi's endpoint.

Following the approach in the issue, added a _first_text_block() helper that scans response.content for the first block carrying text instead of indexing block 0 (falling back to an empty string).

Added test_generate_skips_leading_non_text_block, which mocks a response whose content is [thinking_block, text_block] (the thinking block spec'd without .text so accessing it raises AttributeError, matching the real crash) and asserts the text block's content is returned.

tests/unit/test_providers/test_anthropic_provider.py .............  [100%]
13 passed

The Messages API response is not guaranteed to start with a text block:
Anthropic-compatible endpoints (and Anthropic with extended thinking
enabled) can emit a thinking/reasoning block before the text block.
Indexing response.content[0].text then raised 'ThinkingBlock object has
no attribute text' and crashed every generation.

Scan response.content for the first block that carries text instead of
assuming block 0 is text. Falls back to an empty string if no text block
is present.

Fixes repowise-dev#750
@repowise-bot

repowise-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

✅ Health: 7.7 (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-10 12:58 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)

1 participant