Skip to content

fix(llm): infer provider from naming patterns and sync anthropic models - #6959

Open
prvthmpcypher wants to merge 1 commit into
crewAIInc:mainfrom
prvthmpcypher:fix/anthropic-models-routing
Open

fix(llm): infer provider from naming patterns and sync anthropic models#6959
prvthmpcypher wants to merge 1 commit into
crewAIInc:mainfrom
prvthmpcypher:fix/anthropic-models-routing

Conversation

@prvthmpcypher

Copy link
Copy Markdown

Summary

Fixes #6813.

Problem

  1. In 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 like claude-sonnet-4-6 or custom Anthropic/Gemini models to incorrectly route to the OpenAI native client.
  2. ANTHROPIC_MODELS and AnthropicModels literal in lib/crewai/src/crewai/llms/constants.py had drifted from lib/cli/src/crewai_cli/constants.py, missing claude-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

  • Updated LLM._infer_provider_from_model to fall through to _matches_provider_pattern for known providers before defaulting to "openai", as documented in its docstring.
  • Synchronized ANTHROPIC_MODELS with the CLI model list and updated the AnthropicModels type alias.
  • Added comprehensive unit tests in test_llm.py covering provider inference for Anthropic, Gemini, OpenAI, and custom prefix models.

Closes #6813

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The 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.

Changes

Provider inference and Claude model updates

Layer / File(s) Summary
Anthropic model declarations
lib/crewai/src/crewai/llms/constants.py
The Anthropic literal type and runtime list add seven Claude identifiers and remove two deprecated identifiers.
Provider pattern fallback
lib/crewai/src/crewai/llm.py
LLM._infer_provider_from_model uses provider-specific model-name patterns before defaulting unmatched models to OpenAI.
Provider inference validation
lib/crewai/tests/test_llm.py
Parameterized tests verify Anthropic, Gemini, and OpenAI provider resolution for representative model names.

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
Loading

Suggested reviewers: lorenzejay, greysonlalonde

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes provider inference and Anthropic model synchronization, which are the primary changes.
Description check ✅ Passed The description directly explains the provider inference fix, model-list synchronization, tests, and linked issue.
Linked Issues check ✅ Passed The changes satisfy issue #6813 by adding pattern inference, synchronizing Anthropic models, removing invalid IDs, and adding tests.
Out of Scope Changes check ✅ Passed All changes support issue #6813 and its stated objectives; no unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Warning

⚠️ This pull request shows signs of AI-generated slop (redundant_comments). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
lib/crewai/tests/test_llm.py (1)

1212-1229: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Exercise the new OpenAI pattern fallback.

The current cases do not include a clearly unlisted OpenAI model. They can pass through the hardcoded OPENAI_MODELS lookup 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*.py must 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

📥 Commits

Reviewing files that changed from the base of the PR and between 094b94e and 19a5b13.

📒 Files selected for processing (3)
  • lib/crewai/src/crewai/llm.py
  • lib/crewai/src/crewai/llms/constants.py
  • lib/crewai/tests/test_llm.py

Comment on lines +662 to +664
for provider in ["anthropic", "gemini", "bedrock", "azure", "openai"]:
if cls._matches_provider_pattern(model, provider):
return provider

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 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.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ANTHROPIC_MODELS has drifted from the CLI model list, so models offered by crewai create route to the OpenAI client

1 participant