@mastra/schema-compat 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-safe-stringify-telemetry-20251205024938
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 +111 -3
- 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/json-to-zod.cjs +268 -0
- package/dist/json-to-zod.cjs.map +1 -0
- package/dist/json-to-zod.d.ts +4 -0
- package/dist/json-to-zod.d.ts.map +1 -0
- package/dist/json-to-zod.js +257 -0
- package/dist/json-to-zod.js.map +1 -0
- 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 +21 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,16 +1,124 @@
|
|
|
1
1
|
# @mastra/schema-compat
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-safe-stringify-telemetry-20251205024938
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- Fix
|
|
7
|
+
- Fix discriminatedUnion schema information lost when json schema is converted to zod ([#10764](https://github.com/mastra-ai/mastra/pull/10764))
|
|
8
|
+
|
|
9
|
+
## 0.11.8
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Fixed OpenAI schema compatibility when using `agent.generate()` or `agent.stream()` with `structuredOutput`. ([#10454](https://github.com/mastra-ai/mastra/pull/10454))
|
|
14
|
+
|
|
15
|
+
## Changes
|
|
16
|
+
- **Automatic transformation**: Zod schemas are now automatically transformed for OpenAI strict mode compatibility when using OpenAI models (including reasoning models like o1, o3, o4)
|
|
17
|
+
- **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
|
|
18
|
+
- **Preserves nullable fields**: Intentionally `.nullable()` fields remain unchanged
|
|
19
|
+
- **Deep transformation**: Handles `.optional()` fields at any nesting level (objects, arrays, unions, etc.)
|
|
20
|
+
- **JSON Schema objects**: Not transformed, only Zod schemas
|
|
21
|
+
|
|
22
|
+
## Example
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
const agent = new Agent({
|
|
26
|
+
name: 'data-extractor',
|
|
27
|
+
model: { provider: 'openai', modelId: 'gpt-4o' },
|
|
28
|
+
instructions: 'Extract user information',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const schema = z.object({
|
|
32
|
+
name: z.string(),
|
|
33
|
+
age: z.number().optional(),
|
|
34
|
+
deletedAt: z.date().nullable(),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Schema is automatically transformed for OpenAI compatibility
|
|
38
|
+
const result = await agent.generate('Extract: John, deleted yesterday', {
|
|
39
|
+
structuredOutput: { schema },
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Result: { name: 'John', age: undefined, deletedAt: null }
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 0.11.8-alpha.0
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- Fixed OpenAI schema compatibility when using `agent.generate()` or `agent.stream()` with `structuredOutput`. ([#10454](https://github.com/mastra-ai/mastra/pull/10454))
|
|
50
|
+
|
|
51
|
+
## Changes
|
|
52
|
+
- **Automatic transformation**: Zod schemas are now automatically transformed for OpenAI strict mode compatibility when using OpenAI models (including reasoning models like o1, o3, o4)
|
|
53
|
+
- **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
|
|
54
|
+
- **Preserves nullable fields**: Intentionally `.nullable()` fields remain unchanged
|
|
55
|
+
- **Deep transformation**: Handles `.optional()` fields at any nesting level (objects, arrays, unions, etc.)
|
|
56
|
+
- **JSON Schema objects**: Not transformed, only Zod schemas
|
|
57
|
+
|
|
58
|
+
## Example
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const agent = new Agent({
|
|
62
|
+
name: 'data-extractor',
|
|
63
|
+
model: { provider: 'openai', modelId: 'gpt-4o' },
|
|
64
|
+
instructions: 'Extract user information',
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const schema = z.object({
|
|
68
|
+
name: z.string(),
|
|
69
|
+
age: z.number().optional(),
|
|
70
|
+
deletedAt: z.date().nullable(),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Schema is automatically transformed for OpenAI compatibility
|
|
74
|
+
const result = await agent.generate('Extract: John, deleted yesterday', {
|
|
75
|
+
structuredOutput: { schema },
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Result: { name: 'John', age: undefined, deletedAt: null }
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 0.11.7
|
|
82
|
+
|
|
83
|
+
### Patch Changes
|
|
84
|
+
|
|
85
|
+
- update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
|
|
86
|
+
|
|
87
|
+
## 0.11.7-alpha.0
|
|
88
|
+
|
|
89
|
+
### Patch Changes
|
|
90
|
+
|
|
91
|
+
- update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
|
|
92
|
+
|
|
93
|
+
## 0.11.6
|
|
94
|
+
|
|
95
|
+
### Patch Changes
|
|
96
|
+
|
|
97
|
+
- Fix Zod v4 toJSONSchema bug with z.record() single-argument form ([#9355](https://github.com/mastra-ai/mastra/pull/9355))
|
|
98
|
+
|
|
99
|
+
Zod v4 has a bug in the single-argument form of `z.record(valueSchema)` where it incorrectly assigns the value schema to `keyType` instead of `valueType`, leaving `valueType` undefined. This causes `toJSONSchema()` to throw "Cannot read properties of undefined (reading '\_zod')" when processing schemas containing `z.record()` fields.
|
|
100
|
+
|
|
101
|
+
This fix patches affected schemas before conversion by detecting records with missing `valueType` and correctly assigning the schema to `valueType` while setting `keyType` to `z.string()` (the default). The patch recursively handles nested schemas including those wrapped in `.optional()`, `.nullable()`, arrays, unions, and objects.
|
|
102
|
+
|
|
103
|
+
- Improved reliability of string field types in tool schema compatibility ([#9362](https://github.com/mastra-ai/mastra/pull/9362))
|
|
104
|
+
|
|
105
|
+
## 0.11.6-alpha.0
|
|
106
|
+
|
|
107
|
+
### Patch Changes
|
|
108
|
+
|
|
109
|
+
- Fix Zod v4 toJSONSchema bug with z.record() single-argument form ([#9355](https://github.com/mastra-ai/mastra/pull/9355))
|
|
8
110
|
|
|
9
111
|
Zod v4 has a bug in the single-argument form of `z.record(valueSchema)` where it incorrectly assigns the value schema to `keyType` instead of `valueType`, leaving `valueType` undefined. This causes `toJSONSchema()` to throw "Cannot read properties of undefined (reading '\_zod')" when processing schemas containing `z.record()` fields.
|
|
10
112
|
|
|
11
113
|
This fix patches affected schemas before conversion by detecting records with missing `valueType` and correctly assigning the schema to `valueType` while setting `keyType` to `z.string()` (the default). The patch recursively handles nested schemas including those wrapped in `.optional()`, `.nullable()`, arrays, unions, and objects.
|
|
12
114
|
|
|
13
|
-
- Improved reliability of string field types in tool schema compatibility ([#
|
|
115
|
+
- Improved reliability of string field types in tool schema compatibility ([#9362](https://github.com/mastra-ai/mastra/pull/9362))
|
|
116
|
+
|
|
117
|
+
## 0.11.5
|
|
118
|
+
|
|
119
|
+
### Patch Changes
|
|
120
|
+
|
|
121
|
+
- Fix peerdependencies ([`eb7c1c8`](https://github.com/mastra-ai/mastra/commit/eb7c1c8c592d8fb16dfd250e337d9cdc73c8d5de))
|
|
14
122
|
|
|
15
123
|
## 0.11.4
|
|
16
124
|
|
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)) {
|