ai icon indicating copy to clipboard operation
ai copied to clipboard

addAdditionalPropertiesToJsonSchema does not visit in JSON schemas

Open nikolas-sapa opened this issue 1 month ago • 3 comments

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]);
  }
}

nikolas-sapa avatar Oct 25 '26 12:10 nikolas-sapa

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 definitions but omits modern JSON Schema $defs entries.
  • The reproduction shows an object schema under $defs retaining additionalProperties: undefined instead of the expected false.
  • The omission causes strict structured-output validation failures with providers such as OpenAI.

ai-sdk-factory[bot] avatar Oct 25 '26 12:10 ai-sdk-factory[bot]

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 addAdditionalPropertiesToJsonSchema implementation and identifies the missing $defs traversal 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.

ai-sdk-factory[bot] avatar Oct 25 '26 12:10 ai-sdk-factory[bot]

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.

gr2m avatar Oct 27 '26 18:10 gr2m