fix: crash on non-text content blocks in Anthropic LLM#681
fix: crash on non-text content blocks in Anthropic LLM#681Giggitycountless wants to merge 1 commit into
Conversation
Three functions accessed block.text unconditionally, crashing with
AttributeError (pydantic models) or KeyError (dicts) when encountering
non-text blocks like ThinkingBlock, RedactedThinkingBlock, or ToolUseBlock.
Fixes:
- message_param_str (line 915): hasattr(block, 'text') guard
- message_str (line 932): hasattr(block, 'text') guard
- anthropic_content_to_mcp_content (lines 1378, 1381):
getattr(block, 'text', None) and block.get('text')
Non-text blocks (ToolUseBlock, ThinkingBlock, RedactedThinkingBlock) do
not have a .text attribute. The existing code assumed all blocks are
TextBlock, causing crashes when extended thinking or tool use is active.
Tests: 10 new test cases covering all 4 crash sites with TextBlock,
ToolUseBlock, ThinkingBlock, RedactedThinkingBlock, mixed blocks, and
dict-style blocks. All 36 tests pass (26 existing + 10 new).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis change hardens the Anthropic-to-MCP message conversion logic to safely handle content blocks that may lack a Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
What changed
Fix 4 crash sites in
augmented_llm_anthropic.pywhereblock.textis accessed unconditionally on non-text content blocks (ThinkingBlock, RedactedThinkingBlock, ToolUseBlock).Bug
The Anthropic API returns
Message.contentas a union ofTextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock. OnlyTextBlockhas a.textattribute. Three functions accessed.textunconditionally:message_param_strAttributeError: dict has no attribute textmessage_strAttributeError: ThinkingBlock has no attribute textanthropic_content_to_mcp_contentAttributeError: ToolUseBlock has no attribute textanthropic_content_to_mcp_contentKeyError: textThis crashes whenever extended thinking or tool use is active in the response.
Fix
message_param_strandmessage_str: Addhasattr(block, "text")guard before accessing.textanthropic_content_to_mcp_content: Usegetattr(block, "text", None)for pydantic models andblock.get("text")for dicts4 lines changed total.
Tests
10 new test cases covering all 4 crash sites:
All 36 tests pass (26 existing + 10 new). Zero regressions.
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests