openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

helpers/standard-schema: normalizeStructuredOutputSchema rewrites `oneOf` into `anyOf` on object nodes, then toStrictJsonSchema rejects its own output

Open percymcn opened this issue 3 months ago • 1 comments

Summary

On [email protected], helpers/standard-schema and helpers/zod disagree about one node shape: an object schema that also carries oneOf. resolveStandardJSONSchema() runs

return toStrictJsonSchema(normalizeStructuredOutputSchema(schema));

and normalizeStructuredOutputSchema() moves oneOf branches into anyOf — which produces exactly the shape toStrictJsonSchema() refuses two lines later ("Object anyOf schema at … cannot be represented…"). The same document handed straight to toStrictJsonSchema() is accepted byte-identical.

So whether a schema is "valid" depends on which helper you call, at the same SDK version, and the standard-schema path fails on input it did not have to touch.

Repro (no API key, [email protected])

import { toStrictJsonSchema } from "openai/lib/transform.js";
import { standardResponseFormat } from "openai/helpers/standard-schema.js";

const doc = {
  type: "object",
  properties: {
    f: {
      type: "object",
      properties: { a: { type: "string" } },
      required: ["a"],
      additionalProperties: false,
      oneOf: [{ type: "string" }, { type: "number" }],
    },
  },
  required: ["f"],
  additionalProperties: false,
};

const asStandard = (s) => ({ "~standard": {
  version: 1, vendor: "repro", validate: (v) => ({ value: v }),
  jsonSchema: { input: () => s },
} });

toStrictJsonSchema(doc);                    // ACCEPT (returned byte-identical)
standardResponseFormat(asStandard(doc), "n"); // THROW
CONTROL (plain object)
  toStrictJsonSchema      : ACCEPT
  standardResponseFormat  : ACCEPT

object node carrying `oneOf`
  toStrictJsonSchema      : ACCEPT
  standardResponseFormat  : THROW: Object anyOf schema at `properties/f` cannot be
                            represented in strict Structured Outputs without changing
                            Draft 7 validation.

The rewrite is the cause, not the input. Performing it by hand and handing the result to toStrictJsonSchema() reproduces the identical error:

const rewritten = structuredClone(doc);
rewritten.properties.f.anyOf = rewritten.properties.f.oneOf;
delete rewritten.properties.f.oneOf;
toStrictJsonSchema(rewritten); // THROW: Object anyOf schema at `properties/f` …

Reachability — stated plainly, because it is narrower than it looks

This is not reachable from zod, and I checked rather than assumed. z.discriminatedUnion() does emit oneOf, but at a bare union node with no object shape, so it is accepted by both families when nested; at the root both correctly reject it for an unrelated reason (Root schema must have type: 'object'). Measured on [email protected]:

zodResponseFormat standardResponseFormat
nested z.discriminatedUnion ACCEPT ACCEPT
root z.discriminatedUnion THROW (root type) THROW (root type)

The route that does reach it is the documented escape hatch: props.schema, which lets a caller pass a raw JSON Schema (const { schema, ...formatProps } = props ?? {}). Anyone adapting an existing JSON Schema — OpenAPI-derived or hand-written, where oneOf beside properties is an ordinary idiom — goes through normalizeStructuredOutputSchema() and gets a throw on a document the zod path accepts unchanged.

Suggested fix

Skip the oneOf → anyOf move when the node has object shape and the result would be refused — i.e. when hasObjectShape(node) and the existing escape hatch in the object-anyOf check would not apply (redundant object wrapper type, no object keywords of its own, every branch object-only). Leaving oneOf in place there is strictly better than rewriting it: toStrictJsonSchema() already accepts that form verbatim.

One test-design note

A regression test written on a z.discriminatedUnion fixture passes either way — that shape is a bare union node, which is precisely the case the rewrite handles correctly. The failing case needs oneOf on a node that also declares type: "object" plus at least one object keyword, so it is worth pinning with a raw-JSON-Schema fixture rather than a validator-derived one.

percymcn avatar Aug 10 '26 08:08 percymcn

Thanks for documenting the discrepancy between the local helpers. We verified the example against the actual Structured Outputs validators: strict response-format and function schemas reject oneOf, including both this example and otherwise satisfiable object-constrained unions. The example is also unsatisfiable because its value must simultaneously be an object and either a string or a number. The lower-level toStrictJsonSchema helper accepting such a document does not mean the API accepts it; safe unions should normalize to anyOf, and unsupported constrained shapes should fail closed. Closing because preserving oneOf would violate the actual strict-schema contract.

HAYDEN-OAI avatar Aug 12 '26 16:08 HAYDEN-OAI