npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

openai-zod-validator

v1.0.0

Published

Structured output validation and transformation tool for OpenAI SDK 6 with Zod 4 schema validation

Readme

OpenAI Zod Validator

A powerful TypeScript library for validating OpenAI SDK 6 structured outputs and function calls using Zod 4 schemas. Ensures type-safe AI responses with full TypeScript inference.

Features

  • ✅ Full TypeScript support with type inference
  • ✅ Compatible with OpenAI SDK 6.x
  • ✅ Built for Zod 4.x schemas
  • ✅ Validates structured outputs, function calls, and tool calls
  • ✅ Convert Zod schemas to OpenAI function parameters
  • ✅ Comprehensive error handling
  • ✅ Zero configuration required

Installation

npm install openai-zod-validator openai zod

Quick Start

import OpenAI from 'openai';
import { z } from 'zod';
import { createOpenAIValidator } from 'openai-zod-validator';

// Define your Zod schema
const ResponseSchema = z.object({
  name: z.string(),
  age: z.number(),
  email: z.string().email(),
});

// Create a validator
const validator = createOpenAIValidator(ResponseSchema);

// Use with OpenAI
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const completion = await openai.chat.completions.create({
  model: 'gpt-4',
  messages: [{ role: 'user', content: 'Generate user data' }],
  response_format: { type: 'json_object' },
});

// Validate the response
const result = validator.validateCompletion(completion);
console.log(result.data); // Fully typed as { name: string; age: number; email: string }

Usage with OpenAI SDK 6

1. Structured Outputs (JSON Mode)

import OpenAI from 'openai';
import { z } from 'zod';
import { createOpenAIValidator } from 'openai-zod-validator';

const openai = new OpenAI();

// Define schema
const RecipeSchema = z.object({
  name: z.string(),
  ingredients: z.array(z.string()),
  steps: z.array(z.string()),
  cookingTime: z.number(),
});

const validator = createOpenAIValidator(RecipeSchema);

async function generateRecipe() {
  const completion = await openai.chat.completions.create({
    model: 'gpt-4-turbo-preview',
    messages: [
      {
        role: 'user',
        content: 'Generate a recipe for chocolate chip cookies',
      },
    ],
    response_format: { type: 'json_object' },
  });

  const result = validator.validateCompletion(completion);

  if (result.success) {
    console.log('Recipe:', result.data); // Fully typed!
  } else {
    console.error('Validation errors:', result.errors);
  }
}

2. Function Calling

import OpenAI from 'openai';
import { z } from 'zod';
import { createOpenAIValidator, zodToOpenAIParameters } from 'openai-zod-validator';

const openai = new OpenAI();

// Define function parameter schema
const GetWeatherSchema = z.object({
  location: z.string(),
  unit: z.enum(['celsius', 'fahrenheit']),
});

const validator = createOpenAIValidator(GetWeatherSchema);

async function callWithFunctions() {
  const completion = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [
      {
        role: 'user',
        content: 'What is the weather in San Francisco?',
      },
    ],
    functions: [
      {
        name: 'get_weather',
        description: 'Get the current weather in a location',
        parameters: zodToOpenAIParameters(GetWeatherSchema),
      },
    ],
    function_call: 'auto',
  });

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

  if (message?.function_call) {
    const result = validator.validateFunctionCall(message.function_call);

    if (result.success) {
      console.log('Function arguments:', result.data);
      // result.data is typed as { location: string; unit: 'celsius' | 'fahrenheit' }
    }
  }
}

3. Tool Calls (OpenAI SDK 6 Tools API)

import OpenAI from 'openai';
import { z } from 'zod';
import { createOpenAIValidator, zodToOpenAIParameters } from 'openai-zod-validator';

const openai = new OpenAI();

const SearchSchema = z.object({
  query: z.string(),
  limit: z.number().optional(),
});

const validator = createOpenAIValidator(SearchSchema);

