Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai node Parsed Output Access

From Leeroopedia
Knowledge Sources
Domains Schema_Validation, NLP
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete access pattern for consuming typed, parsed output from Chat Completion and Responses API results provided by the openai-node SDK.

Description

After calling completions.parse() or responses.parse(), the SDK attaches parsed results to the response objects. For Chat Completions, the parsed content is at completion.choices[i].message.parsed (typed as ParsedT | null). For the Responses API, the parsed content is at response.output_parsed (a computed getter that finds the first text output and parses it).

The message.parsed assignment occurs in parseChatCompletion() (src/lib/parser.ts:L203-206). The output_parsed getter is defined in parseResponse() (src/lib/ResponsesParser.ts:L102-119).

Usage

Access the .parsed or .output_parsed property after a parse API call. Always null-check the value, as it will be null for model refusals or empty content.

Code Reference

Source Location

  • Repository: openai-node
  • File: src/lib/parser.ts (Chat Completions), src/lib/ResponsesParser.ts (Responses API)
  • Lines: parser.ts:L203-206 (message.parsed assignment), ResponsesParser.ts:L102-119 (output_parsed getter)

Signature

// Chat Completions parsed output:
interface ParsedChatCompletionMessage<ParsedT> extends ChatCompletionMessage {
  parsed: ParsedT | null;
  tool_calls?: Array<ParsedFunctionToolCall>;
}

// Responses API parsed output:
interface ParsedResponse<ParsedT> {
  output_parsed: ParsedT | null;
  // ... other Response fields
}

Import

import OpenAI from 'openai';
// Types are automatically available from parse() return type

I/O Contract

Inputs

Name Type Required Description
completion ParsedChatCompletion<ParsedT> Yes Result from completions.parse()
response ParsedResponse<ParsedT> Yes (alternative) Result from responses.parse()

Outputs

Name Type Description
message.parsed null Typed parsed content (null if refusal or empty)
message.refusal null Model refusal message (if refused)
output_parsed null Typed parsed content from Responses API

Usage Examples

Chat Completions Parsed Output

import { z } from 'zod';
import OpenAI from 'openai';
import { zodResponseFormat } from 'openai/helpers/zod';

const client = new OpenAI();
const Person = z.object({ name: z.string(), age: z.number() });

const completion = await client.chat.completions.parse({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Extract: Alice is 25.' }],
  response_format: zodResponseFormat(Person, 'person'),
});

const message = completion.choices[0].message;

// Check for refusal
if (message.refusal) {
  console.log('Model refused:', message.refusal);
} else if (message.parsed) {
  // Typed as { name: string, age: number }
  console.log(message.parsed.name);  // 'Alice'
  console.log(message.parsed.age);   // 25
}

Responses API Parsed Output

import { z } from 'zod';
import OpenAI from 'openai';
import { zodTextFormat } from 'openai/helpers/zod';

const client = new OpenAI();
const Summary = z.object({ title: z.string(), points: z.array(z.string()) });

const response = await client.responses.parse({
  model: 'gpt-4o',
  input: 'Summarize the benefits of TypeScript.',
  text: { format: zodTextFormat(Summary, 'summary') },
});

if (response.output_parsed) {
  console.log(response.output_parsed.title);
  console.log(response.output_parsed.points);
}

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment