Skip to content

Fix #5808: Add support for Claude 4.7 Opus (no assistant prefill, drop temperature)#5811

Open
devin-ai-integration[bot] wants to merge 3 commits into
mainfrom
devin/1778762278-fix-claude-4.7-opus-support
Open

Fix #5808: Add support for Claude 4.7 Opus (no assistant prefill, drop temperature)#5811
devin-ai-integration[bot] wants to merge 3 commits into
mainfrom
devin/1778762278-fix-claude-4.7-opus-support

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot commented May 14, 2026

Summary

Fixes #5808 — Claude 4.7 Opus (and Claude 4.6+ models) broke CrewAI because:

  1. These models reject requests where the last message has the assistant role (no assistant message prefill support)
  2. These models reject the temperature parameter

Changes

BaseLLM (lib/crewai/src/crewai/llms/base_llm.py):

  • Added supports_assistant_prefill() method defaulting to True so existing providers are unaffected

AnthropicCompletion (lib/crewai/src/crewai/llms/providers/anthropic/completion.py):

  • Added supports_assistant_prefill() override using _is_no_prefill_model() regex-based detection (Claude 4.6+ / 5.0+)
  • Temperature parameter is now conditionally dropped for models that don't support it

LLM (litellm fallback, lib/crewai/src/crewai/llm.py):

  • Added supports_assistant_prefill() using litellm.get_model_info() with fallback to name-based heuristic

CrewAgentExecutor (lib/crewai/src/crewai/agents/crew_agent_executor.py):

  • Added supports_prefill property using getattr() for backward compatibility with custom LLM adapters
  • Modified _append_message() to split observation into a user-role message when the model doesn't support prefill, ensuring the conversation never ends on an assistant turn

agent_utils.py (lib/crewai/src/crewai/utilities/agent_utils.py):

  • Modified handle_max_iterations_exceeded() to use user role for the forced-answer message when the model doesn't support prefill

Tests

24 new tests in lib/crewai/tests/agents/test_claude_opus_4_7_support.py covering:

  • BaseLLM default behavior
  • Anthropic model detection (claude-opus-4-7, claude-sonnet-4-6, claude-3-5-sonnet, etc.)
  • Temperature dropping for no-prefill models
  • LiteLLM model_info detection and heuristic fallback
  • Message splitting logic (with/without observations)
  • Backward compatibility for custom LLM adapters
  • handle_max_iterations_exceeded role selection

Review & Testing Checklist for Human

  • Verify Claude 4.7 Opus works end-to-end with a real agent task (the core issue from [FEATURE] Support for new Anthropic model: Claude-4.7-Opus #5808)
  • Verify older Claude models (3.5 Sonnet, 3 Opus) still work correctly with assistant prefill
  • Check that the regex claude.*?(\d+)[.-](\d+) correctly matches all Anthropic naming patterns
  • Verify temperature is properly omitted for claude-opus-4-7 but kept for older models
  • Test with a custom LLM adapter that doesn't have supports_assistant_prefill to confirm backward compatibility

Notes

Link to Devin session: https://app.devin.ai/sessions/91e01d2a89e04f188e77f8cb39edb2d8

Summary by CodeRabbit

  • New Features
    • Added support and automatic detection for models that don't accept assistant-message prefill (e.g., Claude 4.7+).
  • Bug Fixes
    • Adjusted conversation handling to avoid ending on an assistant turn for no-prefill models and to omit incompatible parameters (e.g., temperature) where required.
  • Tests
    • Added tests covering prefill detection, parameter gating, and conversation/continuation behaviors.

Review Change Stack

…ure)

Fixes #5808

- Add supports_assistant_prefill() to BaseLLM, LLM (litellm), and
  AnthropicCompletion
- Claude 4.6+ models reject assistant-role messages as the last message
  in a conversation; split observation into a user-role message instead
- Drop temperature parameter for Claude 4.6+ (not supported)
- Update handle_max_iterations_exceeded to use user-role message for
  no-prefill models
- Use getattr() for backward compatibility with custom LLM adapters
- Add 24 tests covering prefill detection, temperature dropping, message
  splitting, and backward compatibility

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

Prompt hidden (unlisted session)

@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

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: ce9b7390-e194-4bbb-86c0-e6d97e64e423

📥 Commits

Reviewing files that changed from the base of the PR and between 7c68e0e and 6c26461.

📒 Files selected for processing (3)
  • lib/crewai/src/crewai/llm.py
  • lib/crewai/src/crewai/llms/providers/anthropic/completion.py
  • lib/crewai/tests/agents/test_claude_opus_4_7_support.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • lib/crewai/src/crewai/llms/providers/anthropic/completion.py
  • lib/crewai/src/crewai/llm.py
  • lib/crewai/tests/agents/test_claude_opus_4_7_support.py

📝 Walkthrough

Walkthrough

Adds model-aware assistant-prefill detection and handling: BaseLLM gains a capability method; Anthropic provider detects Claude 4.6+ and strips temperature; LLM queries LiteLLM metadata with a Claude-version fallback; executor and utilities rewrite assistant messages to avoid final assistant turns; tests added.

Changes

Claude 4.7 Opus prefill support

