Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/lib/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,21 @@ function ensureStrictJsonSchema(
jsonSchema.items = ensureStrictJsonSchema(items, [...path, 'items'], root);
}

// Handle unions (anyOf)
// Handle unions (anyOf). Zod v4 emits oneOf for discriminated unions, but
// OpenAI strict schemas only support anyOf.
const oneOf = jsonSchema.oneOf;
if (Array.isArray(oneOf)) {
const existingAnyOf = jsonSchema.anyOf;
delete jsonSchema.oneOf;

if (Array.isArray(existingAnyOf)) {
jsonSchema.allOf = [...(jsonSchema.allOf ?? []), { anyOf: existingAnyOf }, { anyOf: oneOf }];
delete jsonSchema.anyOf;
} else {
jsonSchema.anyOf = oneOf;
}
}

const anyOf = jsonSchema.anyOf;
if (Array.isArray(anyOf)) {
jsonSchema.anyOf = anyOf.map((variant, i) =>
Expand Down
16 changes: 16 additions & 0 deletions tests/helpers/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ describe.each([
`);
});

it('converts Zod discriminated unions to strict anyOf schemas', () => {
const ResponseSchema = z.object({
data: z.discriminatedUnion('type', [
z.object({ type: z.literal('a') }),
z.object({ type: z.literal('b') }),
]),
});

const schema = zodResponseFormat(ResponseSchema, 'choice').json_schema.schema as Record<string, any>;
const dataSchema = schema['properties']['data'];

expect(dataSchema.oneOf).toBeUndefined();
expect(dataSchema.anyOf).toHaveLength(2);
expect(JSON.stringify(schema)).not.toContain('"oneOf"');
});

it('allows description field to be passed in', () => {
expect(
zodResponseFormat(
Expand Down
67 changes: 67 additions & 0 deletions tests/lib/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,73 @@ describe('toStrictJsonSchema', () => {
}
`);
});

test('keeps existing anyOf separate when converting oneOf', () => {
const schema: JSONSchema = {
type: 'object',
properties: {
value: {
anyOf: [
{
type: 'object',
properties: { str: { type: 'string' } },
required: ['str'],
},
],
oneOf: [
{
type: 'object',
properties: { num: { type: 'number' } },
required: ['num'],
},
],
},
},
required: ['value'],
};

const strict = toStrictJsonSchema(schema);
const valueSchema = strict.properties?.['value'] as JSONSchema;

expect(valueSchema.oneOf).toBeUndefined();
expect(valueSchema.anyOf).toBeUndefined();
expect(valueSchema.allOf).toMatchInlineSnapshot(`
[
{
"anyOf": [
{
"additionalProperties": false,
"properties": {
"str": {
"type": "string",
},
},
"required": [
"str",
],
"type": "object",
},
],
},
{
"anyOf": [
{
"additionalProperties": false,
"properties": {
"num": {
"type": "number",
},
},
"required": [
"num",
],
"type": "object",
},
],
},
]
`);
});
});

describe('allOf Handling', () => {
Expand Down