Skip to content

fix: move handoff instructions to system prompt (fixes #54) - #58

Merged
danny-avila merged 2 commits into
mainfrom
pr-55
Feb 14, 2026
Merged

danny-avila merged 2 commits into
mainfrom
pr-55

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

No description provided.

JumpLink and others added 2 commits February 13, 2026 16:58
… 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
@danny-avila
danny-avila merged commit 31267a6 into main Feb 14, 2026
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>
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.

2 participants