fix: handle empty message content in AnthropicCompletion (#4427)#4476
fix: handle empty message content in AnthropicCompletion (#4427)#4476AtharvaJaiswal005 wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
This PR is being reviewed by Cursor Bugbot
Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
) Filter out messages with empty or whitespace-only content before sending to the Anthropic API, which requires all non-assistant messages to have non-empty content. - Filter empty/whitespace-only messages from formatted message list - Preserve empty assistant messages (allowed by Anthropic API) - Raise ValueError when all messages have empty content to prevent meaningless API calls - Log warning when empty messages are filtered from a mixed list - Add 4 tests covering empty, whitespace, assistant, and all-empty cases
1e0d8be to
4eee048
Compare
|
Hi! Just checking in on this PR - is there anything I should update or change? Happy to make adjustments based on any feedback. Would also appreciate if someone could approve the CI workflow to run. Thanks! |
|
This PR is stale because it has been open for 45 days with no activity. |
Summary
Fixes #4427
AnthropicCompletioncrashes with a400 BadRequestErrorwhen messages with empty string content ("") are passed tocall(). The Anthropic API requires all non-assistant messages to have non-empty content, but the framework was forwarding empty messages without any validation.This commonly occurs in multi-agent pipelines where one agent's empty output becomes the next agent's input, causing cascading failures.
Changes
lib/crewai/src/crewai/llms/providers/anthropic/completion.py_format_messages_for_anthropic()after message formatting but before the first-message-is-user checkValueErrorwith a clear message when all messages have empty content, preventing meaningless API callslib/crewai/tests/llms/anthropic/test_anthropic.pytest_anthropic_filters_empty_user_messages— verifies""content is filteredtest_anthropic_filters_whitespace_only_user_messages— verifies" "is filteredtest_anthropic_preserves_assistant_empty_content— verifies empty assistant messages are kepttest_anthropic_all_empty_user_messages_raises_error— verifiesValueErrorwhen all messages are emptyBehavior
400 BadRequestError400 BadRequestErrorValueErrorraised immediately, no API call wastedTest Plan
test_anthropic_message_formattingpasses (no regression)test_anthropic_completion_initialization_parameterspassesNote
Low Risk
Small, localized change to message formatting plus tests; primary risk is minor behavior change for callers that previously relied on sending empty user messages.
Overview
Prevents Anthropic
400 BadRequestErrorfailures by filtering out non-assistant messages with empty/whitespace-only content in_format_messages_for_anthropic()before enforcing the “first message is user” constraint.If filtering removes everything, it now raises a
ValueErrorearly; if only some are removed, it logs a warning. Adds focused tests covering empty/whitespace user messages, preserving empty assistant content, and the all-empty error case.Written by Cursor Bugbot for commit 4eee048. This will update automatically on new commits. Configure here.