addAdditionalPropertiesToJsonSchema does not visit in JSON schemas
Description
addAdditionalPropertiesToJsonSchema recursively traverses schema objects to append additionalProperties: false (necessary for OpenAI structured outputs and other providers requiring strict schema adherence).
However, it currently only visits definitions (JSON Schema Draft-07 style):
const { definitions } = jsonSchema;
if (definitions != null) {
for (const key of Object.keys(definitions)) {
definitions[key] = visit(definitions[key]);
}
}
In JSON Schema Draft 2019-09, Draft 2020-12, OpenAPI 3.1, Zod 4, and TypeBox, schema references and definitions reside under $defs instead of definitions.
When schemas containing $defs are supplied (such as schemas generated from TypeBox, StandardSchema adapters, or modern JSON schema emitters), the subschemas located in $defs are omitted by addAdditionalPropertiesToJsonSchema. As a result, those subschemas do not receive additionalProperties: false, causing downstream API validation errors (e.g. OpenAI rejecting requests in strict mode with 'additionalProperties' must be false in strict mode).
Reproduction
import { addAdditionalPropertiesToJsonSchema } from '@ai-sdk/provider-utils';
const schema = {
type: 'object',
properties: {
node: { $ref: '#/$defs/Node' },
},
$defs: {
Node: {
type: 'object',
properties: {
value: { type: 'string' },
},
},
},
};
const result = addAdditionalPropertiesToJsonSchema(schema);
console.log(result.$defs.Node.additionalProperties);
// Expected: false
// Actual: undefined
Proposed Fix
Recursively visit $defs alongside definitions:
const { $defs } = jsonSchema as {
$defs?: Record<string, JSONSchema7Definition>;
};
if ($defs != null) {
for (const key of Object.keys($defs)) {
$defs[key] = visit($defs[key]);
}
}
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete traversal defect in existing schema-processing behavior: referenced object subschemas under $defs are not updated as expected.
Evidence
- The utility recursively handles
definitionsbut omits modern JSON Schema$defsentries. - The reproduction shows an object schema under
$defsretainingadditionalProperties: undefinedinstead of the expectedfalse. - The omission causes strict structured-output validation failures with providers such as OpenAI.
Identify bug discovery
Discovery source: discovery:audit (medium confidence).
The report is centered on inspection of the SDK utility’s traversal implementation, identifying that it handles definitions but omits $defs, and proposes a direct source-level fix. There is no concrete claim that the bug was encountered in a production application.
- The description quotes the existing
addAdditionalPropertiesToJsonSchemaimplementation and identifies the missing$defstraversal branch. - The proposed fix directly modifies the utility’s recursive source logic to visit
$defs. - The reproduction is a focused utility-level schema test rather than evidence of a production application failure.
- Downstream API validation errors are described as a possible result, but no actual production incident is stated.
If you find bugs through an agent audit, please share the prompts you used instead. Please see our updated guidelines at https://github.com/vercel/ai/issues/17243. If you have any questions, comment on #24118.