@mastra/schema-compat 1.0.0-beta.3 → 1.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
1
  # @mastra/schema-compat
2
2
 
3
+ ## 1.0.0-beta.5
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed "Transforms cannot be represented in JSON Schema" error when using Zod v4 with structuredOutput ([#11466](https://github.com/mastra-ai/mastra/pull/11466))
8
+
9
+ When using schemas with `.optional()`, `.nullable()`, `.default()`, or `.nullish().default("")` patterns with `structuredOutput` and Zod v4, users would encounter an error because OpenAI schema compatibility layer adds transforms that Zod v4's native `toJSONSchema()` cannot handle.
10
+
11
+ The fix uses Mastra's transform-safe `zodToJsonSchema` function which gracefully handles transforms by using the `unrepresentable: 'any'` option.
12
+
13
+ Also exported `isZodType` utility from `@mastra/schema-compat` and updated it to detect both Zod v3 (`_def`) and Zod v4 (`_zod`) schemas.
14
+
15
+ - fix(schema-compat): handle undefined values in optional fields for OpenAI compat layers ([#11469](https://github.com/mastra-ai/mastra/pull/11469))
16
+
17
+ When a Zod schema has nested objects with `.partial()`, the optional fields would fail validation with "expected string, received undefined" errors. This occurred because the OpenAI schema compat layer converted `.optional()` to `.nullable()`, which only accepts `null` values, not `undefined`.
18
+
19
+ Changed `.nullable()` to `.nullish()` so that optional fields now accept both `null` (when explicitly provided by the LLM) and `undefined` (when fields are omitted entirely).
20
+
21
+ Fixes #11457
22
+
23
+ ## 1.0.0-beta.4
24
+
25
+ ### Patch Changes
26
+
27
+ - Fix OpenAI structured output compatibility for fields with `.default()` values ([#11434](https://github.com/mastra-ai/mastra/pull/11434))
28
+
29
+ When using Zod schemas with `.default()` fields (e.g., `z.number().default(1)`), OpenAI's structured output API was failing with errors like `Missing '<field>' in required`. This happened because `zod-to-json-schema` doesn't include fields with defaults in the `required` array, but OpenAI requires all properties to be required.
30
+
31
+ This fix converts `.default()` fields to `.nullable()` with a transform that returns the default value when `null` is received, ensuring compatibility with OpenAI's strict mode while preserving the original default value semantics.
32
+
3
33
  ## 1.0.0-beta.3
4
34
 
5
35
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -3577,7 +3577,7 @@ function convertZodSchemaToAISDKSchema(zodSchema2, target = "jsonSchema7") {
3577
3577
  });
3578
3578
  }
3579
3579
  function isZodType(value) {
3580
- return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
3580
+ return typeof value === "object" && value !== null && ("_def" in value || "_zod" in value) && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
3581
3581
  }
3582
3582
  function convertSchemaToZod(schema) {
3583
3583
  if (isZodType(schema)) {
@@ -4874,6 +4874,19 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
4874
4874
  return processedInner.nullable();
4875
4875
  }
4876
4876
  return value;
4877
+ } else if (isDefault(zod.z)(value)) {
4878
+ const innerType = "_def" in value ? value._def.innerType : value._zod?.def?.innerType;
4879
+ const defaultValue = "_def" in value ? value._def.defaultValue : value._zod?.def?.defaultValue;
4880
+ if (innerType) {
4881
+ const processedInner = this.processZodType(innerType);
4882
+ return processedInner.nullable().transform((val) => {
4883
+ if (val === null) {
4884
+ return typeof defaultValue === "function" ? defaultValue() : defaultValue;
4885
+ }
4886
+ return val;
4887
+ });
4888
+ }
4889
+ return value;
4877
4890
  } else if (isObj2(zod.z)(value)) {
4878
4891
  return this.defaultZodObjectHandler(value);
4879
4892
  } else if (isUnion2(zod.z)(value)) {
@@ -4997,6 +5010,7 @@ exports.isObj = isObj;
4997
5010
  exports.isOptional = isOptional;
4998
5011
  exports.isString = isString;
4999
5012
  exports.isUnion = isUnion;
5013
+ exports.isZodType = isZodType;
5000
5014
  exports.jsonSchema = jsonSchema;
5001
5015
  //# sourceMappingURL=index.cjs.map
5002
5016
  //# sourceMappingURL=index.cjs.map