fix: sanitize Anthropic messages for empty content and trailing whitespace#4542
Open
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix: sanitize Anthropic messages for empty content and trailing whitespace#4542hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
…space Add message sanitization in _format_messages_for_anthropic to prevent two classes of Anthropic API BadRequestErrors: 1. Empty message content: Anthropic rejects all messages with empty content (except an optional final assistant message). Empty strings are now replaced with a placeholder before sending. 2. Trailing whitespace in final assistant message: Anthropic rejects requests where the final assistant message ends with whitespace. Trailing whitespace is now stripped from the final assistant message, including structured content blocks (e.g. thinking + text). Fixes crewAIInc#4413, Fixes crewAIInc#4427
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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 two confirmed Anthropic API
BadRequestErrorissues inAnthropicCompletion._format_messages_for_anthropic():"."placeholder.Both sanitization steps are implemented in a single
_sanitize_messages_for_anthropic()static method, called at the end of_format_messages_for_anthropic()before returning.Fixes #4413
Fixes #4427
Changes
lib/crewai/src/crewai/llms/providers/anthropic/completion.py: Added_sanitize_messages_for_anthropic()static method and call sitelib/crewai/tests/llms/anthropic/test_anthropic_message_sanitization.py: 15 unit tests covering empty content replacement, trailing whitespace stripping, structured content blocks, and combined scenariosTest plan
pytest lib/crewai/tests/llms/anthropic/test_anthropic_message_sanitization.py)