[BUG] OpenAiResponsesClient.extractText concatenates every message output item regardless of phase, duplicating gpt-5.6 reasoning-model replies

Author: jonathannaguinCreated Sep 14, 2026Updated Sep 25, 2026
Labelsbug

Description

OpenAiResponsesClient.extractText(Object output) loops over every type:"message" item in a response.completed event's output array and appends all of their output_text content, with no regard for the item's phase. On gpt-5.6-class reasoning models, a single turn can legitimately contain two message-type output items — one tagged "phase":"commentary" and one tagged "phase":"final_answer" — and when their text is identical (or a preamble + confirmation pair), the assembled AiMessage.text() ends up as the reply doubled, or a commentary preamble run into the final answer with no separator.

This affects both the streaming (response.completed SSE event) and non-streaming assembly paths — same method, same bug in both. Confirmed unchanged across langchain4j-open-ai 1.14.1, 1.18.1, and 1.20.0.

Reproduction

A real captured output array from a response.completed event:

[
  {
    "type": "message",
    "phase": "commentary",
    "role": "assistant",
    "status": "completed",
    "content": [
      {"type": "output_text", "text": "Just to confirm, you'd like to make a payment of $40 on August 10. Is that correct?"}
    ]
  },
  {
    "type": "message",
    "phase": "final_answer",
    "role": "assistant",
    "status": "completed",
    "content": [
      {"type": "output_text", "text": "Just to confirm, you'd like to make a payment of $40 on August 10. Is that correct?"}
    ]
  }
]

extractText as of 1.20.0 (OpenAiResponsesClient.java):

private static String extractText(Object output) {
    StringBuilder textBuilder = new StringBuilder();
    for (Object item : arr(output)) {
        if (TYPE_MESSAGE.equals(str(at(item, FIELD_TYPE)))) {
            for (Object c : arr(at(item, FIELD_CONTENT))) {
                if (TYPE_OUTPUT_TEXT.equals(str(at(c, FIELD_TYPE)))) {
                    textBuilder.append(str(at(c, FIELD_TEXT)));
                }
            }
        }
    }
    return textBuilder.isEmpty() ? null : textBuilder.toString();
}

produces the reply concatenated with itself. When the two items' text differs (e.g. an "I'll confirm the payment details for you." commentary item followed by a distinct final-answer item), the result is the commentary preamble run directly into the real answer.

Expected behavior

Only the final_answer-phase item's text should be surfaced as the assistant message text — a commentary/thinking-aloud phase item should not contribute to AiMessage.text(). Responses where no item carries a phase field at all (the ordinary case) should keep today's behavior unchanged.

Impact / frequency

Reproduced on ~2% of plain (no-tool-call) confirmation turns against a gpt-5.6-class model over the Responses API.

Workaround in place (client-side)

A decorator on StreamingChatResponseHandler re-reads the raw response.completed SSE event (buffered on OpenAiResponsesChatResponseMetadata.rawServerSentEvents()), re-parses output, selects only the final_answer-phase item(s), and — only when that text differs from the already-assembled (doubled) text — rebuilds the ChatResponse with the corrected text once.

Suggested fix

extractText (and the streaming completed-event assembly path, which calls the same method) should either:

  • prefer the final_answer-phase item's text when any item in the array carries a phase field, falling back to today's unconditional concat when no item has one (preserves behavior for the Chat Completions API and non-reasoning models), or
  • expose the per-item phase on the assembled response/metadata so callers can select it themselves without re-parsing raw SSE data.

Environment

  • langchain4j-open-ai: 1.20.0 (also confirmed present in 1.14.1, 1.18.1)
  • API: OpenAI Responses API, response.completed event — both streaming and non-streaming paths affected