-
Notifications
You must be signed in to change notification settings - Fork 18
Description
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