fix(llm): infer native provider from name patterns for unprefixed models - #6823
fix(llm): infer native provider from name patterns for unprefixed models#6823Anai-Guo wants to merge 1 commit into
Conversation
_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
📝 WalkthroughWalkthroughChangesProvider inference
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 |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
lib/crewai/src/crewai/llm.pylib/crewai/tests/test_llm.py
| # 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 |
There was a problem hiding this comment.
🎯 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 withanthropic..lib/crewai/tests/test_llm.py#L992-L998: add a regression assertion for an unlistedanthropic.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.
What
LLM._infer_provider_from_modelchecked only the hardcoded constants lists (OPENAI_MODELS,ANTHROPIC_MODELS, …) and then defaulted to"openai". Its docstring promises a pattern-matching fallback:…but no pattern matching ever ran. The sibling
_validate_model_in_constantsalready 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 thecrewai createAnthropic menu — was inferred as"openai", soLLM(model="claude-sonnet-4-6")silently constructed anOpenAICompletioninstead ofAnthropicCompletion, with no error.Fix
Fall through to
_matches_provider_patternfor the providers whose prefixes are unambiguous (anthropic,gemini) before defaulting toopenai.bedrock("." in name) andazure(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 existingtest_validate_model_in_constants: in-constants ids keep resolving as before,claude-sonnet-4-6/claude-future-5/gemma-3-latestnow resolve to their real provider, and a non-matching id still defaults toopenai.Fixes #6813
🤖 Generated with Claude Code