Repository navigation
Conversation
… after tool" API error When the receiving agent's filtered messages end with a ToolMessage (e.g. from list_upload_sessions before the handoff), appending handoff instructions as a HumanMessage produced the sequence tool → user, which many chat APIs reject (400 Unexpected role 'user' after role 'tool'). Now we extend the last ToolMessage's content with the instructions instead, so the next turn is validly an assistant response.
Collaborator
|
Closing in favor of #58 Let me know if my solution also works for you |
Collaborator
|
follow-up PR to retain stronger steering of the human message for task instructions: https://github.com/danny-avila/agents/pull/59/changes |
Collaborator
|
Also note, I couldn't reproduce the original issue anyway, so some context about the exact model/provider used would help inform |
Contributor
Author
Yes works, thank you. |
JumpLink
added a commit
to faktenforum/agents
that referenced
this pull request
Feb 16, 2026
…ystem prompt) Revert PR LibreChat-AI#55 approach (inject instructions into last ToolMessage) and restore upstream fix from LibreChat-AI#58: handoff instructions are injected into the system prompt via AgentContext.setHandoffContext and buildIdentityPreamble. This avoids the bridge AIMessage and the '400 Unexpected role user after role tool' API error (issue LibreChat-AI#54). Co-authored-by: Cursor <cursoragent@cursor.com>
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 #54
When the receiving agent's
filteredMessagesend with a ToolMessage (e.g. the router ranlist_upload_sessionsbefore the handoff), we previously appended handoff instructions as aHumanMessage. That produced the sequence… tool → user, which many chat APIs reject with400 Unexpected role 'user' after role 'tool'.This change: if the last message is a
ToolMessage, we extend its content with the handoff instructions instead of adding a newHumanMessage. The next turn is then a valid assistant response. If the last message is not a tool message, behaviour is unchanged (we still append aHumanMessagewith the instructions).Token map handling is updated so the instructions are counted in either the extended tool message or the new human message.
Testing
Seems to be working now (compare this with the screenshot in the issue):
