[BUG] Strict schema preparation rejects allOf properties named constructor or toString
Describe the Bug
prepareJsonSchemaForOpenAIStrictMode rejects otherwise mergeable allOf object schemas when a property is named constructor, toString, or hasOwnProperty. The field can appear in only one branch, yet the error says it is defined differently in multiple branches. Renaming the field to name succeeds.
In mergeAllOfSubschemas, mergedProps is an ordinary object and propName in mergedProps also matches inherited properties from Object.prototype. This incorrectly treats those inherited values as earlier schema definitions. An own __proto__ property also fails.
The helper is called when preparing OpenAI strict structured output, before the provider request. This also reproduces with Zod v3 object intersections converted through zodToJsonSchema; no API key or model call is needed.
Steps To Reproduce
At upstream commit 2a832580e3cf3efcdb4be3355eaa9a02929b3a2c, after installing the workspace dependencies, save this as repro-allof.ts in the repository root:
import { prepareJsonSchemaForOpenAIStrictMode } from "./packages/schema-compat/src/zod-to-json.ts";
console.log(prepareJsonSchemaForOpenAIStrictMode({
allOf: [
{ type: "object", properties: { constructor: { type: "string" } } },
{ type: "object", properties: { age: { type: "number" } } },
],
}));Run pnpm exec tsx repro-allof.ts.
Actual result:
Cannot flatten allOf for OpenAI strict mode: property "constructor" is defined differently in multiple branchesThe linked regression tests fail on the unmodified implementation and pass with the proposed fix.
Link to Minimal Reproducible Example
Expected Behavior
Merge the two distinct fields successfully, preserving constructor and age as own properties, with both fields required and additionalProperties: false. Identical duplicate definitions should still merge, while genuinely conflicting definitions should still throw.
Environment Information
Mastra source commit: 2a832580e3cf3efcdb4be3355eaa9a02929b3a2c
Package: @mastra/schema-compat 1.3.11-alpha.1
OS: Windows
Node.js: 22.15.1 (reproduction and tests), 24.19.0 (lint and build)
pnpm: 11.21.0
Provider/browser: not required; deterministic schema conversionVerification
- I have searched the existing issues to make sure this is not a duplicate
- I have included sufficient information for the team to reproduce and understand the issue
Source: mastra-ai/mastra