Skip to content

fix(llm): infer native provider from name patterns for unprefixed models - #6823

Open
Anai-Guo wants to merge 1 commit into
crewAIInc:mainfrom
Anai-Guo:fix/infer-provider-pattern-fallback
Open

fix(llm): infer native provider from name patterns for unprefixed models#6823
Anai-Guo wants to merge 1 commit into
crewAIInc:mainfrom
Anai-Guo:fix/infer-provider-pattern-fallback

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Aug 5, 2026

Copy link
Copy Markdown

What

LLM._infer_provider_from_model checked only the hardcoded constants lists (OPENAI_MODELS, ANTHROPIC_MODELS, …) and then defaulted to "openai". Its docstring promises a pattern-matching fallback:

This method first checks the hardcoded constants list for known models. If not found, it uses pattern matching to infer the provider from model name patterns.

…but no pattern matching ever ran. The sibling _validate_model_in_constants already falls through to _matches_provider_pattern; this method never did.

Impact

A bare Anthropic id that is not yet in ANTHROPIC_MODELS — e.g. claude-sonnet-4-6, offered near the top of the crewai create Anthropic menu — was inferred as "openai", so LLM(model="claude-sonnet-4-6") silently constructed an OpenAICompletion instead of AnthropicCompletion, with no error.

Fix

Fall through to _matches_provider_pattern for the providers whose prefixes are unambiguous (anthropic, gemini) before defaulting to openai. bedrock ("." in name) and azure (gpt-/o-series) are intentionally excluded here because their patterns overlap other providers and would reroute models that currently resolve to openai; those remain on the explicit-provider path. Models already present in the constants lists are unaffected (they return before the new fallback).

Test

Adds test_infer_provider_from_model_falls_back_to_patterns, mirroring the existing test_validate_model_in_constants: in-constants ids keep resolving as before, claude-sonnet-4-6 / claude-future-5 / gemma-3-latest now resolve to their real provider, and a non-matching id still defaults to openai.

Fixes #6813

🤖 Generated with Claude Code

_infer_provider_from_model checked only the hardcoded constants lists and
then defaulted to "openai", despite its docstring promising a pattern-
matching fallback. Its sibling _validate_model_in_constants already falls
through to _matches_provider_pattern; this method never did.

As a result a bare Anthropic id that is not yet in ANTHROPIC_MODELS (e.g.
claude-sonnet-4-6, offered near the top of the crewai create picker) was
inferred as "openai" and constructed an OpenAI client with no error.

Fall through to _matches_provider_pattern for the providers whose prefixes
are unambiguous (anthropic, gemini) before defaulting to openai. Bedrock
and azure are intentionally excluded because their patterns overlap other
providers. In-constants ids are unaffected.

Fixes crewAIInc#6813
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Provider inference

Layer / File(s) Summary
Pattern fallback and regression coverage
lib/crewai/src/crewai/llm.py, lib/crewai/tests/test_llm.py
LLM._infer_provider_from_model now infers Anthropic and Gemini providers from model naming patterns after known-model lookup. Bedrock and Azure patterns remain excluded from this fallback. Tests cover known models, unlisted Claude and Gemini models, and the OpenAI default.

Suggested reviewers: thecybertech

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: enabling pattern-matching fallback for unprefixed Anthropic and Gemini models in the provider inference method.
Description check ✅ Passed The description clearly explains the problem, fix, and test coverage. It details that pattern matching was missing from _infer_provider_from_model despite its docstring promise, and now falls back to _matches_provider_pattern for unambiguous providers.
Linked Issues check ✅ Passed The PR fully addresses issue #6813 by implementing pattern-matching fallback in _infer_provider_from_model for Anthropic and Gemini models, preventing incorrect OpenAI routing for unprefixed models like claude-sonnet-4-6.
Out of Scope Changes check ✅ Passed All changes are scoped to implementing pattern-matching fallback for provider inference and adding corresponding test coverage. No unrelated modifications are present.
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.

@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

🤖 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-671: Exclude models beginning with anthropic. from the
Anthropic fallback in _matches_provider_pattern, preserving the
explicit-provider path for Bedrock identifiers; add a regression assertion in
lib/crewai/tests/test_llm.py lines 992-998 covering an unlisted
anthropic.claude-... identifier and its expected provider resolution.
🪄 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: 907a6739-73fc-456c-8608-447abc9ac8a7

📥 Commits

Reviewing files that changed from the base of the PR and between cfb1ed2 and 514f461.

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

Comment on lines +662 to +671
# Models absent from the constants lists (new releases, "latest"
# aliases) still fall back to provider naming patterns, as this
# method's docstring describes and the sibling
# _validate_model_in_constants already does. Only providers whose
# prefixes are unambiguous are checked here; bedrock ("." in name)
# and azure (gpt-/o-series) patterns overlap other providers and are
# left to the explicit-provider path.
for provider in ("anthropic", "gemini"):
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

Exclude anthropic. Bedrock identifiers from the Anthropic fallback.

_matches_provider_pattern(model, "anthropic") accepts identifiers that start with anthropic.. Therefore, an unlisted Bedrock ID such as anthropic.claude-future-v1:0 routes to the native Anthropic client instead of remaining on the explicit-provider path and then the OpenAI default. This contradicts the stated Bedrock exclusion.

  • lib/crewai/src/crewai/llm.py#L662-L671: skip the Anthropic pattern fallback when the model starts with anthropic..
  • lib/crewai/tests/test_llm.py#L992-L998: add a regression assertion for an unlisted anthropic.claude-... Bedrock-shaped ID.
📍 Affects 2 files
  • lib/crewai/src/crewai/llm.py#L662-L671 (this comment)
  • lib/crewai/tests/test_llm.py#L992-L998
🤖 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 - 671, Exclude models
beginning with anthropic. from the Anthropic fallback in
_matches_provider_pattern, preserving the explicit-provider path for Bedrock
identifiers; add a regression assertion in lib/crewai/tests/test_llm.py lines
992-998 covering an unlisted anthropic.claude-... identifier and its expected
provider resolution.

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