Implementation:Openai Openai node ZodFunction
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, Schema_Validation |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for creating typed, auto-parseable tool definitions from Zod schemas provided by the openai-node SDK.
Description
The zodFunction helper creates an AutoParseableTool for the Chat Completions API, and zodResponsesFunction creates an AutoParseableResponseTool for the Responses API. Both accept a Zod schema for parameters, an optional implementation callback, and produce a tool specification with automatic argument parsing.
When used with runTools(), the SDK automatically: (1) sends the tool to the API, (2) parses the model's arguments via the Zod schema, (3) calls the implementation function, and (4) sends the result back to the model.
Usage
Use zodFunction with client.chat.completions.runTools() for automatic tool execution, or with client.chat.completions.parse() for auto-parsed tool call arguments without automatic execution.
Code Reference
Source Location
- Repository: openai-node
- File: src/helpers/zod.ts
- Lines: L123-180 (zodFunction: L123-152, zodResponsesFunction: L154-180)
Signature
export function zodFunction<Parameters extends z3.ZodType | z4.ZodType>(options: {
name: string;
parameters: Parameters;
function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined;
description?: string | undefined;
}): AutoParseableTool<{
arguments: Parameters;
name: string;
function: (args: InferZodType<Parameters>) => unknown;
}>;
export function zodResponsesFunction<Parameters extends z3.ZodType | z4.ZodType>(options: {
name: string;
parameters: Parameters;
function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined;
description?: string | undefined;
}): AutoParseableResponseTool<{
arguments: Parameters;
name: string;
function: (args: InferZodType<Parameters>) => unknown;
}>;
Import
import { zodFunction, zodResponsesFunction } from 'openai/helpers/zod';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | Tool function name (sent to the model) |
| parameters | ZodType (v3 or v4) | Yes | Zod schema defining the function arguments |
| function | (args: T) => unknown | No | Implementation callback (required for runTools) |
| description | string | No | Description helping the model understand when to use this tool |
Outputs
| Name | Type | Description |
|---|---|---|
| (zodFunction) | AutoParseableTool | Tool spec for Chat Completions with JSON Schema and auto-parsing |
| (zodResponsesFunction) | AutoParseableResponseTool | Tool spec for Responses API with JSON Schema and auto-parsing |
Usage Examples
Defining Tools with zodFunction
import { z } from 'zod';
import { zodFunction } from 'openai/helpers/zod';
const getWeather = zodFunction({
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: z.object({
location: z.string().describe('City name'),
unit: z.enum(['celsius', 'fahrenheit']).optional(),
}),
function: async ({ location, unit }) => {
// Call weather API
return { temperature: 22, condition: 'sunny', location };
},
});
Using with runTools
import OpenAI from 'openai';
const client = new OpenAI();
const runner = client.chat.completions.runTools({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
tools: [getWeather],
});
const result = await runner.finalChatCompletion();
console.log(result.choices[0].message.content);