fix: support custom model naming prefixes for anthropic provider (#5893)#6333
fix: support custom model naming prefixes for anthropic provider (#5893)#6333C1-BA-B1-F3 wants to merge 2 commits into
Conversation
…wAIInc#5893) The model prefix filtering was too strict, only matching 'claude-' and 'anthropic.' prefixes. Custom deployments with non-standard naming (e.g., 'anthropic--claude-...') were incorrectly filtered out. Now uses substring matching for 'claude' and 'anthropic' to support any naming convention. Also adds pattern-based inference in _infer_provider_from_model for custom model names. Closes crewAIInc#5893
There was a problem hiding this comment.
Summary: This PR changes LLM provider pattern matching and inference for Anthropic/custom model names, with no new authentication, authorization, data handling, or external request surfaces introduced.
Risk: Low risk. No exploitable security vulnerabilities were identified because the changes only affect local model-provider selection logic and tests, without exposing attacker-controlled resource access or security boundaries.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe PR broadens Anthropic model-name handling in ChangesAnthropic model prefix detection
🚥 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)
629-630: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueOpenAI prefix list here diverges from
_matches_provider_pattern.
_matches_provider_patternrecognizeso4andwhisper-for OpenAI, but this inference branch only checksgpt-/o1/o3. The end result is unaffected since the function defaults to"openai"anyway, but the asymmetry is easy to misread. Optional: align the prefix sets (or share a constant) so the two stay in sync.🤖 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 629 - 630, The OpenAI provider inference in the LLM model-prefix branch is out of sync with `_matches_provider_pattern`, making the logic harder to read and maintain. Update the provider detection in the `LLM`/`_matches_provider_pattern` path so the OpenAI prefixes are aligned (including `o4` and `whisper-`) or extract the shared prefix list into a single constant used by both checks. Keep the behavior consistent with the existing `model_lower` matching logic and the `"openai"` return path.
510-516: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoffAnthropic detection now diverges across two helpers.
This broadens matching to any model containing
claude/anthropic, but_is_anthropic_model(Lines 717-718) still detects via the fixed prefixes("anthropic/", "claude-", "claude/"). The two now disagree for names likeanthropic-custom(matches here, butis_anthropicstaysFalse), which would skip the Anthropic-specific message formatting in_format_messages_for_provider. Your target caseanthropic--claude-...happens to satisfy both (via theclaude-substring), so it's not broken today, but the divergence is fragile.Consider routing both through a single shared predicate to keep custom-deployment handling consistent.
🤖 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 510 - 516, The Anthropic model detection logic is inconsistent between the provider check in llm.py and the shared helper _is_anthropic_model, which can cause custom Anthropic deployments to be classified differently and skip Anthropic-specific formatting in _format_messages_for_provider. Update the detection paths to use one shared predicate (or make _is_anthropic_model cover the same cases as the provider-level check) so is_anthropic and the provider matching stay aligned for custom model names like anthropic-custom and anthropic--claude-...
🤖 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`:
- Around line 629-630: The OpenAI provider inference in the LLM model-prefix
branch is out of sync with `_matches_provider_pattern`, making the logic harder
to read and maintain. Update the provider detection in the
`LLM`/`_matches_provider_pattern` path so the OpenAI prefixes are aligned
(including `o4` and `whisper-`) or extract the shared prefix list into a single
constant used by both checks. Keep the behavior consistent with the existing
`model_lower` matching logic and the `"openai"` return path.
- Around line 510-516: The Anthropic model detection logic is inconsistent
between the provider check in llm.py and the shared helper _is_anthropic_model,
which can cause custom Anthropic deployments to be classified differently and
skip Anthropic-specific formatting in _format_messages_for_provider. Update the
detection paths to use one shared predicate (or make _is_anthropic_model cover
the same cases as the provider-level check) so is_anthropic and the provider
matching stay aligned for custom model names like anthropic-custom and
anthropic--claude-...
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 2aa7dc3b-6f4c-4e41-bf9b-45854311872b
📒 Files selected for processing (2)
lib/crewai/src/crewai/llm.pylib/crewai/tests/test_llm.py
- Add o4 and whisper- prefixes to OpenAI inference branch to match _matches_provider_pattern - Broaden _is_anthropic_model to use substring matching consistent with _matches_provider_pattern, so custom deployments like 'anthropic-custom' are detected correctly by both methods Addresses CodeRabbit review comments on PR crewAIInc#6333.
Description
The model prefix filtering for the Anthropic provider was too strict, only matching
claude-andanthropic.prefixes. Custom deployments with non-standard naming conventions (e.g.,anthropic--claude-...) were incorrectly filtered out.This PR makes the prefix matching more flexible by using substring matching for
claudeandanthropic, supporting any naming convention for custom deployments.Changes
lib/crewai/src/crewai/llm.py:_matches_provider_patternto use substring matching for anthropic models_infer_provider_from_modelto infer provider from model name patternslib/crewai/tests/test_llm.py: Added tests for custom prefix supportTesting
Added
TestModelPrefixFilteringtest class verifying that:anthropic--claude-are recognizedclaude-andanthropic.still workCloses #5893
Summary by CodeRabbit