fix: add 'anthropic/' prefix to model pattern matching#6049
Conversation
Users with self-deployed Anthropic models often use naming conventions like 'anthropic/claude-...' (LiteLLM routing format). The existing pattern matching only recognized 'claude-' and 'anthropic.' prefixes, causing these models to be incorrectly filtered out. Fixes crewAIInc#5893
📝 WalkthroughWalkthroughExtended the Anthropic provider pattern matching in ChangesAnthropic Provider Pattern Matching
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes 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.
🧹 Nitpick comments (2)
lib/crewai/src/crewai/llm.py (2)
114-114: ⚖️ Poor tradeoffConsider using this constant to eliminate duplication.
The
ANTHROPIC_PREFIXESconstant is defined but not used in_matches_provider_pattern(line 459) or_is_anthropic_model(line 652), both of which define their own inline prefix lists. This leads to three different Anthropic prefix lists in the codebase:
- Line 114:
("anthropic/", "claude-", "claude/")- Line 459:
["claude-", "anthropic.", "anthropic/"]- Line 652:
("anthropic/", "claude-", "claude/")These lists have different contents ("claude/" vs "anthropic."), which could cause subtle bugs where one method recognizes a model but another doesn't.
Consider consolidating to a single source of truth, or documenting why different prefix sets are needed for different purposes.
🤖 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` at line 114, Replace the three inline Anthropic prefix lists with the single source-of-truth constant ANTHROPIC_PREFIXES: update _matches_provider_pattern and _is_anthropic_model to reference ANTHROPIC_PREFIXES (or a derived normalized set if a variant is required), and reconcile the differing entries ("anthropic.", "claude/") by deciding the canonical prefixes and documenting/normalizing them before use so all checks use the same prefix definitions.
652-653: ⚡ Quick winConsider using
startswithinstead ofinto avoid false positives.The substring match (
prefix in model.lower()) on line 653 is less precise than the prefix match (model_lower.startswith(prefix)) used in_matches_provider_pattern(line 458). This could cause false positives:
"my-provider/anthropic/claude-3"would incorrectly match as Anthropic"openai/custom-claude-like"would incorrectly match as AnthropicAdditionally, this method's prefix list
("anthropic/", "claude-", "claude/")differs from_matches_provider_pattern's list["claude-", "anthropic.", "anthropic/"]— notably missing the"anthropic."prefix while including"claude/".🔍 Proposed fix to use startswith and align with _matches_provider_pattern
`@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/") - return any(prefix in model.lower() for prefix in anthropic_prefixes) + model_lower = model.lower() + anthropic_prefixes = ("anthropic/", "claude-", "anthropic.") + 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 652 - 653, The current check uses substring matching which can yield false positives; in the function defining anthropic_prefixes and returning any(prefix in model.lower() ...), change to use a lower-cased model variable (e.g., model_lower = model.lower()) and use model_lower.startswith(prefix) instead of `in`, and align the prefix list with _matches_provider_pattern by using prefixes ["claude-", "anthropic.", "anthropic/"] (remove "claude/" and add "anthropic."). Ensure the updated logic still returns any(model_lower.startswith(prefix) for prefix in anthropic_prefixes).
🤖 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.
Nitpick comments:
In `@lib/crewai/src/crewai/llm.py`:
- Line 114: Replace the three inline Anthropic prefix lists with the single
source-of-truth constant ANTHROPIC_PREFIXES: update _matches_provider_pattern
and _is_anthropic_model to reference ANTHROPIC_PREFIXES (or a derived normalized
set if a variant is required), and reconcile the differing entries
("anthropic.", "claude/") by deciding the canonical prefixes and
documenting/normalizing them before use so all checks use the same prefix
definitions.
- Around line 652-653: The current check uses substring matching which can yield
false positives; in the function defining anthropic_prefixes and returning
any(prefix in model.lower() ...), change to use a lower-cased model variable
(e.g., model_lower = model.lower()) and use model_lower.startswith(prefix)
instead of `in`, and align the prefix list with _matches_provider_pattern by
using prefixes ["claude-", "anthropic.", "anthropic/"] (remove "claude/" and add
"anthropic."). Ensure the updated logic still returns
any(model_lower.startswith(prefix) for prefix in anthropic_prefixes).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 66726fda-e2dd-4bc7-9084-7a2b702581e8
📒 Files selected for processing (1)
lib/crewai/src/crewai/llm.py
Problem
Users with self-deployed Anthropic models use naming conventions like
anthropic/claude-...(the standard LiteLLM routing format). The existing_matches_provider_patternonly recognizedclaude-andanthropic.prefixes, causing these models to be incorrectly filtered out when explicitly settingprovider='anthropic'.Fixes #5893
Fix
Added
anthropic/to the recognized prefix list for the anthropic provider in_matches_provider_pattern:This is backwards-compatible and handles the common LiteLLM model naming convention.
Summary by CodeRabbit
anthropic/model name prefix format, in addition to existing naming patterns.