Implementation:Openai Openai node ResponsesParser
| Knowledge Sources | |
|---|---|
| Domains | SDK, Responses API, Parsing |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
ResponsesParser provides the parsing logic for the Responses API auto-parsing feature, transforming raw API responses into typed, parsed output objects.
Description
The ResponsesParser module implements the auto-parsing pipeline for the OpenAI Responses API. The central function maybeParseResponse inspects whether the request parameters include auto-parseable inputs (such as a json_schema text format or auto-parseable tools) and, if so, delegates to parseResponse to produce a ParsedResponse. If no auto-parseable configuration is detected, it returns the response with all parsed and parsed_arguments fields set to null.
The parseResponse function iterates over each output item in the response. For function_call items, it parses the raw JSON arguments string using the corresponding tool's $parseRaw method (for auto-parseable tools) or JSON.parse (for strict tools). For message items with output_text content, it applies the text format's parser. The module defines a lazy output_parsed property on the result that returns the first non-null parsed text content.
The module also exports helper types and functions: AutoParseableResponseTool for defining tools with branded $parseRaw methods, makeParseableResponseTool for constructing them, isAutoParsableTool for runtime type checking, shouldParseToolCall for determining whether a given tool call should be parsed, validateInputTools for ensuring tools meet the strictness requirements, and addOutputText for concatenating output text from response messages.
Usage
Use the ResponsesParser functions when working with the Responses API's structured output features. The SDK calls maybeParseResponse internally when you use client.responses.parse() or configure auto-parseable text formats and tools. You may use makeParseableResponseTool to create custom auto-parseable tools.
Code Reference
Source Location
- Repository: openai-node
- File: src/lib/ResponsesParser.ts
Signature
export function maybeParseResponse<
Params extends ResponseCreateParamsBase | null,
ParsedT = Params extends null ? null : ExtractParsedContentFromParams<NonNullable<Params>>,
>(response: Response, params: Params): ParsedResponse<ParsedT>;
export function parseResponse<
Params extends ResponseCreateParamsBase,
ParsedT = ExtractParsedContentFromParams<Params>,
>(response: Response, params: Params): ParsedResponse<ParsedT>;
export function hasAutoParseableInput(params: ResponseCreateParamsWithTools): boolean;
export function makeParseableResponseTool<OptionsT extends ToolOptions>(
tool: FunctionTool,
options: { parser: (content: string) => any; callback: ((args: any) => any) | undefined },
): AutoParseableResponseTool<OptionsT['arguments']>;
export function isAutoParsableTool(tool: any): tool is AutoParseableResponseTool<any>;
export function shouldParseToolCall(
params: ResponseCreateParamsNonStreaming | null | undefined,
toolCall: ResponseFunctionToolCall,
): boolean;
export function validateInputTools(tools: ChatCompletionTool[] | undefined): void;
export function addOutputText(rsp: Response): void;
Import
import {
maybeParseResponse,
parseResponse,
makeParseableResponseTool,
isAutoParsableTool,
} from 'openai/lib/ResponsesParser';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| response | Response |
Yes | The raw Response object from the Responses API. |
| params | null | Yes | The original request parameters, used to determine which tools and text formats support auto-parsing. |
| tool | FunctionTool |
Yes (for makeParseableResponseTool) | The function tool definition to wrap with auto-parsing capabilities. |
| tools | ChatCompletionTool[] |
No (for validateInputTools) | Array of tools to validate for strictness requirements. |
Outputs
| Name | Type | Description |
|---|---|---|
| ParsedResponse<ParsedT> | ParsedResponse<ParsedT> |
The response object augmented with output_parsed, parsed on text content, and parsed_arguments on function call items.
|
| AutoParseableResponseTool | AutoParseableResponseTool |
A branded tool object with $parseRaw and $callback methods.
|
Usage Examples
import OpenAI from 'openai';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
const client = new OpenAI();
// The SDK internally uses ResponsesParser when you call .parse()
const response = await client.responses.parse({
model: 'gpt-4o',
input: 'Extract the name and age from: John is 30 years old.',
text: {
format: zodTextFormat(z.object({
name: z.string(),
age: z.number(),
}), 'extraction'),
},
});
// response.output_parsed is typed and parsed automatically
console.log(response.output_parsed);
// { name: 'John', age: 30 }