fix: strip trailing whitespace from final assistant message for Anthropic#4430
Open
Chase-Xuu wants to merge 6 commits intocrewAIInc:mainfrom
Open
fix: strip trailing whitespace from final assistant message for Anthropic#4430Chase-Xuu wants to merge 6 commits intocrewAIInc:mainfrom
Chase-Xuu wants to merge 6 commits intocrewAIInc:mainfrom
Conversation
904b5cd to
c6e50f3
Compare
Author
|
The Cursor Bugbot concerns have been addressed in the latest commits:
The test suite passes with the updated expectations. |
Fixes crewAIInc#4427 The Anthropic API requires all messages to have non-empty content except for the optional final assistant message. When messages with empty string content are passed to the API, it returns a 400 error: 'messages.0: all messages must have non-empty content except for the optional final assistant message' This commit adds validation in _format_messages_for_anthropic() to: - Filter out messages with empty or whitespace-only content - Allow empty content only for the final assistant message (per API spec) - Fall back to a default 'Hello' message if all messages are filtered out Also adds regression tests to verify the fix.
…opic Fixes crewAIInc#4413 Anthropic API rejects requests where the final assistant message ends with trailing whitespace with error: 'final assistant content cannot end with trailing whitespace'. This fix adds a check in _format_messages_for_anthropic() to strip trailing whitespace from the final assistant message content before sending to the API. The fix only applies to: - Final assistant messages (not intermediate ones) - String content (not list/structured content) Added tests to verify: - Trailing whitespace is stripped from final assistant messages - Multiple whitespace characters are handled - Non-final assistant messages preserve whitespace - User messages are not affected
Address review feedback: insert placeholder messages when filtering empty messages creates consecutive same-role messages, avoiding Anthropic's 'roles must alternate' error.
The role alternation fix inserts placeholder messages when filtering creates consecutive same-role messages. Updated test to expect 3 messages with the placeholder assistant message.
c6e50f3 to
c62e4c8
Compare
…olders
Address code review feedback: inserting synthetic {'content': '...'} messages
alters the conversation meaning. Instead, merge consecutive same-role messages
to preserve all original content while maintaining Anthropic's role alternation
requirement.
Handles both string and list content formats (Anthropic supports both).
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.
Remove .strip() from merged message content to avoid stripping meaningful leading/trailing whitespace from user or non-final assistant messages during role alternation fixing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4413
Problem
When using LLM.call with an Anthropic model, if the final assistant message ends with trailing whitespace, the call fails with a 400 BadRequestError:
Solution
Added a check in
_format_messages_for_anthropic()method in the AnthropicCompletion provider to strip trailing whitespace from the final assistant message content before sending to the API.The fix:
rstrip()to remove all trailing whitespace characters (spaces, tabs, newlines)Changes
lib/crewai/src/crewai/llms/providers/anthropic/completion.py: Added whitespace stripping logicTesting
Added tests in
lib/crewai/tests/llms/anthropic/test_anthropic.py(from related PR #4427) to verify:Reproduction
Note
Medium Risk
Moderate risk because it changes Anthropic message formatting by filtering/merging messages, which can subtly alter conversation history and tool-call sequencing, though the behavior is constrained and covered by new tests.
Overview
Fixes Anthropic API 400s by hardening
_format_messages_for_anthropic()to drop empty/whitespace-only messages (except an allowed empty final assistant message) and then merge consecutive same-role messages to preserve required role alternation.Also strips trailing whitespace from the final assistant message when its content is a string, and adds unit tests covering empty-message filtering, role-merge behavior, and whitespace stripping/preservation cases.
Written by Cursor Bugbot for commit 492ec2c. This will update automatically on new commits. Configure here.