Skip to content

Fix #5893 - Broaden Anthropic model prefix detection for custom-deployed models#6240

Closed
Whning0513 wants to merge 1 commit into
crewAIInc:mainfrom
Whning0513:fix-issue-5893
Closed

Fix #5893 - Broaden Anthropic model prefix detection for custom-deployed models#6240
Whning0513 wants to merge 1 commit into
crewAIInc:mainfrom
Whning0513:fix-issue-5893

Conversation

@Whning0513

@Whning0513 Whning0513 commented Jun 19, 2026

Copy link
Copy Markdown

[BUG] Model naming prefixes filtering is too strict

The Anthropic model detection logic only checked for specific prefixes ["claude-", "anthropic."], which is too restrictive. Users with custom-deployed Anthropic models (e.g., anthropic--claude-sonnet) or models with different naming conventions are incorrectly filtered out, causing incorrect behavior in model routing and provider-specific handling.

Reported by: Community user

The model detection logic in llm.py used a hardcoded, narrow list of prefixes to identify Anthropic models. Custom-deployed models with non-standard prefixes (e.g., anthropic--... or other variations) were not recognized as Anthropic models, leading to incorrect provider-specific message formatting and cache handling.

File: lib/crewai/src/crewai/llm.py

  • Added ANTHROPIC_PREFIXES constant: ("anthropic/", "claude-", "claude/")
  • Added is_anthropic field to the LLM model class
  • Added _is_anthropic_model() static method that checks if a model string starts with any of the Anthropic prefixes (case-insensitive)
  • Added _validate_llm_fields() model validator (mode="before") that sets is_anthropic on model creation based on the model name
  • The existing anthropic-specific handling in message processing now uses the self.is_anthropic flag for broader detection

Low - The change broadens Anthropic model detection to recognize any model starting with anthropic/, claude-, or claude/. This correctly identifies more custom-deployed Anthropic models without false positives, since these prefixes are unique to Anthropic deployments.

  • Bug Fixes
    • Improved Anthropic/Claude model detection for more accurate provider routing.
    • Refined completion event handling in streaming and non-streaming responses.

@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 adjusts LLM provider detection and call-start event emission for Anthropic/custom Claude model handling; no exploitable security vulnerabilities were identified.

Risk: Low risk. The changes do not introduce new public endpoints, authentication/authorization behavior, direct data access, or executable use of untrusted input.

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

llm.py receives three sets of changes: Anthropic/Claude model prefix matching is widened in _matches_provider_pattern and _is_anthropic_model; call() and acall() are refactored to emit LLMCallStartedEvent directly from llm_call_context() instead of via _emit_call_started_event; and finish_reason/response_id extraction and propagation is removed from all streaming and non-streaming response paths and from the _handle_emit_call_events signature.

Changes

LLM provider detection and event emission refactor

Layer / File(s) Summary
Anthropic/Claude model prefix matching expansion
lib/crewai/src/crewai/llm.py
_matches_provider_pattern gains additional accepted prefix variants; _is_anthropic_model switches to startswith against a fixed tuple, widening detection to custom-deployed Claude model names (addresses strict prefix filtering bug).
LLMCallStartedEvent inline emission and _handle_emit_call_events signature
lib/crewai/src/crewai/llm.py
Adds LLMCallStartedEvent import, removes extract_choices_finish_reason_and_id import; call() and acall() emit LLMCallStartedEvent directly with call_id from llm_call_context() instead of delegating to _emit_call_started_event; _handle_emit_call_events drops finish_reason and response_id parameters and docstring fields.
Remove finish_reason/response_id from all streaming and non-streaming paths
lib/crewai/src/crewai/llm.py
Sync and async streaming handlers no longer accumulate per-chunk finish_reason/response_id; sync and async non-streaming structured and plain-text branches no longer extract or pass those values into _handle_emit_call_events; all completion and partial-response error paths updated accordingly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR includes additional changes to event emission logic (LLMCallStartedEvent and LLMCallCompletedEvent) that are not mentioned in issue #5893 or PR objectives. Remove or justify the event emission refactoring changes, or document them as part of a separate issue/objective in the PR description.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: broadening Anthropic model prefix detection for custom-deployed models.
Linked Issues check ✅ Passed The PR addresses issue #5893 by implementing expanded prefix detection for Anthropic models, directly resolving the reported problem with custom-deployed models.
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 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lib/crewai/src/crewai/llm.py (1)

642-653: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Prefix mismatch between _is_anthropic_model and _matches_provider_pattern.

_matches_provider_pattern includes "anthropic." in its prefix list (line 459), but _is_anthropic_model does not. Models like anthropic.claude-3-sonnet will be correctly routed but is_anthropic will be False, causing incorrect message formatting in _format_messages_for_provider (line 2170).

🐛 Proposed fix to add missing prefix
     `@staticmethod`
     def _is_anthropic_model(model: str) -> bool:
         """Determine if the model is from Anthropic provider.

         Args:
             model: The model identifier string.

         Returns:
             bool: True if the model is from Anthropic, False otherwise.
         """
-        anthropic_prefixes = ("anthropic/", "claude-", "claude/")
+        anthropic_prefixes = ("anthropic/", "anthropic.", "claude-", "claude/")
         return any(model.lower().startswith(prefix) for prefix in anthropic_prefixes)
🤖 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 642 - 653, The anthropic_prefixes
tuple in the _is_anthropic_model method is missing the "anthropic." prefix (with
a dot), which is included in the _matches_provider_pattern method. This causes
models like anthropic.claude-3-sonnet to not be correctly identified as
Anthropic models by _is_anthropic_model, leading to incorrect message formatting
in _format_messages_for_provider. Add "anthropic." to the anthropic_prefixes
tuple in _is_anthropic_model to match the prefix patterns used in
_matches_provider_pattern.
🤖 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.

Outside diff comments:
In `@lib/crewai/src/crewai/llm.py`:
- Around line 642-653: The anthropic_prefixes tuple in the _is_anthropic_model
method is missing the "anthropic." prefix (with a dot), which is included in the
_matches_provider_pattern method. This causes models like
anthropic.claude-3-sonnet to not be correctly identified as Anthropic models by
_is_anthropic_model, leading to incorrect message formatting in
_format_messages_for_provider. Add "anthropic." to the anthropic_prefixes tuple
in _is_anthropic_model to match the prefix patterns used in
_matches_provider_pattern.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a7929c21-930b-44fd-baf3-8ebb0752ea72

📥 Commits

Reviewing files that changed from the base of the PR and between 854c67d and 36ebebe.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/llm.py

@Whning0513

Copy link
Copy Markdown
Author

This is superseded by the earlier #5894 and the related prefix work in #5914. This branch also contains unrelated event-emission refactoring, so I am closing it to keep the provider-prefix fix focused in the earlier PRs. Sorry for the duplicate submission.

@Whning0513 Whning0513 closed this Jul 19, 2026
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.

1 participant