Skip to content

fix: sanitize Anthropic messages - strip trailing whitespace and filter empty content#4620

Open
Anandesh-Sharma wants to merge 1 commit intocrewAIInc:mainfrom
Anandesh-Sharma:fix/4413-anthropic-trailing-whitespace
Open

fix: sanitize Anthropic messages - strip trailing whitespace and filter empty content#4620
Anandesh-Sharma wants to merge 1 commit intocrewAIInc:mainfrom
Anandesh-Sharma:fix/4413-anthropic-trailing-whitespace

Conversation

@Anandesh-Sharma
Copy link

Summary

  • Strips trailing whitespace from the final assistant message in _format_messages_for_anthropic() to prevent Anthropic's 400 BadRequestError
  • Filters out user messages with empty string content, which Anthropic also rejects
  • Handles both string content and list/block content formats (e.g., thinking blocks + text)

Fixes #4413
Fixes #4427

Test plan

  • Verify assistant messages with trailing whitespace like "Say: " are sanitized
  • Verify empty user messages {"role": "user", "content": ""} are filtered out
  • Verify normal messages are not modified
  • Verify list-format assistant content (thinking blocks) is handled correctly

🤖 Generated with Claude Code

Anthropic's API rejects messages where the final assistant content ends
with trailing whitespace, and requires all messages to have non-empty
content. This fix:

1. Strips trailing whitespace from the final assistant message content
   (both string and list/block formats)
2. Filters out user messages with empty string content

Fixes crewAIInc#4413
Fixes crewAIInc#4427

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant