@mastra/schema-compat 0.11.7 → 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 +36 -0
- package/dist/index.cjs +61 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -14
- package/dist/index.js.map +1 -1
- package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
- package/dist/provider-compats/openai.d.ts.map +1 -1
- package/dist/schema-compatibility-v3.d.ts +4 -3
- package/dist/schema-compatibility-v3.d.ts.map +1 -1
- package/dist/schema-compatibility-v4.d.ts +7 -3
- package/dist/schema-compatibility-v4.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/zodTypes.d.ts +2 -0
- package/dist/zodTypes.d.ts.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
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
|
+
|
|
3
39
|
## 0.11.7
|
|
4
40
|
|
|
5
41
|
### 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
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
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
|
|
1331
|
-
|
|
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)) {
|