CLIProxyAPI icon indicating copy to clipboard operation
CLIProxyAPI copied to clipboard

Responses API output_text is null for OpenAI-compatible non-streaming responses

Open Beyfish opened this issue 7 months ago • 1 comments

Bug

When /v1/responses is backed by an OpenAI-compatible chat completions provider, CLIProxyAPI returns generated text inside output[].content[].text, but the top-level output_text field is missing/null.

Many Responses API clients read response.output_text directly, so they display an empty result even though the model generated text and usage reports output tokens.

Reproduction shape

Request:

{
  "model": "gpt-5.4",
  "input": "Reply with exactly: ping",
  "max_output_tokens": 32
}

Observed response shape:

{
  "status": "completed",
  "output_text": null,
  "output": [
    {
      "type": "message",
      "content": [
        { "type": "output_text", "text": "ping" }
      ]
    }
  ],
  "usage": { "output_tokens": 5 }
}

Expected

Top-level output_text should be populated with the concatenated assistant output text:

{
  "output_text": "ping"
}

Likely fix

In internal/translator/openai/openai/responses/openai_openai-responses_response.go:

  1. In ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream, aggregate each choices[].message.content string while building message output items.
  2. Set resp.output_text to the concatenated value when non-empty.
  3. In the streaming completed event builder, aggregate st.MsgTextBuf in output order and set response.output_text on response.completed.

Potential helper:

func responseOutputTextFromMessageBuffers(msgTextBuf map[int]*strings.Builder, msgOutputIx map[int]int) string {
    type textItem struct {
        outputIndex int
        text        string
    }
    items := make([]textItem, 0, len(msgTextBuf))
    for choiceIndex, b := range msgTextBuf {
        if b == nil || b.Len() == 0 {
            continue
        }
        items = append(items, textItem{outputIndex: msgOutputIx[choiceIndex], text: b.String()})
    }
    sort.Slice(items, func(i, j int) bool { return items[i].outputIndex < items[j].outputIndex })
    var out strings.Builder
    for _, item := range items {
        out.WriteString(item.text)
    }
    return out.String()
}

Then set:

if outputText := responseOutputTextFromMessageBuffers(st.MsgTextBuf, st.MsgOutputIx); outputText != "" {
    completed, _ = sjson.SetBytes(completed, "response.output_text", outputText)
}

For non-streaming, accumulate outputText.WriteString(c.String()) near the message item creation, then:

if outputText.Len() > 0 {
    resp, _ = sjson.SetBytes(resp, "output_text", outputText.String())
}

Workaround

Clients can read output[].content[].text as a fallback when output_text is absent/null.

Beyfish avatar Apr 26 '26 14:04 Beyfish

Reviewed this against the native gpt-5.4 /v1/responses streaming output.

The native Responses result does not include a top-level response.output_text; assistant text is carried under response.output[].content[].text. After restarting the service and comparing the same request with gpt-5.4 and agnes-2.0-flash, both now match on this contract: no top-level output_text, message text in output[].content[].text, and tool calls kept as separate function_call output items.

Because the reported shape is now considered the expected compatibility target rather than a repository bug, closing this as not planned.

luispater avatar Jun 17 '26 19:06 luispater