Fix #5893 - Broaden Anthropic model prefix detection for custom-deployed models#6240
Fix #5893 - Broaden Anthropic model prefix detection for custom-deployed models#6240Whning0513 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Summary: This PR adjusts LLM provider detection and call-start event emission for Anthropic/custom Claude model handling; no exploitable security vulnerabilities were identified.
Risk: Low risk. The changes do not introduce new public endpoints, authentication/authorization behavior, direct data access, or executable use of untrusted input.
📝 WalkthroughWalkthrough
ChangesLLM provider detection and event emission refactor
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lib/crewai/src/crewai/llm.py (1)
642-653:⚠️ Potential issue | 🟠 Major | ⚡ Quick winPrefix mismatch between
_is_anthropic_modeland_matches_provider_pattern.
_matches_provider_patternincludes"anthropic."in its prefix list (line 459), but_is_anthropic_modeldoes not. Models likeanthropic.claude-3-sonnetwill be correctly routed butis_anthropicwill beFalse, causing incorrect message formatting in_format_messages_for_provider(line 2170).🐛 Proposed fix to add missing prefix
`@staticmethod` def _is_anthropic_model(model: str) -> bool: """Determine if the model is from Anthropic provider. Args: model: The model identifier string. Returns: bool: True if the model is from Anthropic, False otherwise. """ - anthropic_prefixes = ("anthropic/", "claude-", "claude/") + anthropic_prefixes = ("anthropic/", "anthropic.", "claude-", "claude/") return any(model.lower().startswith(prefix) for prefix in anthropic_prefixes)🤖 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/llm.py` around lines 642 - 653, The anthropic_prefixes tuple in the _is_anthropic_model method is missing the "anthropic." prefix (with a dot), which is included in the _matches_provider_pattern method. This causes models like anthropic.claude-3-sonnet to not be correctly identified as Anthropic models by _is_anthropic_model, leading to incorrect message formatting in _format_messages_for_provider. Add "anthropic." to the anthropic_prefixes tuple in _is_anthropic_model to match the prefix patterns used in _matches_provider_pattern.
🤖 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.
Outside diff comments:
In `@lib/crewai/src/crewai/llm.py`:
- Around line 642-653: The anthropic_prefixes tuple in the _is_anthropic_model
method is missing the "anthropic." prefix (with a dot), which is included in the
_matches_provider_pattern method. This causes models like
anthropic.claude-3-sonnet to not be correctly identified as Anthropic models by
_is_anthropic_model, leading to incorrect message formatting in
_format_messages_for_provider. Add "anthropic." to the anthropic_prefixes tuple
in _is_anthropic_model to match the prefix patterns used in
_matches_provider_pattern.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: a7929c21-930b-44fd-baf3-8ebb0752ea72
📒 Files selected for processing (1)
lib/crewai/src/crewai/llm.py
[BUG] Model naming prefixes filtering is too strict
The Anthropic model detection logic only checked for specific prefixes
["claude-", "anthropic."], which is too restrictive. Users with custom-deployed Anthropic models (e.g.,anthropic--claude-sonnet) or models with different naming conventions are incorrectly filtered out, causing incorrect behavior in model routing and provider-specific handling.Reported by: Community user
The model detection logic in
llm.pyused a hardcoded, narrow list of prefixes to identify Anthropic models. Custom-deployed models with non-standard prefixes (e.g.,anthropic--...or other variations) were not recognized as Anthropic models, leading to incorrect provider-specific message formatting and cache handling.File:
lib/crewai/src/crewai/llm.pyANTHROPIC_PREFIXESconstant:("anthropic/", "claude-", "claude/")is_anthropicfield to theLLMmodel class_is_anthropic_model()static method that checks if a model string starts with any of the Anthropic prefixes (case-insensitive)_validate_llm_fields()model validator (mode="before") that setsis_anthropicon model creation based on the model nameself.is_anthropicflag for broader detectionLow - The change broadens Anthropic model detection to recognize any model starting with
anthropic/,claude-, orclaude/. This correctly identifies more custom-deployed Anthropic models without false positives, since these prefixes are unique to Anthropic deployments.