Implementation:Openai Openai node Responses Resource
| Knowledge Sources | |
|---|---|
| Domains | SDK, Responses API, Inference |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Responses resource class is the primary interface for creating, retrieving, streaming, and managing model responses through the OpenAI Responses API.
Description
The Responses class extends APIResource and provides the core methods for interacting with the OpenAI Responses endpoint (/responses). This is the main API for generating text, JSON, and structured outputs from OpenAI models. The class supports six operations: create for generating responses (with streaming overloads), retrieve for fetching existing responses, delete for removing responses, cancel for stopping background responses, parse for structured output parsing with Zod schemas, stream for creating managed response streams, and compact for compacting conversation context.
The module is the largest type definition file in the SDK at 7057 lines, defining the complete type system for the Responses API. This includes over 100 exported types covering input types (messages, images, audio, files), output types (text, refusals, function calls, reasoning, code interpreter results), tool definitions (function, file search, web search, computer use, shell, code interpreter, MCP, apply patch, custom), stream events (deltas for text, audio, function call arguments, reasoning summaries), and configuration types (tool choice, response format, truncation, conversation state).
The class has two sub-resources: inputItems for listing input items of a response, and inputTokens for counting input tokens before making a request. The parse method integrates with the SDK's structured output system to return typed ParsedResponse objects with parsed output from Zod schemas. The stream method returns a ResponseStream that provides high-level event handling for streaming responses.
Usage
Use this resource for all text generation, structured output, tool-using, and multimodal inference tasks with OpenAI models. It supports both simple single-turn requests and multi-turn conversations via previous_response_id. The streaming mode provides real-time token delivery for responsive user interfaces.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/responses/responses.ts
Signature
export class Responses extends APIResource {
inputItems: InputItemsAPI.InputItems;
inputTokens: InputTokensAPI.InputTokens;
create(body: ResponseCreateParamsNonStreaming, options?: RequestOptions): APIPromise<Response>;
create(body: ResponseCreateParamsStreaming, options?: RequestOptions): APIPromise<Stream<ResponseStreamEvent>>;
retrieve(responseID: string, query?: ResponseRetrieveParamsNonStreaming, options?: RequestOptions): APIPromise<Response>;
retrieve(responseID: string, query: ResponseRetrieveParamsStreaming, options?: RequestOptions): APIPromise<Stream<ResponseStreamEvent>>;
delete(responseID: string, options?: RequestOptions): APIPromise<void>;
parse<Params, ParsedT>(body: Params, options?: RequestOptions): APIPromise<ParsedResponse<ParsedT>>;
stream<Params, ParsedT>(body: Params, options?: RequestOptions): ResponseStream<ParsedT>;
cancel(responseID: string, options?: RequestOptions): APIPromise<Response>;
compact(body: ResponseCompactParams, options?: RequestOptions): APIPromise<CompactedResponse>;
}
Import
import OpenAI from 'openai';
I/O Contract
Inputs (create)
| Name | Type | Required | Description |
|---|---|---|---|
| model | ResponsesModel |
Yes | Model ID (e.g., gpt-4o, o3, gpt-4.1) |
| input | Array<ResponseInputItem> | null | No | Text, image, or file inputs to the model |
| instructions | null | No | System/developer message for the model |
| stream | null | No | Enable streaming mode |
| tools | Array<Tool> |
No | Tools available to the model |
| tool_choice | ToolChoiceFunction | ToolChoiceMcp | No | How the model selects tools |
| text | ResponseTextConfig |
No | Text output configuration (format, response format) |
| temperature | null | No | Sampling temperature (0-2) |
| top_p | null | No | Nucleus sampling parameter |
| max_output_tokens | null | No | Maximum tokens in the response |
| reasoning | Reasoning |
No | Reasoning configuration for o-series and gpt-5+ models |
| previous_response_id | null | No | Previous response ID for multi-turn conversations |
| metadata | null | No | Key-value pairs for storing additional information |
| include | Array<ResponseIncludable> |
No | Additional fields to include in output |
| parallel_tool_calls | boolean |
No | Allow parallel tool execution |
| background | null | No | Run response in the background (can be cancelled) |
Outputs
| Name | Type | Description |
|---|---|---|
| id | string |
Unique response identifier |
| object | 'response' |
Always 'response' |
| created_at | number |
Unix timestamp of creation |
| model | ResponsesModel |
Model used |
| output | Array<ResponseOutputItem> |
Generated content items |
| output_text | string |
Convenience accessor for concatenated text output |
| status | ResponseStatus |
Response status (completed, failed, incomplete, in_progress, queued, cancelled) |
| usage | ResponseUsage |
Token usage details |
| error | null | Error information if failed |
| metadata | null | Associated metadata |
| temperature | null | Temperature used |
| parallel_tool_calls | boolean |
Whether parallel tool calls were allowed |
Tool Types
| Tool | Type Constant | Description |
|---|---|---|
| FunctionTool | 'function' |
Custom function calling with JSON Schema parameters |
| FileSearchTool | 'file_search' |
Search uploaded files in vector stores |
| WebSearchTool | 'web_search' |
Search the web for current information |
| ComputerTool | 'computer_use_preview' |
Control a virtual computer environment |
| FunctionShellTool | 'shell' |
Execute shell commands |
| ApplyPatchTool | 'apply_patch' |
Create, delete, or update files via unified diffs |
| CustomTool | 'custom' |
Custom tool with specified input format |
| CodeInterpreter | 'code_interpreter' |
Execute code in a sandboxed environment |
| McpTool | 'mcp' |
Remote Model Context Protocol server tools |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI();
// Simple text generation
const response = await client.responses.create({
model: 'gpt-4o',
input: 'Explain quantum computing in simple terms.',
});
console.log(response.output_text);
// Streaming response
const stream = client.responses.stream({
model: 'gpt-4o',
input: 'Write a haiku about programming.',
});
for await (const event of stream) {
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
}
}
// Multi-turn conversation
const firstResponse = await client.responses.create({
model: 'gpt-4o',
input: 'What is the capital of France?',
});
const followUp = await client.responses.create({
model: 'gpt-4o',
input: 'What is its population?',
previous_response_id: firstResponse.id,
});
// Retrieve a response
const retrieved = await client.responses.retrieve('resp_677efb5139a88190b512bc3fef8e535d');
// Delete a response
await client.responses.delete('resp_677efb5139a88190b512bc3fef8e535d');
// Cancel a background response
const cancelled = await client.responses.cancel('resp_677efb5139a88190b512bc3fef8e535d');
// With tools
const toolResponse = await client.responses.create({
model: 'gpt-4o',
input: 'Search the web for the latest AI news.',
tools: [{ type: 'web_search' }],
});
Key Types
ResponseOutputItem (discriminated union)
type ResponseOutputItem =
| ResponseOutputMessage
| ResponseFunctionToolCall
| ResponseFileSearchToolCall
| ResponseFunctionWebSearch
| ResponseComputerToolCall
| ResponseReasoningItem
| ResponseCompactionItem
| ResponseCodeInterpreterToolCall
| ResponseCustomToolCall
// ... and more
ResponseStreamEvent
// Key streaming events include:
// response.created, response.in_progress, response.completed
// response.output_item.added, response.output_item.done
// response.content_part.added, response.content_part.done
// response.output_text.delta, response.output_text.done
// response.refusal.delta, response.refusal.done
// response.function_call_arguments.delta, response.function_call_arguments.done
// response.reasoning_summary_text.delta
ParsedResponse
interface ParsedResponse<ParsedT> extends Response {
output: Array<ParsedResponseOutputItem<ParsedT>>;
output_parsed: ParsedT | null;
}