Fix #5808: Add support for Claude 4.7 Opus (no assistant prefill, drop temperature)#5811
Fix #5808: Add support for Claude 4.7 Opus (no assistant prefill, drop temperature)#5811devin-ai-integration[bot] wants to merge 3 commits into
Conversation
…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>
|
Prompt hidden (unlisted session) |
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughAdds 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. ChangesClaude 4.7 Opus prefill support
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
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 |
There was a problem hiding this comment.
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
📒 Files selected for processing (6)
lib/crewai/src/crewai/agents/crew_agent_executor.pylib/crewai/src/crewai/llm.pylib/crewai/src/crewai/llms/base_llm.pylib/crewai/src/crewai/llms/providers/anthropic/completion.pylib/crewai/src/crewai/utilities/agent_utils.pylib/crewai/tests/agents/test_claude_opus_4_7_support.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>
Summary
Fixes #5808 — Claude 4.7 Opus (and Claude 4.6+ models) broke CrewAI because:
assistantrole (no assistant message prefill support)temperatureparameterChanges
BaseLLM(lib/crewai/src/crewai/llms/base_llm.py):supports_assistant_prefill()method defaulting toTrueso existing providers are unaffectedAnthropicCompletion(lib/crewai/src/crewai/llms/providers/anthropic/completion.py):supports_assistant_prefill()override using_is_no_prefill_model()regex-based detection (Claude 4.6+ / 5.0+)LLM(litellm fallback,lib/crewai/src/crewai/llm.py):supports_assistant_prefill()usinglitellm.get_model_info()with fallback to name-based heuristicCrewAgentExecutor(lib/crewai/src/crewai/agents/crew_agent_executor.py):supports_prefillproperty usinggetattr()for backward compatibility with custom LLM adapters_append_message()to split observation into auser-role message when the model doesn't support prefill, ensuring the conversation never ends on anassistantturnagent_utils.py(lib/crewai/src/crewai/utilities/agent_utils.py):handle_max_iterations_exceeded()to useuserrole for the forced-answer message when the model doesn't support prefillTests
24 new tests in
lib/crewai/tests/agents/test_claude_opus_4_7_support.pycovering:Review & Testing Checklist for Human
claude.*?(\d+)[.-](\d+)correctly matches all Anthropic naming patternssupports_assistant_prefillto confirm backward compatibilityNotes
getattr()with a fallback for the prefill check to ensure backward compatibility with custom LLM adapters that may not implementsupports_assistant_prefill()Link to Devin session: https://app.devin.ai/sessions/91e01d2a89e04f188e77f8cb39edb2d8
Summary by CodeRabbit