If you consume raw Responses API JSON, looking for text at one fixed array position can fail as soon as the response includes a reasoning item or a tool call. A small adapter can translate the tagged items into states your interface understands. This example is for a completed, non-streaming response in a text-oriented interface. Its policy is to show no answer on an incomplete response and to treat a refusal in the selected messages as distinct from an empty answer.

Prerequisites

Use Node.js and save this as response.mjs. No SDK, credentials or network connection are required. The fixture functions build small portions of the documented response shape and omit unrelated fields.

import assert from 'node:assert/strict';

function readAnswer(body) {
  if (body?.status !== 'completed') return { kind: 'not-completed' };
  const parts = body.output
    .filter(item => item.type === 'message' && item.role === 'assistant'
      && item.phase !== 'commentary')
    .flatMap(item => item.content);
  if (parts.some(part => part.type === 'refusal')) return { kind: 'refused' };
  const text = parts.filter(part => part.type === 'output_text')
    .map(part => {
      if (typeof part.text !== 'string') throw new TypeError('invalid text');
      return part.text;
    }).join('');
  return text.trim() ? { kind: 'answer', text } : { kind: 'no-text' };
}

const reply = (...content) => ({ type: 'message', role: 'assistant', content });
const body = (...output) => ({ status: 'completed', output });
const text = value => ({ type: 'output_text', text: value });
const answer = readAnswer(body({ type: 'reasoning' },
  { ...reply(text('Checking.')), phase: 'commentary' },
  reply(text('Ships ')), reply(text('Friday.'))));
assert.deepEqual(answer, { kind: 'answer', text: 'Ships Friday.' });
assert.equal(readAnswer(body(reply(text('partial'), { type: 'refusal' }))).kind,
  'refused');
assert.equal(readAnswer(body({ type: 'function_call' })).kind, 'no-text');
assert.equal(readAnswer({ status: 'incomplete' }).kind, 'not-completed');
assert.throws(() => readAnswer(body(reply(text(42)))), TypeError);
console.log(answer.text);

Run node response.mjs. Expected output:

Ships Friday.

Connect the adapter to an application

Check HTTP success at the transport boundary and pass the parsed response object to this adapter. It selects assistant messages and excludes those expressly labeled commentary, while including messages that have no phase label. It joins parts without inserting characters, so the space after Ships remains meaningful. If your interface displays separate messages or citation annotations, preserve the original part boundaries instead of flattening them.

A result of no-text can be normal when the next step is handling a tool call. This adapter does not execute tools or implement that loop. If rendering repeatedly produces not-completed, inspect the response status and incomplete details before assuming there was a parser failure. If a completed response throws while reading output, verify that the input is the parsed response object rather than an HTTP wrapper or a streaming event. This is a selector for the documented shape, not a complete JSON schema validator for arbitrary input.

Reference: Responses API reference. More Javascript tutorials and AI tutorials.