Skip to content

fix(anthropic): replace whitespace-only trailing user message with non-whitespace character#6215

Open
tsushanth wants to merge 1 commit into
livekit:mainfrom
tsushanth:fix/anthropic-trailing-user-whitespace
Open

fix(anthropic): replace whitespace-only trailing user message with non-whitespace character#6215
tsushanth wants to merge 1 commit into
livekit:mainfrom
tsushanth:fix/anthropic-trailing-user-whitespace

Conversation

@tsushanth

Copy link
Copy Markdown

Fixes #5254
Fixes #6213

to_chat_ctx in _provider_format/anthropic.py appends a trailing user turn when inject_trailing_user_message=True and the chat context ends on an assistant message. This path is taken for models matched by _NO_PREFILL_PATTERNS (currently claude-sonnet-4-6 and claude-opus-4-6) to satisfy their no-prefill constraint.

The placeholder text was a single ASCII space (" "), which Anthropic's Messages API rejects:

HTTP 400: messages: text content blocks must contain non-whitespace text

This fails every LLM call that goes through the no-prefill branch when the context ends on an assistant turn — common in sequential generate_reply() calls, agent handoffs, and silence re-engagement flows.

The fix replaces the single space with a period. The structural intent (forcing a user turn) is preserved; the one-character change is the minimal-blast-radius fix that satisfies the API constraint.

…n-whitespace character

inject_trailing_user_message appends a dummy user turn when the chat
context ends on an assistant message, to satisfy Claude 4.6+ models
that reject prefilling. The placeholder text was a single ASCII space,
which Anthropic's Messages API rejects with HTTP 400 (messages: text
content blocks must contain non-whitespace text).

Replace the space with a period so the structural intent of the dummy
turn is preserved while satisfying the API's non-whitespace requirement.

Fixes livekit#5254
Fixes livekit#6213
@tsushanth tsushanth requested a review from a team as a code owner June 24, 2026 17:27
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants