Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai node Completions Parse

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

Overview

Concrete tool for making auto-parsed Chat Completion API calls with schema-validated typed responses provided by the openai-node SDK.

Description

The Completions.parse() method wraps Completions.create() with automatic response parsing. It calls the standard endpoint, then uses parseChatCompletion() to detect AutoParseable format objects and invoke their $parseRaw methods on the response content. The result is a ParsedChatCompletion<ParsedT> with a message.parsed property containing the typed, validated data.

The method also validates input tools to ensure they have proper parsing functions attached.

Usage

Use this method when your request includes zodResponseFormat as response_format or zodFunction tools. It provides automatic parsing of both structured output content and tool call arguments.

Code Reference

Source Location

  • Repository: openai-node
  • File: src/resources/chat/completions/completions.ts
  • Lines: L141-156 (parse method)

Signature

class Completions extends APIResource {
  parse<
    Params extends ChatCompletionParseParams,
    ParsedT = ExtractParsedContentFromParams<Params>,
  >(
    body: Params,
    options?: RequestOptions,
  ): APIPromise<ParsedChatCompletion<ParsedT>>;
}

Import

import OpenAI from 'openai';
// Access via: client.chat.completions.parse(...)

I/O Contract

Inputs

Name Type Required Description
body ChatCompletionParseParams Yes Non-streaming request params with AutoParseable response_format or tools
options RequestOptions No Per-request overrides

Outputs

Name Type Description
result APIPromise<ParsedChatCompletion<ParsedT>> Completion with choices[].message.parsed: ParsedT (typed, validated object)

Usage Examples

Structured Output Parsing

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

const client = new OpenAI();

const Event = z.object({
  name: z.string(),
  date: z.string(),
  participants: z.array(z.string()),
});

const completion = await client.chat.completions.parse({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'Extract event info.' },
    { role: 'user', content: 'The annual meeting is on March 5th with Alice and Bob.' },
  ],
  response_format: zodResponseFormat(Event, 'event'),
});

const event = completion.choices[0].message.parsed;
// event is typed as { name: string, date: string, participants: string[] }
if (event) {
  console.log(event.name, event.date, event.participants);
}

Related Pages

Implements Principle

Requires Environment

Page Connections

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