Skip to content

fix(llm): detect Anthropic models with custom deployment naming#6190

Open
HumphreySun98 wants to merge 1 commit into
crewAIInc:mainfrom
HumphreySun98:fix/anthropic-model-detection-custom-naming
Open

fix(llm): detect Anthropic models with custom deployment naming#6190
HumphreySun98 wants to merge 1 commit into
crewAIInc:mainfrom
HumphreySun98:fix/anthropic-model-detection-custom-naming

Conversation

@HumphreySun98

@HumphreySun98 HumphreySun98 commented Jun 16, 2026

Copy link
Copy Markdown

Problem

Provider detection only recognized the canonical claude- / anthropic. prefixes. A self-deployed Anthropic model named e.g. anthropic--claude-3-5-sonnet was therefore:

  • mis-detected by _infer_provider_from_model, which defaulted unknown unprefixed ids to OpenAI, and
  • not matched by _matches_provider_pattern,

so it was routed to the OpenAI adapter, losing all Anthropic-specific request handling (stop-sequence normalization, thinking blocks, prompt caching, etc.).

Fix

  • Broaden the Anthropic pattern in _matches_provider_pattern to match claude anywhere or an anthropic prefix.
  • Let _infer_provider_from_model fall back to the Anthropic/Gemini patterns before defaulting to OpenAI.

anthropic is matched via startswith, so lookalikes like anthropomorphic are unaffected; broad patterns such as bedrock's bare . match are intentionally excluded to avoid false positives.

Tests

Extended test_validate_model_in_constants and added test_infer_provider_from_model_custom_naming in test_llm.py, including no-regression and no-false-positive cases. Verified failing-without/passing-with the fix; the 20 existing model-routing tests still pass; ruff and mypy clean.

Fixes #5893


This PR was authored with Claude Code. Per CONTRIBUTING.md, AI-generated contributions require the llm-generated label — I don't have triage permission to set it, so could a maintainer please add it? 🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved recognition of Anthropic Claude model names, now supporting custom deployment naming formats and names containing "claude" in any position.
    • Enhanced provider routing to better identify unprefixed Anthropic and Gemini model identifiers, reducing misrouting to OpenAI.
  • Tests

    • Added test coverage for custom model naming patterns and provider inference logic.

Provider detection only recognized the canonical "claude-" / "anthropic."
prefixes, so self-deployed Anthropic models named e.g. "anthropic--claude-..."
were silently routed to the OpenAI adapter (and unprefixed ids defaulted to
OpenAI outright), losing all Anthropic-specific request handling.

Broaden the Anthropic pattern in _matches_provider_pattern to match "claude"
anywhere or an "anthropic" prefix, and let _infer_provider_from_model fall
back to the Anthropic/Gemini patterns before defaulting to OpenAI. "anthropic"
is matched via startswith so lookalikes like "anthropomorphic" are unaffected;
broad patterns such as bedrock's bare "." match are intentionally excluded.

Fixes crewAIInc#5893

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

_matches_provider_pattern in LLM is updated to match any model name containing "claude" (not just strict "claude-" or "anthropic." prefixes). _infer_provider_from_model now probes Anthropic and Gemini patterns before defaulting to "openai". Tests are added to cover custom deployment naming variants.

Changes

Provider Pattern Matching Fix

Layer / File(s) Summary
Broadened Anthropic matching and unprefixed provider inference
lib/crewai/src/crewai/llm.py
_matches_provider_pattern returns True for any model name containing "claude" (e.g., anthropic--claude-..., anthropic.claude-...), replacing the previous strict prefix check. _infer_provider_from_model now calls _matches_provider_pattern for "anthropic" and "gemini" before falling back to "openai".
Tests for custom naming and provider routing
lib/crewai/tests/test_llm.py
test_validate_model_in_constants is extended to assert double-dash and dot Anthropic naming variants are accepted and that non-Anthropic lookalikes (e.g., anthropomorphic) are rejected. New test_infer_provider_from_model_custom_naming asserts anthropic resolution for custom variants, gemini for gemini-shaped ids, and openai fallback for unknown or genuine OpenAI ids.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 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 accurately summarizes the main change: fixing detection of Anthropic models with custom deployment naming conventions.
Linked Issues check ✅ Passed The PR directly addresses issue #5893 by updating the filtering functions to support wider model naming conventions while maintaining correct provider routing.
Out of Scope Changes check ✅ Passed All changes focus on broadening Anthropic model detection logic and testing; no unrelated modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 80.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 and usage tips.

@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 510-516: The provider detection logic at the diff location has
been expanded to check for `model_lower.startswith("anthropic")`, but the
`_is_anthropic_model` method does not include this same pattern matching logic.
Update the `_is_anthropic_model` method to mirror the expanded Anthropic
matching rule by adding a check for `model_lower.startswith("anthropic")`
alongside the existing pattern checks. This will ensure that both the provider
inference and the `is_anthropic` attribute remain consistent, allowing
Anthropic-specific message formatting in `_format_messages_for_provider` to be
properly applied.
🪄 Autofix (Beta)

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: d9473017-151b-42bc-97ed-06b1a168b440

📥 Commits

Reviewing files that changed from the base of the PR and between e9d568d and 4be3753.

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

Comment on lines 510 to 516
if provider == "anthropic" or provider == "claude":
return any(
model_lower.startswith(prefix) for prefix in ["claude-", "anthropic."]
)
# Match Anthropic models regardless of custom deployment naming
# (e.g. "anthropic--claude-...", "anthropic.claude-...") rather than
# only the canonical "claude-" / "anthropic." prefixes (#5893).
# "anthropomorphic" does not start with "anthropic", so it is safe.
return "claude" in model_lower or model_lower.startswith("anthropic")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Align Anthropic pattern logic with is_anthropic detection to prevent inconsistent routing behavior.

Line 515 expands Anthropic matching to startswith("anthropic"), but _is_anthropic_model (Line 707-717) does not mirror that rule. This creates inconsistent state where provider inference can resolve to Anthropic while self.is_anthropic stays False, skipping Anthropic-specific message handling in _format_messages_for_provider (Line 2326-2333).

Proposed fix
@@
     `@staticmethod`
     def _is_anthropic_model(model: str) -> bool:
@@
-        anthropic_prefixes = ("anthropic/", "claude-", "claude/")
-        return any(prefix in model.lower() for prefix in anthropic_prefixes)
+        model_lower = model.lower()
+        return "claude" in model_lower or model_lower.startswith("anthropic")
🤖 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 provider detection
logic at the diff location has been expanded to check for
`model_lower.startswith("anthropic")`, but the `_is_anthropic_model` method does
not include this same pattern matching logic. Update the `_is_anthropic_model`
method to mirror the expanded Anthropic matching rule by adding a check for
`model_lower.startswith("anthropic")` alongside the existing pattern checks.
This will ensure that both the provider inference and the `is_anthropic`
attribute remain consistent, allowing Anthropic-specific message formatting in
`_format_messages_for_provider` to be properly applied.

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