Skip to content

fix(agent): strip trailing empty assistant messages before API calls to prevent prefill rejection - #2129

Closed
ygd58 wants to merge 1 commit into
NousResearch:mainfrom
ygd58:fix/strip-trailing-empty-assistant-messages
Closed

fix(agent): strip trailing empty assistant messages before API calls to prevent prefill rejection#2129
ygd58 wants to merge 1 commit into
NousResearch:mainfrom
ygd58:fix/strip-trailing-empty-assistant-messages

Conversation

@ygd58

@ygd58 ygd58 commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #2128

Problem

Commit e84d952 added an empty assistant message after reasoning-only responses for the Responses API (required by missing_following_item). This empty message leaks into conversation history and gets sent to Chat Completions / Anthropic Messages API providers that reject trailing assistant messages as unsupported prefill:

litellm.BadRequestError: This model does not support assistant message prefill. The conversation must end with a user message.

Fix

Added step 3 to _sanitize_api_messages() that strips trailing empty assistant messages (no content, no tool_calls) before every API call. This runs unconditionally so it protects all providers — Chat Completions, Anthropic Messages API, and any OpenAI-compatible endpoint.

Why here?

_sanitize_api_messages() already runs before every LLM call and handles similar cleanup tasks (orphaned tool calls, stub results). This is the natural place for this fix.

@ygd58

ygd58 commented Mar 20, 2026

Copy link
Copy Markdown
Contributor Author

The 3 failing tests (test_cli_new_session.py) are pre-existing failures unrelated to this PR. They fail on reset_session_state attribute which doesn't exist in _FakeAgent this is a test infrastructure issue, not caused by the run_agent.py change here.

@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #2466 — cherry-picked with authorship preserved. Thanks!

@teknium1 teknium1 closed this Mar 22, 2026
teknium1 added a commit that referenced this pull request Mar 22, 2026
Reverts the sanitizer addition from PR #2466 (originally #2129).
We already have _empty_content_retries handling for reasoning-only
responses. The trailing strip risks silently eating valid messages
and is redundant with existing empty-content handling.
outsourc-e pushed a commit to outsourc-e/hermes-agent that referenced this pull request Mar 26, 2026
Reverts the sanitizer addition from PR NousResearch#2466 (originally NousResearch#2129).
We already have _empty_content_retries handling for reasoning-only
responses. The trailing strip risks silently eating valid messages
and is redundant with existing empty-content handling.
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
Reverts the sanitizer addition from PR NousResearch#2466 (originally NousResearch#2129).
We already have _empty_content_retries handling for reasoning-only
responses. The trailing strip risks silently eating valid messages
and is redundant with existing empty-content handling.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
Reverts the sanitizer addition from PR NousResearch#2466 (originally NousResearch#2129).
We already have _empty_content_retries handling for reasoning-only
responses. The trailing strip risks silently eating valid messages
and is redundant with existing empty-content handling.
gweeteve pushed a commit to gweeteve/hermes-agent that referenced this pull request Jun 2, 2026
Reverts the sanitizer addition from PR NousResearch#2466 (originally NousResearch#2129).
We already have _empty_content_retries handling for reasoning-only
responses. The trailing strip risks silently eating valid messages
and is redundant with existing empty-content handling.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
Reverts the sanitizer addition from PR NousResearch#2466 (originally NousResearch#2129).
We already have _empty_content_retries handling for reasoning-only
responses. The trailing strip risks silently eating valid messages
and is redundant with existing empty-content handling.
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: Empty assistant message from reasoning-only fix leaks into Chat Completions API, causing prefill rejection

2 participants