Skip to content

fix: support custom model naming prefixes for anthropic provider (#5893)#6333

Open
C1-BA-B1-F3 wants to merge 2 commits into
crewAIInc:mainfrom
C1-BA-B1-F3:fix/model-prefix-filtering
Open

fix: support custom model naming prefixes for anthropic provider (#5893)#6333
C1-BA-B1-F3 wants to merge 2 commits into
crewAIInc:mainfrom
C1-BA-B1-F3:fix/model-prefix-filtering

Conversation

@C1-BA-B1-F3

@C1-BA-B1-F3 C1-BA-B1-F3 commented Jun 25, 2026

Copy link
Copy Markdown

Description

The model prefix filtering for the Anthropic provider was too strict, only matching claude- and anthropic. 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 claude and anthropic, supporting any naming convention for custom deployments.

Changes

  • lib/crewai/src/crewai/llm.py:
    • Updated _matches_provider_pattern to use substring matching for anthropic models
    • Updated _infer_provider_from_model to infer provider from model name patterns
  • lib/crewai/tests/test_llm.py: Added tests for custom prefix support

Testing

Added TestModelPrefixFiltering test class verifying that:

  • Custom prefixes like anthropic--claude- are recognized
  • Standard prefixes like claude- and anthropic. still work
  • Provider inference works for custom model names

Closes #5893

Summary by CodeRabbit

  • Bug Fixes
    • Improved provider detection for models with non-standard naming, with enhanced handling for Anthropic/Claude and similar variants.
    • Added broader fallback inference for custom deployment names, better mapping common prefixes/patterns (e.g., GPT, o1/o3/o4, Whisper) to the correct provider, with sensible defaults.
  • Tests
    • Added test coverage for both standard and custom model naming formats to verify provider matching and inference behavior.

…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

@corridor-security corridor-security 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.

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.

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b5b856da-9101-43e9-9587-4997afff1768

📥 Commits

Reviewing files that changed from the base of the PR and between a9d2f85 and cdb9327.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/llm.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/crewai/src/crewai/llm.py

📝 Walkthrough

Walkthrough

The PR broadens Anthropic model-name handling in LLM by matching custom claude or anthropic names and adding fallback provider inference for custom deployments. Tests cover the updated matching and inference rules.

Changes

Anthropic model prefix detection

Layer / File(s) Summary
Anthropic matching and fallback inference
lib/crewai/src/crewai/llm.py
_matches_provider_pattern accepts Anthropic model names containing claude or anthropic, _infer_provider_from_model adds custom-name provider inference, and _is_anthropic_model uses the same broader detection rule.
Prefix filtering tests
lib/crewai/tests/test_llm.py
New tests cover custom and standard Anthropic model-name patterns and inference from a custom Anthropic deployment name.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: supporting custom Anthropic model naming prefixes.
Linked Issues check ✅ Passed The changes address the issue by accepting custom Anthropic names, preserving standard prefixes, and adding tests for both.
Out of Scope Changes check ✅ Passed The extra inference tweaks stay within the same provider-detection area and support the prefix-matching fix.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

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

🧹 Nitpick comments (2)
lib/crewai/src/crewai/llm.py (2)

629-630: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

OpenAI prefix list here diverges from _matches_provider_pattern.

_matches_provider_pattern recognizes o4 and whisper- for OpenAI, but this inference branch only checks gpt-/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 tradeoff

Anthropic 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 like anthropic-custom (matches here, but is_anthropic stays False), which would skip the Anthropic-specific message formatting in _format_messages_for_provider. Your target case anthropic--claude-... happens to satisfy both (via the claude- 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

📥 Commits

Reviewing files that changed from the base of the PR and between 01fc389 and a9d2f85.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/llm.py
  • lib/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.
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.

[BUG] Model naming prefixes filtering is too strict

1 participant