fix(llm): detect Anthropic models with custom deployment naming#6190
fix(llm): detect Anthropic models with custom deployment naming#6190HumphreySun98 wants to merge 1 commit into
Conversation
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>
📝 WalkthroughWalkthrough
ChangesProvider Pattern Matching Fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 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
📒 Files selected for processing (2)
lib/crewai/src/crewai/llm.pylib/crewai/tests/test_llm.py
| 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") | ||
|
|
There was a problem hiding this comment.
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.
Problem
Provider detection only recognized the canonical
claude-/anthropic.prefixes. A self-deployed Anthropic model named e.g.anthropic--claude-3-5-sonnetwas therefore:_infer_provider_from_model, which defaulted unknown unprefixed ids to OpenAI, and_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
_matches_provider_patternto matchclaudeanywhere or ananthropicprefix._infer_provider_from_modelfall back to the Anthropic/Gemini patterns before defaulting to OpenAI.anthropicis matched viastartswith, so lookalikes likeanthropomorphicare unaffected; broad patterns such as bedrock's bare.match are intentionally excluded to avoid false positives.Tests
Extended
test_validate_model_in_constantsand addedtest_infer_provider_from_model_custom_namingintest_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;ruffandmypyclean.Fixes #5893
This PR was authored with Claude Code. Per
CONTRIBUTING.md, AI-generated contributions require thellm-generatedlabel — I don't have triage permission to set it, so could a maintainer please add it? 🤖 Generated with Claude CodeSummary by CodeRabbit
Release Notes
Bug Fixes
Tests