fix(llm): infer provider from naming patterns and sync anthropic models - #6959
fix(llm): infer provider from naming patterns and sync anthropic models#6959prvthmpcypher wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThe change adds provider-pattern fallback to LLM provider inference, updates Anthropic model declarations with current Claude identifiers, removes deprecated identifiers, and adds parameterized provider-resolution tests. ChangesProvider inference and Claude model updates
Sequence Diagram(s)sequenceDiagram
participant Caller
participant LLM._infer_provider_from_model
participant _matches_provider_pattern
participant _get_native_provider
Caller->>LLM._infer_provider_from_model: provide model name
LLM._infer_provider_from_model->>_matches_provider_pattern: match unknown model name
_matches_provider_pattern-->>LLM._infer_provider_from_model: return inferred provider
LLM._infer_provider_from_model->>_get_native_provider: select inferred provider
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 Warning |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
lib/crewai/tests/test_llm.py (1)
1212-1229: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winExercise the new OpenAI pattern fallback.
The current cases do not include a clearly unlisted OpenAI model. They can pass through the hardcoded
OPENAI_MODELSlookup without testing the new pattern branch. Add unlisted GPT identifiers with and without a dot, plus one unmatched model for the documented OpenAI default.As per coding guidelines,
**/*test*.pymust write unit tests for new functionality, focusing on behavior rather than implementation details.Proposed regression cases
("o3-mini", "openai"), + ("gpt-future-6", "openai"), + ("gpt-future-6.1", "openai"), + ("custom-model-v1", "openai"),🤖 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/tests/test_llm.py` around lines 1212 - 1229, Add cases to test_infer_provider_from_model for unlisted GPT identifiers both with and without a dot, ensuring they resolve to "openai" through the fallback, and add one unmatched model that verifies the documented default also resolves to "openai".Source: Coding guidelines
🤖 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/llm.py`:
- Around line 662-664: Update the provider iteration in LLM provider resolution
so OpenAI-specific matching takes precedence over Azure and Bedrock for bare or
dotted GPT/o-series model names. Ensure generic gpt-* and o* models resolve to
"openai", while preserving explicit Azure and Bedrock matches for models
uniquely identified by those providers.
---
Nitpick comments:
In `@lib/crewai/tests/test_llm.py`:
- Around line 1212-1229: Add cases to test_infer_provider_from_model for
unlisted GPT identifiers both with and without a dot, ensuring they resolve to
"openai" through the fallback, and add one unmatched model that verifies the
documented default also resolves to "openai".
🪄 Autofix
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: 2c39e75d-de37-49b7-9a0f-3165754fa830
📒 Files selected for processing (3)
lib/crewai/src/crewai/llm.pylib/crewai/src/crewai/llms/constants.pylib/crewai/tests/test_llm.py
| for provider in ["anthropic", "gemini", "bedrock", "azure", "openai"]: | ||
| if cls._matches_provider_pattern(model, provider): | ||
| return provider |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Evaluate specific provider patterns before broad matches.
For an unlisted gpt-* model, the Azure pattern matches before the OpenAI pattern. gpt-future-6 therefore resolves to "azure". A dotted unlisted GPT model resolves to "bedrock" because the Bedrock pattern accepts any model containing "." and runs first. LLM.__new__ then selects the wrong native provider.
Evaluate OpenAI before Azure and Bedrock for bare model names. Reserve generic gpt-* and o* matching for OpenAI, or make the Azure and Bedrock patterns mutually exclusive.
Proposed ordering fix
- for provider in ["anthropic", "gemini", "bedrock", "azure", "openai"]:
+ for provider in ["anthropic", "gemini", "openai", "azure", "bedrock"]:📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| for provider in ["anthropic", "gemini", "bedrock", "azure", "openai"]: | |
| if cls._matches_provider_pattern(model, provider): | |
| return provider | |
| for provider in ["anthropic", "gemini", "openai", "azure", "bedrock"]: | |
| if cls._matches_provider_pattern(model, provider): | |
| return provider |
🤖 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 662 - 664, Update the provider
iteration in LLM provider resolution so OpenAI-specific matching takes
precedence over Azure and Bedrock for bare or dotted GPT/o-series model names.
Ensure generic gpt-* and o* models resolve to "openai", while preserving
explicit Azure and Bedrock matches for models uniquely identified by those
providers.
Summary
Fixes #6813.
Problem
LLM._infer_provider_from_model, model provider resolution only checked hardcoded constant lists and defaulted directly to"openai"without calling_matches_provider_pattern(). This caused models likeclaude-sonnet-4-6or custom Anthropic/Gemini models to incorrectly route to the OpenAI native client.ANTHROPIC_MODELSandAnthropicModelsliteral inlib/crewai/src/crewai/llms/constants.pyhad drifted fromlib/cli/src/crewai_cli/constants.py, missingclaude-opus-4-6,claude-sonnet-4-6,claude-opus-4-7,claude-opus-4-8,claude-opus-5,claude-sonnet-5,claude-fable-5, and contained two invalid IDs (claude-4-sonnet-20250514,claude-4-opus-20250514).Solution
LLM._infer_provider_from_modelto fall through to_matches_provider_patternfor known providers before defaulting to"openai", as documented in its docstring.ANTHROPIC_MODELSwith the CLI model list and updated theAnthropicModelstype alias.test_llm.pycovering provider inference for Anthropic, Gemini, OpenAI, and custom prefix models.Closes #6813