@mastra/schema-compat 0.11.7-alpha.0 → 0.11.8-alpha.0

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,47 @@
1
1
  # @mastra/schema-compat
2
2
 
3
+ ## 0.11.8-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed OpenAI schema compatibility when using `agent.generate()` or `agent.stream()` with `structuredOutput`. ([#10454](https://github.com/mastra-ai/mastra/pull/10454))
8
+
9
+ ## Changes
10
+ - **Automatic transformation**: Zod schemas are now automatically transformed for OpenAI strict mode compatibility when using OpenAI models (including reasoning models like o1, o3, o4)
11
+ - **Optional field handling**: `.optional()` fields are converted to `.nullable()` with a transform that converts `null` → `undefined`, preserving optional semantics while satisfying OpenAI's strict mode requirements
12
+ - **Preserves nullable fields**: Intentionally `.nullable()` fields remain unchanged
13
+ - **Deep transformation**: Handles `.optional()` fields at any nesting level (objects, arrays, unions, etc.)
14
+ - **JSON Schema objects**: Not transformed, only Zod schemas
15
+
16
+ ## Example
17
+
18
+ ```typescript
19
+ const agent = new Agent({
20
+ name: 'data-extractor',
21
+ model: { provider: 'openai', modelId: 'gpt-4o' },
22
+ instructions: 'Extract user information',
23
+ });
24
+
25
+ const schema = z.object({
26
+ name: z.string(),
27
+ age: z.number().optional(),
28
+ deletedAt: z.date().nullable(),
29
+ });
30
+
31
+ // Schema is automatically transformed for OpenAI compatibility
32
+ const result = await agent.generate('Extract: John, deleted yesterday', {
33
+ structuredOutput: { schema },
34
+ });
35
+
36
+ // Result: { name: 'John', age: undefined, deletedAt: null }
37
+ ```
38
+
39
+ ## 0.11.7
40
+
41
+ ### Patch Changes
42
+
43
+ - update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
44
+
3
45
  ## 0.11.7-alpha.0
4
46
 
5
47
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -86,7 +86,8 @@ var SUPPORTED_ZOD_TYPES = [
86
86
  "ZodNumber",
87
87
  "ZodDate",
88
88
  "ZodAny",
89
- "ZodDefault"
89
+ "ZodDefault",
90
+ "ZodNullable"
90
91
  ];
91
92
  var ALL_ZOD_TYPES = [...SUPPORTED_ZOD_TYPES, ...UNSUPPORTED_ZOD_TYPES];
92
93
  var SchemaCompatLayer = class {
@@ -503,7 +504,8 @@ var SUPPORTED_ZOD_TYPES2 = [
503
504
  "ZodNumber",
504
505
  "ZodDate",
505
506
  "ZodAny",
506
- "ZodDefault"
507
+ "ZodDefault",
508
+ "ZodNullable"
507
509
  ];
508
510
  var SchemaCompatLayer2 = class {
509
511
  model;
@@ -546,6 +548,12 @@ var SchemaCompatLayer2 = class {
546
548
  isNull(v) {
547
549
  return v instanceof v4.ZodNull;
548
550
  }
551
+ /**
552
+ * Type guard for nullable Zod types
553
+ */
554
+ isNullable(v) {
555
+ return v instanceof v4.ZodNullable;
556
+ }
549
557
  /**
550
558
  * Type guard for array Zod types
551
559
  */
@@ -1139,6 +1147,9 @@ function isDate(z10) {
1139
1147
  function isDefault(z10) {
1140
1148
  return (v) => v instanceof z10["ZodDefault"];
1141
1149
  }
1150
+ function isNullable(z10) {
1151
+ return (v) => v instanceof z10["ZodNullable"];
1152
+ }
1142
1153
 
1143
1154
  // src/provider-compats/anthropic.ts
1144
1155
  var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer3 {
@@ -1279,15 +1290,30 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
1279
1290
  }
1280
1291
  processZodType(value) {
1281
1292
  if (isOptional2(zod.z)(value)) {
1282
- return this.defaultZodOptionalHandler(value, [
1283
- "ZodObject",
1284
- "ZodArray",
1285
- "ZodUnion",
1286
- "ZodString",
1287
- "ZodNever",
1288
- "ZodUndefined",
1289
- "ZodTuple"
1290
- ]);
1293
+ const innerType = "_def" in value ? value._def.innerType : value._zod?.def?.innerType;
1294
+ if (innerType) {
1295
+ if (isNullable(zod.z)(innerType)) {
1296
+ const processed = this.processZodType(innerType);
1297
+ return processed.transform((val) => val === null ? void 0 : val);
1298
+ }
1299
+ const processedInner = this.processZodType(innerType);
1300
+ return processedInner.nullable().transform((val) => val === null ? void 0 : val);
1301
+ }
1302
+ return value;
1303
+ } else if (isNullable(zod.z)(value)) {
1304
+ const innerType = "_def" in value ? value._def.innerType : value._zod?.def?.innerType;
1305
+ if (innerType) {
1306
+ if (isOptional2(zod.z)(innerType)) {
1307
+ const innerInnerType = "_def" in innerType ? innerType._def.innerType : innerType._zod?.def?.innerType;
1308
+ if (innerInnerType) {
1309
+ const processedInnerInner = this.processZodType(innerInnerType);
1310
+ return processedInnerInner.nullable().transform((val) => val === null ? void 0 : val);
1311
+ }
1312
+ }
1313
+ const processedInner = this.processZodType(innerType);
1314
+ return processedInner.nullable();
1315
+ }
1316
+ return value;
1291
1317
  } else if (isObj2(zod.z)(value)) {
1292
1318
  return this.defaultZodObjectHandler(value);
1293
1319
  } else if (isUnion2(zod.z)(value)) {
@@ -1327,8 +1353,30 @@ var OpenAIReasoningSchemaCompatLayer = class extends SchemaCompatLayer3 {
1327
1353
  }
1328
1354
  processZodType(value) {
1329
1355
  if (isOptional2(zod.z)(value)) {
1330
- const innerZodType = this.processZodType(value._def.innerType);
1331
- return innerZodType.nullable();
1356
+ const innerType = "_def" in value ? value._def.innerType : value._zod?.def?.innerType;
1357
+ if (innerType) {
1358
+ if (isNullable(zod.z)(innerType)) {
1359
+ const processed = this.processZodType(innerType);
1360
+ return processed.transform((val) => val === null ? void 0 : val);
1361
+ }
1362
+ const processedInner = this.processZodType(innerType);
1363
+ return processedInner.nullable().transform((val) => val === null ? void 0 : val);
1364
+ }
1365
+ return value;
1366
+ } else if (isNullable(zod.z)(value)) {
1367
+ const innerType = "_def" in value ? value._def.innerType : value._zod?.def?.innerType;
1368
+ if (innerType && isOptional2(zod.z)(innerType)) {
1369
+ const innerInnerType = "_def" in innerType ? innerType._def.innerType : innerType._zod?.def?.innerType;
1370
+ if (innerInnerType) {
1371
+ const processedInnerInner = this.processZodType(innerInnerType);
1372
+ return processedInnerInner.nullable().transform((val) => val === null ? void 0 : val);
1373
+ }
1374
+ }
1375
+ if (innerType) {
1376
+ const processedInner = this.processZodType(innerType);
1377
+ return processedInner.nullable();
1378
+ }
1379
+ return value;
1332
1380
  } else if (isObj2(zod.z)(value)) {
1333
1381
  return this.defaultZodObjectHandler(value, { passthrough: false });
1334
1382
  } else if (isArr2(zod.z)(value)) {