fix: don't inject cache_breakpoint for non-Anthropic LLMs#5954
fix: don't inject cache_breakpoint for non-Anthropic LLMs#5954felipebridge wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthrough
ChangesAnthropic-conditional cache breakpoint
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/src/crewai/agents/crew_agent_executor.py`:
- Around line 167-170: _llm_is_anthropic can miss Anthropic models when the
model string has different casing or when self.llm is provided as a plain
string; update _llm_is_anthropic to handle both shapes and normalize case: if
self.llm is a string use it directly, otherwise get getattr(self.llm, "model",
"") and then lower() the model value before checking startswith("claude") or
"anthropic" in model to ensure consistent detection; reference the
_llm_is_anthropic function and the self.llm / model variable when making the
change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 02d3e9d0-8177-46d3-aa88-a99382cc9f1e
📒 Files selected for processing (1)
lib/crewai/src/crewai/agents/crew_agent_executor.py
| def _llm_is_anthropic(self) -> bool: | ||
| """Return True if the configured LLM is an Anthropic model.""" | ||
| model: str = getattr(getattr(self, "llm", None), "model", "") or "" | ||
| return model.startswith("claude") or "anthropic" in model |
There was a problem hiding this comment.
Harden Anthropic detection for case and string LLM refs.
_llm_is_anthropic() can false-negative when model casing isn’t lowercase or when self.llm is a string ref (allowed by the field type). Normalizing and handling both shapes keeps behavior consistent for Anthropic users.
Suggested patch
def _llm_is_anthropic(self) -> bool:
"""Return True if the configured LLM is an Anthropic model."""
- model: str = getattr(getattr(self, "llm", None), "model", "") or ""
- return model.startswith("claude") or "anthropic" in model
+ llm_ref = getattr(self, "llm", None)
+ model: str = (
+ llm_ref if isinstance(llm_ref, str) else getattr(llm_ref, "model", "")
+ ) or ""
+ model = model.lower()
+ return model.startswith("claude") or "anthropic" in model🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/crewai/src/crewai/agents/crew_agent_executor.py` around lines 167 - 170,
_llm_is_anthropic can miss Anthropic models when the model string has different
casing or when self.llm is provided as a plain string; update _llm_is_anthropic
to handle both shapes and normalize case: if self.llm is a string use it
directly, otherwise get getattr(self.llm, "model", "") and then lower() the
model value before checking startswith("claude") or "anthropic" in model to
ensure consistent detection; reference the _llm_is_anthropic function and the
self.llm / model variable when making the change.
Problem
_setup_messagesunconditionally callsmark_cache_breakpoint()on every message regardless of the configured LLM provider.cache_breakpointis an Anthropic-only extension — Groq, OpenAI-compatible endpoints, and other providers reject messages containing this key with a hard validation error:This breaks crewAI for any non-Anthropic provider once the cache-breakpoint code path is triggered.
Fix
Added
_llm_is_anthropic()that checks the model name, and gatedmark_cache_breakpointon that check. For every other provider the messages pass through unchanged — no behavior change for existing Anthropic users.Summary by CodeRabbit
Bug Fixes