Repository navigation
fix: move handoff instructions to system prompt (fixes #54) - #58
Merged
Merged
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.
…ssage
Replaces the approach of appending instructions as a HumanMessage (or
extending ToolMessage content) with system prompt injection through the
existing AgentContext.setHandoffContext → buildIdentityPreamble pipeline.
This fixes the "400 Unexpected role 'user' after role 'tool'" error
(issue #54) that occurred when a router agent ran a non-handoff tool
before handing off. After filtering transfer messages, the last message
was a ToolMessage, and appending a HumanMessage created an invalid
tool → user role sequence.
By moving instructions into the system prompt (## Task Instructions),
role ordering is never affected regardless of what the last filtered
message is
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.
No description provided.