Skip to content

fix(protocol): merge a Responses assistant message item into the trailing assistant turnfix(protocol): merge a Responses assistant message item into the trai… - #176

Closed
billy-yi wants to merge 1 commit into
jasonxu114514:mainfrom
billy-yi:fix/responses-assistant-text-after-tool-calls
Closed

billy-yi wants to merge 1 commit into
jasonxu114514:mainfrom
billy-yi:fix/responses-assistant-text-after-tool-calls

Conversation

@billy-yi

Copy link
Copy Markdown

SymptomA client that speaks the Responses protocol and streams the assistant text after its function calls gets every such request rejected with HTTP 400:prepare go upstream request: messages[5]: assistant message appears before tool results for call_…, call_…The model is fine and the same conversation succeeds whenever the text happens to arrive before the tool calls, so the failure looks random.## Root causeA Responses turn can carry its text in a message item that follows the function_call items of the same turn:json{"type":"function_call","call_id":"call_a",...},{"type":"function_call","call_id":"call_b",...},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"I will inspect the directory first."}]},{"type":"function_call_output","call_id":"call_a",...},{"type":"function_call_output","call_id":"call_b",...}````decodeBridgeRequest` appended that message item as a **new** bridge message, so `encodeChatRequest` saw an assistant message while the tool calls it belongs to were still waiting for their results, and rejected the request:messages[N]: assistant message appears before tool results for …````appendBridgeBlockonly merges a standalone item into the previous message when that message holds *only* tool calls or results, and amessage` item is appended directly, so the merge never happened for text.## FixThe text of an assistant `message` item now joins the trailing assistant message. That is exactly what the Chat and Anthropic encoders can express: one assistant turn holding `content` and `tool_calls`, followed by the tool results. An assistant message that starts a new turn (the previous message is a user turn or a tool result) still starts its own message.## Tests`internal/protocol/request_test.go` covers both directions:* `TestDecodeResponsesAssistantTextAfterToolCalls` — text item after the calls: one assistant bridge message holding 2 calls and 1 text block, and `encodeBridgeRequest(Chat, …)` succeeds. It fails on `main` with `got 5 bridge messages, want 4`.* `TestDecodeResponsesAssistantTextAfterToolResults` — text of the next assistant turn still gets its own message.`go vet ./internal/protocol/ && go test ./internal/protocol/` passes.## ReproductionAny Responses client that emits the assistant text after the function calls reproduces it against the Go upstream (the `/zen/go` and `/inference` lanes); sending the same items with the text before the calls returns 200.…ling assistant turn

A Responses turn can carry its assistant text in a message item that follows the function_call items of the same turn. decodeBridgeRequest appended that item as a new bridge message, so encodeChatRequest saw an assistant message while the tool calls it belongs to were still waiting for their results and rejected the request with:

messages[N]: assistant message appears before tool results for ...

The text now joins the trailing assistant message, which is what the Chat and Anthropic encoders can express: one assistant turn holding content and tool_calls, followed by the tool results.

…ling assistant turn

A Responses turn can carry its assistant text in a message item that follows
the function_call items of the same turn. decodeBridgeRequest appended that
item as a new bridge message, so encodeChatRequest saw an assistant message
while the tool calls it belongs to were still waiting for their results and
rejected the request with:

  messages[N]: assistant message appears before tool results for ...

The text now joins the trailing assistant message, which is what the Chat and
Anthropic encoders can express: one assistant turn holding content and
tool_calls, followed by the tool results.
jasonxu114514 added a commit that referenced this pull request Nov 16, 2026
Preserve upstream HTTP failures across transport retries, classify title requests before shaping, and rename legacy functions exactly once.

Keep adjacent Responses assistant items together, including interleaved text and calls. Add opt-in prompt environment overrides with matching shell descriptions, strict validation, and WebUI round-trip preservation.

Document compatibility boundaries and retain existing defaults. Include the requested test/ ignore rule. Offline zz_ regressions passed and were removed after validation as requested; vet and Windows/Linux builds passed.

Refs #173, #175, #176, #178, #179, #181
@jasonxu114514

Copy link
Copy Markdown
Owner

Thanks @billy-yi. Implemented on main in 14faec7: 14faec7 . The existing appendBridgeBlock now joins adjacent assistant text, reasoning and calls, including call-text-call; user messages and tool results remain boundaries. Offline Responses-to-Chat/Anthropic regressions, vet and Windows/Linux builds passed. Operator-requested zz_ tests were removed afterward. No live upstream check. This supersedes the proposed fix; PR commits were not merged.

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