Layer / File(s) Summary
Base capability contract in BaseLLM
lib/crewai/src/crewai/llms/base_llm.py
BaseLLM adds a supports_assistant_prefill() method defaulting to True, providing an overridable interface for providers to signal whether assistant-role message prefill is accepted.
Anthropic model detection and parameter adjustment
lib/crewai/src/crewai/llms/providers/anthropic/completion.py
AnthropicCompletion detects Claude 4.6+ models via regex-based version parsing, implements supports_assistant_prefill() and _is_no_prefill_model() helpers, and conditionally omits the temperature parameter for detected no-prefill models.
LiteLLM model info integration
lib/crewai/src/crewai/llm.py
LLM adds supports_assistant_prefill() that queries litellm.get_model_info() for provider-specific metadata and falls back to a Claude-version heuristic (Claude 4.6+ treated as no-prefill) when lookup fails.
CrewAgentExecutor message restructuring
lib/crewai/src/crewai/agents/crew_agent_executor.py
CrewAgentExecutor exposes supports_prefill and updates _append_message to restructure assistant messages for no-prefill models: split Observation: into a separate user message or append a continuation user prompt so the conversation doesn't end on an assistant turn.
Forced final answer role selection
lib/crewai/src/crewai/utilities/agent_utils.py
handle_max_iterations_exceeded now queries llm.supports_assistant_prefill() and appends the forced final-answer message as role="user" when prefill is unsupported (or assistant when supported).
Comprehensive test suite
lib/crewai/tests/agents/test_claude_opus_4_7_support.py
Adds tests for BaseLLM default, Anthropic model parsing, temperature removal for no-prefill models, LiteLLM metadata integration and fallback, _append_message restructuring, and handle_max_iterations_exceeded role selection.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

size/M, enhancement

Suggested reviewers

  • greysonlalonde

Poem

A rabbit hops through Claude's new halls, 🐰
Where Opus 4.7 now answers calls,
No prefill? No problem — split what it said,
Assistant then user, the flow keeps ahead,
Hopping on code, we keep conversations tall!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.52% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 accurately reflects the main changes: adding support for Claude 4.7 Opus with fixes for assistant prefill and temperature parameter handling.
Linked Issues check ✅ Passed The PR comprehensively addresses both requirements from issue #5808: it implements assistant message prefill detection and handling across all LLM components, and conditionally omits the temperature parameter for no-prefill models.
Out of Scope Changes check ✅ Passed All changes are directly scoped to addressing Claude 4.7 Opus compatibility: model detection logic, temperature parameter handling, message role management, and comprehensive test coverage for the feature.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch devin/1778762278-fix-claude-4.7-opus-support

Comment @coderabbitai help to get the list of available commands and usage tips.

Co-Authored-By: João <joao@crewai.com>

from __future__ import annotations

from typing import Literal
from unittest.mock import MagicMock, patch

from crewai.llms.base_llm import BaseLLM
from crewai.utilities.agent_utils import format_message_for_llm
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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 2295-2302: The fallback heuristic that inspects Claude model IDs
(the model_lower logic using re.search r"claude.*?(\d+)[.-](\d+)") can
accidentally treat date suffixes as the minor version; change the regex to
require the minor version be a short numeric token (e.g., use
(\d+)[.-](\d{1,2})(?!\d) or otherwise assert the minor part is not followed by
more digits) so date-like sequences (e.g., -20250514) are not captured, then
keep the existing parsing/branching (assign major, minor from the groups and
return False when (major == 4 and minor >= 6) or major >= 5) using the updated
match logic on model_lower.

In `@lib/crewai/src/crewai/llms/providers/anthropic/completion.py`:
- Around line 457-459: The regex that parses Claude model strings is treating
date-stamped names like "claude-opus-4-20250514-v1:0" as major=4,
minor=20250514; update the parsing used by _is_no_prefill_model (the function
that decides whether to drop temperature) so the minor version is only captured
when it follows a dot (e.g., match major with something like (\d+) and minor
with (?:\.(\d+))?), then treat missing minor as None/0 when deciding "4.6+";
ensure the temperature-dropping check (the branch that sets
params["temperature"] only if not self._is_no_prefill_model()) uses the
corrected parsed values so date-stamped models are not misclassified.
🪄 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: 70ad8940-51b5-437b-830e-65a10971b9b1

📥 Commits

Reviewing files that changed from the base of the PR and between c36827b and 7c68e0e.

📒 Files selected for processing (6)
  • lib/crewai/src/crewai/agents/crew_agent_executor.py
  • lib/crewai/src/crewai/llm.py
  • lib/crewai/src/crewai/llms/base_llm.py
  • lib/crewai/src/crewai/llms/providers/anthropic/completion.py
  • lib/crewai/src/crewai/utilities/agent_utils.py
  • lib/crewai/tests/agents/test_claude_opus_4_7_support.py

Comment thread lib/crewai/src/crewai/llm.py
Comment thread lib/crewai/src/crewai/llms/providers/anthropic/completion.py
The regex now limits the minor version capture to 1-2 digits so
names like claude-opus-4-20250514 are not misclassified as 4.20250514.
Added tests for date-stamped and Bedrock-style model IDs.

Co-Authored-By: João <joao@crewai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support for new Anthropic model: Claude-4.7-Opus

0 participants