Files
ai/controllers/openai_responses_test.go
b649e72819 A Responses stream ends when the answer does, not when [DONE] arrives (#113)
Hanzo's provider ends its stream without [DONE], and nothing else finished
the response. Every delta reached the client and none of the terminal
events did — no response.output_text.done, .content_part.done,
.output_item.done or .completed — so a client reading the stream to spec
waited for a completion that never came, retried, and reported the stream
as truncated. Observed against api.hanzo.ai: 63 deltas carrying the whole
answer, then the connection simply closed.

Finishing on finish_reason alone was already tried and reverted, for a
real reason: the usage-only chunk commonly arrives after the terminal
choice chunk, and finishing early published response.completed with zero
usage and made that accounting chunk impossible to apply.

Both signals are now required, in whichever order they arrive: whichever
of finish_reason and usage is second finishes the response. The stream
closes on time and the usage is in it. [DONE] still finishes when a
provider sends one, and the bridge Close stays the backstop for a stream
that sends neither.

Two tests, one per arrival order. The first fails without this change with
the exact production symptom — the wire stops at output_text.delta.

Co-authored-by: Hanzo <[email protected]>

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-07 21:56:59 -07:00

303 lines
11 KiB
Go

package controllers
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/klauspost/compress/zstd"
)
func TestResponsesZstdPureGoRoundTrip(t *testing.T) {
w, err := zstd.NewWriter(nil)
if err != nil {
t.Fatal(err)
}
compressed := w.EncodeAll([]byte(`{"model":"zen4-coder","input":"hello"}`), nil)
w.Close()
decoded, err := decodeResponsesZstd(compressed)
if err != nil {
t.Fatal(err)
}
if got, want := string(decoded), `{"model":"zen4-coder","input":"hello"}`; got != want {
t.Fatalf("decoded = %q, want %q", got, want)
}
}
func TestResponsesInputToMessagesToolRoundTrip(t *testing.T) {
input := json.RawMessage(`[
{"type":"message","role":"user","content":[{"type":"input_text","text":"run it"}]},
{"type":"function_call","id":"fc_1","call_id":"call_1","name":"shell_command","arguments":"{\"command\":\"pwd\"}"},
{"type":"function_call_output","call_id":"call_1","output":"/tmp"},
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}
]`)
messages, err := responsesInputToMessages("be precise", input)
if err != nil {
t.Fatal(err)
}
if len(messages) != 5 {
t.Fatalf("messages = %#v, want system + user + assistant tool call + tool result + assistant", messages)
}
if messages[0].Role != "system" || messages[0].Content != "be precise" {
t.Fatalf("system message = %#v", messages[0])
}
if messages[2].Role != "assistant" || len(messages[2].ToolCalls) != 1 {
t.Fatalf("tool call history = %#v", messages[2])
}
if got := messages[2].ToolCalls[0].Function.Name; got != "shell_command" {
t.Fatalf("tool name = %q", got)
}
if messages[3].Role != "tool" || messages[3].ToolCallID != "call_1" || messages[3].Content != "/tmp" {
t.Fatalf("tool result = %#v", messages[3])
}
}
// A user message carrying a screenshot beside its text — what a coding agent's
// browser tool sends after every capture. go-openai refuses to marshal a message
// that sets both Content and MultiContent, so the parts must travel alone.
func TestResponsesInputToMessagesImageMarshals(t *testing.T) {
input := json.RawMessage(`[
{"type":"message","role":"user","content":[
{"type":"input_text","text":"what is on screen?"},
{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgo="}
]}
]`)
messages, err := responsesInputToMessages("", input)
if err != nil {
t.Fatal(err)
}
if len(messages) != 1 || len(messages[0].MultiContent) != 2 || messages[0].Content != "" {
t.Fatalf("message = %#v, want two parts and no joined text", messages[0])
}
if _, err := json.Marshal(messages[0]); err != nil {
t.Fatalf("marshal: %v", err)
}
}
func TestResponsesToChatRequestTools(t *testing.T) {
parallel := true
req := &OpenAIResponsesRequest{
Model: "zen4-coder", Input: json.RawMessage(`"hello"`), Stream: true,
ParallelToolCalls: &parallel,
ToolChoice: json.RawMessage(`{"type":"function","name":"shell_command"}`),
Tools: []responsesTool{{
Type: "function", Name: "shell_command", Description: "run shell",
Parameters: json.RawMessage(`{"type":"object","properties":{"command":{"type":"string"}}}`),
}},
}
chat, kinds, err := responsesToChatRequest(req)
if err != nil {
t.Fatal(err)
}
if len(chat.Tools) != 1 || chat.Tools[0].Function.Name != "shell_command" {
t.Fatalf("tools = %#v", chat.Tools)
}
if kinds["shell_command"] != "function" {
t.Fatalf("tool kinds = %#v", kinds)
}
choice, ok := chat.ToolChoice.(map[string]interface{})
if !ok || choice["type"] != "function" {
t.Fatalf("tool choice = %#v", chat.ToolChoice)
}
if enabled, ok := chat.ParallelToolCalls.(bool); !ok || !enabled {
t.Fatalf("parallel_tool_calls = %#v", chat.ParallelToolCalls)
}
if chat.StreamOptions == nil || !chat.StreamOptions.IncludeUsage {
t.Fatalf("stream_options must request the final usage chunk: %#v", chat.StreamOptions)
}
}
func TestResponsesStreamTextWire(t *testing.T) {
req := &OpenAIResponsesRequest{Model: "zen4-coder", Stream: true}
out := &bytes.Buffer{}
bridge := newResponsesBridge(out, req, nil)
upstream := strings.Join([]string{
`data: {"id":"chat_1","model":"qwen","choices":[{"delta":{"role":"assistant","content":"HANZO_"},"finish_reason":null}]}`,
`data: {"id":"chat_1","model":"qwen","choices":[{"delta":{"content":"OK"},"finish_reason":null}]}`,
`data: {"id":"chat_1","model":"qwen","choices":[{"delta":{},"finish_reason":"stop"}]}`,
`data: {"id":"chat_1","model":"qwen","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":2,"total_tokens":14}}`,
`data: [DONE]`,
"",
}, "\n\n")
// Split in the middle of a frame to prove arbitrary network writes work.
cut := len(upstream) / 2
if _, err := bridge.Write([]byte(upstream[:cut])); err != nil {
t.Fatal(err)
}
if _, err := bridge.Write([]byte(upstream[cut:])); err != nil {
t.Fatal(err)
}
if err := bridge.Close(); err != nil {
t.Fatal(err)
}
wire := out.String()
assertInOrder(
t, wire,
`event: response.created`,
`event: response.output_item.added`,
`event: response.content_part.added`,
`"delta":"HANZO_"`,
`"delta":"OK"`,
`event: response.output_item.done`,
`event: response.completed`,
)
for _, forbidden := range []string{`chat.completion.chunk`, `"choices"`, `data: [DONE]`} {
if strings.Contains(wire, forbidden) {
t.Fatalf("raw Chat Completions token %q leaked:\n%s", forbidden, wire)
}
}
if !strings.Contains(wire, `"text":"HANZO_OK"`) || !strings.Contains(wire, `"total_tokens":14`) {
t.Fatalf("missing final text/usage:\n%s", wire)
}
if got := strings.Count(wire, `event: response.completed`); got != 1 {
t.Fatalf("response.completed count = %d, want exactly one after the usage chunk:\n%s", got, wire)
}
}
func TestResponsesStreamFunctionCallWire(t *testing.T) {
req := &OpenAIResponsesRequest{Model: "zen4-coder", Stream: true}
out := &bytes.Buffer{}
bridge := newResponsesBridge(out, req, map[string]string{"shell_command": "function"})
upstream := strings.Join([]string{
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"shell_command","arguments":""}}]},"finish_reason":null}]}`,
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"command\":"}}]},"finish_reason":null}]}`,
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"pwd\"}"}}]},"finish_reason":null}]}`,
`data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":20,"completion_tokens":5,"total_tokens":25}}`,
`data: [DONE]`,
"",
}, "\n\n")
if _, err := bridge.Write([]byte(upstream)); err != nil {
t.Fatal(err)
}
if err := bridge.Close(); err != nil {
t.Fatal(err)
}
wire := out.String()
assertInOrder(
t, wire,
`event: response.created`,
`event: response.output_item.added`,
`event: response.function_call_arguments.delta`,
`event: response.function_call_arguments.done`,
`event: response.output_item.done`,
`event: response.completed`,
)
for _, required := range []string{`"type":"function_call"`, `"call_id":"call_1"`, `"name":"shell_command"`, `"arguments":"{\"command\":\"pwd\"}"`} {
if !strings.Contains(wire, required) {
t.Fatalf("missing %q:\n%s", required, wire)
}
}
}
// A non-streaming answer is ONE body, so it is translated as one — by the function
// the sink in Responses hands it to, not through the stream bridge. It used to go
// through the bridge because the bridge was standing in for the response writer and
// saw everything; it now sees only the stream, so this asks the translation
// directly.
func TestResponsesNonStreamingTranslation(t *testing.T) {
req := &OpenAIResponsesRequest{Model: "zen4-coder"}
chat := `{"id":"chat_1","choices":[{"message":{"role":"assistant","content":"OK"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`
body, err := openAIChatResponseToResponses([]byte(chat), req, nil)
if err != nil {
t.Fatal(err)
}
var got map[string]interface{}
if err := json.Unmarshal(body, &got); err != nil {
t.Fatal(err)
}
if got["object"] != "response" || got["status"] != "completed" {
t.Fatalf("response = %#v", got)
}
if strings.Contains(string(body), `"choices"`) {
t.Fatalf("chat response leaked: %s", body)
}
}
func assertInOrder(t *testing.T, text string, needles ...string) {
t.Helper()
pos := 0
for _, needle := range needles {
next := strings.Index(text[pos:], needle)
if next < 0 {
t.Fatalf("missing/out-of-order %q after byte %d:\n%s", needle, pos, text)
}
pos += next + len(needle)
}
}
// A provider is free to end its stream without [DONE], and Hanzo's does. When
// that happened the response never closed: every delta arrived, none of the
// terminal events did, and a client reading the Responses stream to spec waited
// for response.completed, retried, and reported the stream as truncated.
//
// The usage chunk still has to land in it, which is why finishing on
// finish_reason alone was wrong — whichever signal arrives second finishes.
func TestResponsesStreamCompletesWithoutDone(t *testing.T) {
req := &OpenAIResponsesRequest{Model: "enso-auto", Stream: true}
out := &bytes.Buffer{}
bridge := newResponsesBridge(out, req, nil)
upstream := strings.Join([]string{
`data: {"id":"chat_1","model":"enso","choices":[{"delta":{"role":"assistant","content":"hi"},"finish_reason":null}]}`,
`data: {"id":"chat_1","model":"enso","choices":[{"delta":{},"finish_reason":"stop"}]}`,
`data: {"id":"chat_1","model":"enso","choices":[],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`,
"",
}, "\n\n")
if _, err := bridge.Write([]byte(upstream)); err != nil {
t.Fatal(err)
}
wire := out.String()
// Before Close, because a client must not have to wait for the connection
// to drop to learn the answer is complete.
assertInOrder(
t, wire,
`event: response.created`,
`"delta":"hi"`,
`event: response.output_text.done`,
`event: response.content_part.done`,
`event: response.output_item.done`,
`event: response.completed`,
)
if !strings.Contains(wire, `"total_tokens":4`) {
t.Fatalf("usage from the post-finish_reason chunk was lost:\n%s", wire)
}
if err := bridge.Close(); err != nil {
t.Fatal(err)
}
if got := strings.Count(out.String(), `event: response.completed`); got != 1 {
t.Fatalf("response.completed count = %d, want exactly one; Close must not repeat it", got)
}
}
// The usage chunk may also arrive first. Whichever signal is second finishes.
func TestResponsesStreamCompletesWhenUsagePrecedesFinishReason(t *testing.T) {
req := &OpenAIResponsesRequest{Model: "enso-auto", Stream: true}
out := &bytes.Buffer{}
bridge := newResponsesBridge(out, req, nil)
upstream := strings.Join([]string{
`data: {"id":"c","model":"enso","choices":[{"delta":{"role":"assistant","content":"yo"},"finish_reason":null}]}`,
`data: {"id":"c","model":"enso","choices":[],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`,
`data: {"id":"c","model":"enso","choices":[{"delta":{},"finish_reason":"stop"}]}`,
"",
}, "\n\n")
if _, err := bridge.Write([]byte(upstream)); err != nil {
t.Fatal(err)
}
wire := out.String()
if !strings.Contains(wire, `event: response.completed`) {
t.Fatalf("no response.completed when usage preceded finish_reason:\n%s", wire)
}
if !strings.Contains(wire, `"total_tokens":2`) {
t.Fatalf("usage missing:\n%s", wire)
}
}