fix(agent): strip trailing empty assistant messages before API calls to prevent prefill rejection - #2129
Closed
ygd58 wants to merge 1 commit into
Closed
Conversation
…to prevent prefill rejection
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. |
Contributor
|
Merged via PR #2466 — cherry-picked with authorship preserved. Thanks! |
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.
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.
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.