fix: merge split assistant messages to prevent DeepSeek 400 on role alternation - #49162
Closed
woaini30050 wants to merge 2 commits into
Closed
fix: merge split assistant messages to prevent DeepSeek 400 on role alternation#49162woaini30050 wants to merge 2 commits into
woaini30050 wants to merge 2 commits into
Conversation
woaini30050
force-pushed
the
fix/merge-split-assistant-messages
branch
2 times, most recently
from
June 20, 2026 15:26
b6553e3 to
cec71de
Compare
- Remove dead `merged_count` tracking in sanitize_api_messages - Fix duplicated docstring after merge function insertion - Restore missing `# --- Role allowlist` comment
woaini30050
force-pushed
the
fix/merge-split-assistant-messages
branch
from
June 20, 2026 15:27
cec71de to
3ec5f90
Compare
This was referenced Jun 30, 2026
Closed
Closed
Contributor
|
Merged via PR #55603 (commit We consolidated this with the sibling reports (#29148, #49147) and the other open PRs (#49162, #34510) into one pass in |
This was referenced Aug 3, 2026
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.
Description
When Hermes builds the API request payload for the next turn, some code paths (streaming accumulation, provider adapters, session replay) can split a single assistant response that contains both
content(text reasoning) andtool_callsinto two consecutive assistant messages:Strict providers like DeepSeek v4 Flash enforce strict
assistant ↔ toolalternation and reject two consecutiveassistantmessages with HTTP 400:The DB storage is correct (1 row with both
content+tool_calls), but the API payload incorrectly has 2 messages.Fix
Adds
merge_split_assistant_messages()inagent.agent_runtime_helpers.pythat merges adjacentassistantmessages where:contentbut NOtool_callstool_callsbut NOcontent→ Result: a single
{"role": "assistant", "content": "...", "tool_calls": [...]}The merge runs inside
sanitize_api_messages(), which executes before every API call — covering both the main conversation loop and the summary/max-iterations path. This is a defense-in-depth approach that catches the split regardless of where it originates.Safety (what is NOT merged)
Testing
Related Issues
Closes #49147