Skip to content

fix: strip trailing whitespace from Anthropic assistant messages#5577

Open
VANDRANKI wants to merge 2 commits intocrewAIInc:mainfrom
VANDRANKI:fix/anthropic-trailing-whitespace
Open

fix: strip trailing whitespace from Anthropic assistant messages#5577
VANDRANKI wants to merge 2 commits intocrewAIInc:mainfrom
VANDRANKI:fix/anthropic-trailing-whitespace

Conversation

@VANDRANKI
Copy link
Copy Markdown

Fixes #4413.

Bug

Anthropic's API returns a 400 BadRequestError when any assistant message has trailing whitespace in its content. CrewAI sometimes appends partial assistant turns to steer the model (e.g. "content": "Say: "), which can end with a space.

Fix

In _format_messages_for_provider, strip trailing whitespace from assistant message content before returning for Anthropic models:

for msg in messages:
    if msg["role"] == "assistant" and isinstance(msg.get("content"), str):
        sanitized.append({**msg, "content": msg["content"].rstrip()})
    else:
        sanitized.append(msg)

The sanitization is scoped to the Anthropic code path only and does not affect other providers.

VANDRANKI and others added 2 commits April 21, 2026 14:20
Anthropic's API rejects requests with a 400 BadRequestError when any
assistant message has trailing whitespace in its content. This commonly
occurs when CrewAI appends a partial assistant turn to steer the model.

Strip trailing whitespace from assistant message content in
_format_messages_for_provider before sending to Anthropic.

Fixes crewAIInc#4413

Co-Authored-By: graylin-byte <259755272+graylin-byte@users.noreply.github.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

Development

Successfully merging this pull request may close these issues.

[BUG] LLM.call fails with trailing whitespace in final assistant message for anthropic models

2 participants