async function callWithTools() {
  const completion = await openai.chat.completions.create({
    model: 'gpt-4-turbo-preview',
    messages: [
      {
        role: 'user',
        content: 'Search for TypeScript tutorials',
      },
    ],
    tools: [
      {
        type: 'function',
        function: {
          name: 'search',
          description: 'Search for content',
          parameters: zodToOpenAIParameters(SearchSchema),
        },
      },
    ],
  });

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

  if (message?.tool_calls) {
    for (const toolCall of message.tool_calls) {
      const result = validator.validateToolCall(toolCall);

      if (result.success) {
        console.log('Tool arguments:', result.data);
      }
    }
  }
}

4. Direct Data Validation

import { z } from 'zod';
import { createOpenAIValidator } from 'openai-zod-validator';

const UserSchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().email(),
  age: z.number().min(0),
});

const validator = createOpenAIValidator(UserSchema);

// Validate any data
const data = {
  id: '123',
  name: 'John Doe',
  email: '[email protected]',
  age: 30,
};

const result = validator.validate(data);

if (result.success) {
  console.log('Valid user:', result.data);
} else {
  console.error('Validation errors:', result.errors);
}

API Reference

createOpenAIValidator(schema, options?)

Creates a validator instance for a Zod schema.

Parameters:

  • schema: A Zod schema
  • options?: Optional configuration
    • throwOnError?: boolean - Whether to throw on validation errors (default: true)
    • stripUnknown?: boolean - Whether to strip unknown keys (default: false)

Returns: Validator instance with methods:

  • validate(data) - Validate any data
  • validateCompletion(completion) - Validate OpenAI chat completion
  • validateFunctionCall(functionCall) - Validate function call arguments
  • validateToolCall(toolCall) - Validate tool call arguments
  • getSchema() - Get the underlying Zod schema

zodToOpenAIParameters(schema)

Converts a Zod schema to OpenAI function parameters format.

Parameters:

  • schema: A Zod schema

Returns: OpenAI-compatible parameter object

Advanced Examples

Custom Error Handling

const validator = createOpenAIValidator(MySchema, {
  throwOnError: false
});

const result = validator.validate(data);

if (!result.success) {
  result.errors?.errors.forEach(error => {
    console.log(`Error at ${error.path.join('.')}: ${error.message}`);
  });
}

Complex Nested Schemas

const AddressSchema = z.object({
  street: z.string(),
  city: z.string(),
  zipCode: z.string(),
});

const PersonSchema = z.object({
  name: z.string(),
  age: z.number(),
  addresses: z.array(AddressSchema),
  metadata: z.record(z.string()),
});

const validator = createOpenAIValidator(PersonSchema);

// Full type inference works with nested objects
const result = validator.validate(data);
// result.data is fully typed with all nested properties

Using with Streaming

const validator = createOpenAIValidator(ResponseSchema);

const stream = await openai.chat.completions.create({
  model: 'gpt-4',
  messages: [{ role: 'user', content: 'Generate data' }],
  stream: true,
  response_format: { type: 'json_object' },
});

let accumulatedContent = '';

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content || '';
  accumulatedContent += content;
}

// Validate accumulated content
const result = validator.validate(JSON.parse(accumulatedContent));

TypeScript Support

This library is built with TypeScript and provides full type inference:

const Schema = z.object({
  name: z.string(),
  count: z.number(),
});

const validator = createOpenAIValidator(Schema);
const result = validator.validate(data);

if (result.success) {
  // result.data is automatically typed as:
  // { name: string; count: number }
  console.log(result.data.name.toUpperCase());
  console.log(result.data.count * 2);
}

Error Handling

The library provides detailed error information:

const validator = createOpenAIValidator(Schema, { throwOnError: false });
const result = validator.validate(invalidData);

if (!result.success) {
  result.errors?.errors.forEach(error => {
    console.log('Path:', error.path);
    console.log('Message:', error.message);
    console.log('Code:', error.code);
  });
}

Requirements

  • Node.js >= 18.0.0
  • OpenAI SDK >= 6.0.0
  • Zod >= 4.0.0

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions, please open an issue on GitHub.