Skip to content

Zod v4's toJSONSchema() includes hidden ~standard property causing 400 errors with downstream providers #131

@patrykgz

Description

@patrykgz

When using Zod v4's toJSONSchema() method to define structured outputs, the resulting schema includes a non-enumerable ~standard property (from the [Standard Schema spec](https://standardschema.dev/)). This property is not visible when logging with JSON.stringify(), but it is included when the SDK serializes the request, causing 400 errors from providers like Anthropic that reject unknown schema properties.

To Reproduce

import { z } from "zod";

const schema = z.object({
  name: z.string(),
});

const jsonSchema = schema.toJSONSchema();

// This won't show the ~standard property
console.log(JSON.stringify(jsonSchema, null, 2));

// But it exists and gets sent in the request
console.log(Object.keys(jsonSchema)); // includes "~standard"

Expected Behavior

The SDK should strip non-standard properties (or at minimum properties prefixed with ~) before sending the schema to providers.

Workaround

const zodSchema = options.outputSchema?.toJSONSchema();
let jsonSchema: Record<string, unknown> | undefined;

if (zodSchema) {
  const { "~standard": _, ...rest } = zodSchema;
  jsonSchema = rest;
}

Suggested Fix

Sanitize JSON schemas before sending requests by stripping keys prefixed with ~. This would prevent Standard Schema metadata from leaking into API calls.

If you disagree with this approach, feel free to close, but leaving this here in case others hit the same issue. I only found out after using a mitmproxy lol

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions