@mastra/schema-compat 0.0.0-1.x-tester-20251106055847
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 +213 -0
- package/LICENSE.md +15 -0
- package/README.md +144 -0
- package/dist/chunk-5WM4A32G.cjs +83 -0
- package/dist/chunk-5WM4A32G.cjs.map +1 -0
- package/dist/chunk-U2HXWNAF.js +77 -0
- package/dist/chunk-U2HXWNAF.js.map +1 -0
- package/dist/index.cjs +4954 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4928 -0
- package/dist/index.js.map +1 -0
- package/dist/json-schema.d.ts +4 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/provider-compats/anthropic.d.ts +13 -0
- package/dist/provider-compats/anthropic.d.ts.map +1 -0
- package/dist/provider-compats/deepseek.d.ts +13 -0
- package/dist/provider-compats/deepseek.d.ts.map +1 -0
- package/dist/provider-compats/google.d.ts +13 -0
- package/dist/provider-compats/google.d.ts.map +1 -0
- package/dist/provider-compats/meta.d.ts +13 -0
- package/dist/provider-compats/meta.d.ts.map +1 -0
- package/dist/provider-compats/openai-reasoning.d.ts +14 -0
- package/dist/provider-compats/openai-reasoning.d.ts.map +1 -0
- package/dist/provider-compats/openai.d.ts +13 -0
- package/dist/provider-compats/openai.d.ts.map +1 -0
- package/dist/schema-compatibility-v3.d.ts +287 -0
- package/dist/schema-compatibility-v3.d.ts.map +1 -0
- package/dist/schema-compatibility-v4.d.ts +278 -0
- package/dist/schema-compatibility-v4.d.ts.map +1 -0
- package/dist/schema-compatibility.d.ts +196 -0
- package/dist/schema-compatibility.d.ts.map +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils-test-suite.d.ts +2 -0
- package/dist/utils-test-suite.d.ts.map +1 -0
- package/dist/utils.d.ts +96 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/zod-to-json-test-suite.d.ts +6 -0
- package/dist/zod-to-json-test-suite.d.ts.map +1 -0
- package/dist/zod-to-json.cjs +12 -0
- package/dist/zod-to-json.cjs.map +1 -0
- package/dist/zod-to-json.d.ts +6 -0
- package/dist/zod-to-json.d.ts.map +1 -0
- package/dist/zod-to-json.js +3 -0
- package/dist/zod-to-json.js.map +1 -0
- package/dist/zodTypes.d.ts +21 -0
- package/dist/zodTypes.d.ts.map +1 -0
- package/package.json +83 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# @mastra/schema-compat
|
|
2
|
+
|
|
3
|
+
## 0.0.0-1.x-tester-20251106055847
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- Bump minimum required Node.js version to 22.13.0 ([#9706](https://github.com/mastra-ai/mastra/pull/9706))
|
|
8
|
+
|
|
9
|
+
- Mark as stable ([`83d5942`](https://github.com/mastra-ai/mastra/commit/83d5942669ce7bba4a6ca4fd4da697a10eb5ebdc))
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Fix Zod v4 toJSONSchema bug with z.record() single-argument form ([#9265](https://github.com/mastra-ai/mastra/pull/9265))
|
|
14
|
+
|
|
15
|
+
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.
|
|
16
|
+
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
- Improved reliability of string field types in tool schema compatibility ([#9266](https://github.com/mastra-ai/mastra/pull/9266))
|
|
20
|
+
|
|
21
|
+
## 0.11.4
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Fixes an issue when the OpenAI reasoning schema compatibility layer was calling defaultValue() as a function, which works in Zod v3 but fails in Zod v4 where defaultValue is stored directly as a value. ([#8090](https://github.com/mastra-ai/mastra/pull/8090))
|
|
26
|
+
|
|
27
|
+
## 0.11.4-alpha.0
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- Fixes an issue when the OpenAI reasoning schema compatibility layer was calling defaultValue() as a function, which works in Zod v3 but fails in Zod v4 where defaultValue is stored directly as a value. ([#8090](https://github.com/mastra-ai/mastra/pull/8090))
|
|
32
|
+
|
|
33
|
+
## 0.11.3
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- Change SchemaCompat zodToJsonSchema ref strategy from none to relative, leading to less schema warnings and smaller converted schema sizes ([#7697](https://github.com/mastra-ai/mastra/pull/7697))
|
|
38
|
+
|
|
39
|
+
## 0.11.3-alpha.0
|
|
40
|
+
|
|
41
|
+
### Patch Changes
|
|
42
|
+
|
|
43
|
+
- Change SchemaCompat zodToJsonSchema ref strategy from none to relative, leading to less schema warnings and smaller converted schema sizes ([#7697](https://github.com/mastra-ai/mastra/pull/7697))
|
|
44
|
+
|
|
45
|
+
## 0.11.2
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- ab48c97: dependencies updates:
|
|
50
|
+
- Updated dependency [`zod-to-json-schema@^3.24.6` ↗︎](https://www.npmjs.com/package/zod-to-json-schema/v/3.24.6) (from `^3.24.5`, in `dependencies`)
|
|
51
|
+
- 637f323: Fix issue with some compilers and calling zod v4's toJSONSchema function
|
|
52
|
+
- de3cbc6: Update the `package.json` file to include additional fields like `repository`, `homepage` or `files`.
|
|
53
|
+
- 45e4d39: Try fixing the `Attempted import error: 'z'.'toJSONSchema' is not exported from 'zod'` error by tricking the compiler
|
|
54
|
+
|
|
55
|
+
## 0.11.2-alpha.3
|
|
56
|
+
|
|
57
|
+
### Patch Changes
|
|
58
|
+
|
|
59
|
+
- [#7350](https://github.com/mastra-ai/mastra/pull/7350) [`45e4d39`](https://github.com/mastra-ai/mastra/commit/45e4d391a2a09fc70c48e4d60f505586ada1ba0e) Thanks [@LekoArts](https://github.com/LekoArts)! - Try fixing the `Attempted import error: 'z'.'toJSONSchema' is not exported from 'zod'` error by tricking the compiler
|
|
60
|
+
|
|
61
|
+
## 0.11.2-alpha.2
|
|
62
|
+
|
|
63
|
+
### Patch Changes
|
|
64
|
+
|
|
65
|
+
- [#7343](https://github.com/mastra-ai/mastra/pull/7343) [`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e) Thanks [@LekoArts](https://github.com/LekoArts)! - Update the `package.json` file to include additional fields like `repository`, `homepage` or `files`.
|
|
66
|
+
|
|
67
|
+
## 0.11.2-alpha.1
|
|
68
|
+
|
|
69
|
+
### Patch Changes
|
|
70
|
+
|
|
71
|
+
- [#5816](https://github.com/mastra-ai/mastra/pull/5816) [`ab48c97`](https://github.com/mastra-ai/mastra/commit/ab48c979098ea571faf998a55d3a00e7acd7a715) Thanks [@dane-ai-mastra](https://github.com/apps/dane-ai-mastra)! - dependencies updates:
|
|
72
|
+
- Updated dependency [`zod-to-json-schema@^3.24.6` ↗︎](https://www.npmjs.com/package/zod-to-json-schema/v/3.24.6) (from `^3.24.5`, in `dependencies`)
|
|
73
|
+
|
|
74
|
+
## 0.11.2-alpha.0
|
|
75
|
+
|
|
76
|
+
### Patch Changes
|
|
77
|
+
|
|
78
|
+
- [#7121](https://github.com/mastra-ai/mastra/pull/7121) [`637f323`](https://github.com/mastra-ai/mastra/commit/637f32371d79a8f78c52c0d53411af0915fcec67) Thanks [@DanielSLew](https://github.com/DanielSLew)! - Fix issue with some compilers and calling zod v4's toJSONSchema function
|
|
79
|
+
|
|
80
|
+
## 0.11.1
|
|
81
|
+
|
|
82
|
+
### Patch Changes
|
|
83
|
+
|
|
84
|
+
- [`c6113ed`](https://github.com/mastra-ai/mastra/commit/c6113ed7f9df297e130d94436ceee310273d6430) Thanks [@wardpeet](https://github.com/wardpeet)! - Fix peerdpes for @mastra/core
|
|
85
|
+
|
|
86
|
+
## 0.11.0
|
|
87
|
+
|
|
88
|
+
### Minor Changes
|
|
89
|
+
|
|
90
|
+
- [#7032](https://github.com/mastra-ai/mastra/pull/7032) [`1191ce9`](https://github.com/mastra-ai/mastra/commit/1191ce946b40ed291e7877a349f8388e3cff7e5c) Thanks [@wardpeet](https://github.com/wardpeet)! - Bump zod peerdep to 3.25.0 to support both v3/v4
|
|
91
|
+
|
|
92
|
+
### Patch Changes
|
|
93
|
+
|
|
94
|
+
- [#7028](https://github.com/mastra-ai/mastra/pull/7028) [`da58ccc`](https://github.com/mastra-ai/mastra/commit/da58ccc1f2ac33da0cb97b00443fc6208b45bdec) Thanks [@wardpeet](https://github.com/wardpeet)! - Fix exportsmap
|
|
95
|
+
|
|
96
|
+
- [#6982](https://github.com/mastra-ai/mastra/pull/6982) [`94e9f54`](https://github.com/mastra-ai/mastra/commit/94e9f547d66ef7cd01d9075ab53b5ca9a1cae100) Thanks [@wardpeet](https://github.com/wardpeet)! - Fix AI peerdeps for NPM install
|
|
97
|
+
|
|
98
|
+
- [#6944](https://github.com/mastra-ai/mastra/pull/6944) [`a93f3ba`](https://github.com/mastra-ai/mastra/commit/a93f3ba05eef4cf17f876d61d29cf0841a9e70b7) Thanks [@wardpeet](https://github.com/wardpeet)! - Add support for zod v4
|
|
99
|
+
|
|
100
|
+
## 0.11.0-alpha.2
|
|
101
|
+
|
|
102
|
+
### Minor Changes
|
|
103
|
+
|
|
104
|
+
- [#7032](https://github.com/mastra-ai/mastra/pull/7032) [`1191ce9`](https://github.com/mastra-ai/mastra/commit/1191ce946b40ed291e7877a349f8388e3cff7e5c) Thanks [@wardpeet](https://github.com/wardpeet)! - Bump zod peerdep to 3.25.0 to support both v3/v4
|
|
105
|
+
|
|
106
|
+
## 0.10.6-alpha.1
|
|
107
|
+
|
|
108
|
+
### Patch Changes
|
|
109
|
+
|
|
110
|
+
- [#7028](https://github.com/mastra-ai/mastra/pull/7028) [`da58ccc`](https://github.com/mastra-ai/mastra/commit/da58ccc1f2ac33da0cb97b00443fc6208b45bdec) Thanks [@wardpeet](https://github.com/wardpeet)! - Fix exportsmap
|
|
111
|
+
|
|
112
|
+
## 0.10.6-alpha.0
|
|
113
|
+
|
|
114
|
+
### Patch Changes
|
|
115
|
+
|
|
116
|
+
- [#6982](https://github.com/mastra-ai/mastra/pull/6982) [`94e9f54`](https://github.com/mastra-ai/mastra/commit/94e9f547d66ef7cd01d9075ab53b5ca9a1cae100) Thanks [@wardpeet](https://github.com/wardpeet)! - Fix AI peerdeps for NPM install
|
|
117
|
+
|
|
118
|
+
- [#6944](https://github.com/mastra-ai/mastra/pull/6944) [`a93f3ba`](https://github.com/mastra-ai/mastra/commit/a93f3ba05eef4cf17f876d61d29cf0841a9e70b7) Thanks [@wardpeet](https://github.com/wardpeet)! - Add support for zod v4
|
|
119
|
+
|
|
120
|
+
## 0.10.7
|
|
121
|
+
|
|
122
|
+
### Patch Changes
|
|
123
|
+
|
|
124
|
+
- dd94a26: Dont rely on the full language model for schema compat
|
|
125
|
+
- 2fff911: Fix vnext working memory tool schema when model is incompatible with schema
|
|
126
|
+
- ae2eb63: Handle regex checks better, return description as a string rather than an object with pattern and flags.
|
|
127
|
+
|
|
128
|
+
## 0.10.7-alpha.1
|
|
129
|
+
|
|
130
|
+
### Patch Changes
|
|
131
|
+
|
|
132
|
+
- ae2eb63: Handle regex checks better, return description as a string rather than an object with pattern and flags.
|
|
133
|
+
|
|
134
|
+
## 0.10.7-alpha.0
|
|
135
|
+
|
|
136
|
+
### Patch Changes
|
|
137
|
+
|
|
138
|
+
- dd94a26: Dont rely on the full language model for schema compat
|
|
139
|
+
- 2fff911: Fix vnext working memory tool schema when model is incompatible with schema
|
|
140
|
+
|
|
141
|
+
## 0.10.6
|
|
142
|
+
|
|
143
|
+
### Patch Changes
|
|
144
|
+
|
|
145
|
+
- 4a406ec: fixes TypeScript declaration file imports to ensure proper ESM compatibility
|
|
146
|
+
|
|
147
|
+
## 0.10.6-alpha.0
|
|
148
|
+
|
|
149
|
+
### Patch Changes
|
|
150
|
+
|
|
151
|
+
- 4a406ec: fixes TypeScript declaration file imports to ensure proper ESM compatibility
|
|
152
|
+
|
|
153
|
+
## 0.10.5
|
|
154
|
+
|
|
155
|
+
### Patch Changes
|
|
156
|
+
|
|
157
|
+
- 4da943f: Fix Cannot read properties of undefined (reading 'typeName') in schema compat check
|
|
158
|
+
|
|
159
|
+
## 0.10.5-alpha.0
|
|
160
|
+
|
|
161
|
+
### Patch Changes
|
|
162
|
+
|
|
163
|
+
- 4da943f: Fix Cannot read properties of undefined (reading 'typeName') in schema compat check
|
|
164
|
+
|
|
165
|
+
## 0.10.4
|
|
166
|
+
|
|
167
|
+
### Patch Changes
|
|
168
|
+
|
|
169
|
+
- 0c85311: Fix Google models ZodNull tool schema handling
|
|
170
|
+
|
|
171
|
+
## 0.10.4-alpha.0
|
|
172
|
+
|
|
173
|
+
### Patch Changes
|
|
174
|
+
|
|
175
|
+
- 0c85311: Fix Google models ZodNull tool schema handling
|
|
176
|
+
|
|
177
|
+
## 0.10.3
|
|
178
|
+
|
|
179
|
+
### Patch Changes
|
|
180
|
+
|
|
181
|
+
- 98bbe5a: Claude cannot handle tuple schemas now.
|
|
182
|
+
- a853c43: Allow for object.passthrough in schema compat (aka MCP tool support).
|
|
183
|
+
|
|
184
|
+
## 0.10.3-alpha.1
|
|
185
|
+
|
|
186
|
+
### Patch Changes
|
|
187
|
+
|
|
188
|
+
- a853c43: Allow for object.passthrough in schema compat (aka MCP tool support).
|
|
189
|
+
|
|
190
|
+
## 0.10.3-alpha.0
|
|
191
|
+
|
|
192
|
+
### Patch Changes
|
|
193
|
+
|
|
194
|
+
- 98bbe5a: Claude cannot handle tuple schemas now.
|
|
195
|
+
|
|
196
|
+
## 0.10.2
|
|
197
|
+
|
|
198
|
+
### Patch Changes
|
|
199
|
+
|
|
200
|
+
- f6fd25f: Updates @mastra/schema-compat to allow all zod schemas. Uses @mastra/schema-compat to apply schema transformations to agent output schema.
|
|
201
|
+
- f9816ae: Create @mastra/schema-compat package to extract the schema compatibility layer to be used outside of mastra
|
|
202
|
+
|
|
203
|
+
## 0.10.2-alpha.3
|
|
204
|
+
|
|
205
|
+
### Patch Changes
|
|
206
|
+
|
|
207
|
+
- f6fd25f: Updates @mastra/schema-compat to allow all zod schemas. Uses @mastra/schema-compat to apply schema transformations to agent output schema.
|
|
208
|
+
|
|
209
|
+
## 0.10.2-alpha.2
|
|
210
|
+
|
|
211
|
+
### Patch Changes
|
|
212
|
+
|
|
213
|
+
- f9816ae: Create @mastra/schema-compat package to extract the schema compatibility layer to be used outside of mastra
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Apache License 2.0
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# @mastra/schema-compat
|
|
2
|
+
|
|
3
|
+
Schema compatibility layer for Mastra.ai that provides compatibility fixes for different AI model providers when using Zod schemas with tools.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @mastra/schema-compat
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
The package provides a base `SchemaCompatLayer` class that you can extend to create custom compatibility layers for different AI model providers:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { SchemaCompatLayer } from '@mastra/schema-compat';
|
|
19
|
+
import type { LanguageModelV1 } from 'ai';
|
|
20
|
+
|
|
21
|
+
class MyCustomCompat extends SchemaCompatLayer {
|
|
22
|
+
constructor(model: LanguageModelV1) {
|
|
23
|
+
super(model);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
shouldApply(): boolean {
|
|
27
|
+
return this.getModel().provider === 'my-provider';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getSchemaTarget() {
|
|
31
|
+
return 'jsonSchema7';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
|
|
35
|
+
// Your custom processing logic here
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Schema Processing
|
|
42
|
+
|
|
43
|
+
The package includes pre-built compatibility layers for popular AI providers:
|
|
44
|
+
|
|
45
|
+
Use the `applyCompatLayer` function to automatically apply the right compatibility layer:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { applyCompatLayer, OpenAISchemaCompatLayer, AnthropicSchemaCompatLayer } from '@mastra/schema-compat';
|
|
49
|
+
import { yourCustomCompatibilityLayer } from './customCompatibilityLayer';
|
|
50
|
+
import { z } from 'zod';
|
|
51
|
+
|
|
52
|
+
const schema = z.object({
|
|
53
|
+
name: z.string().email(),
|
|
54
|
+
preferences: z.array(z.string()).min(1),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const compatLayers = [
|
|
58
|
+
new OpenAISchemaCompatLayer(model),
|
|
59
|
+
new AnthropicSchemaCompatLayer(model),
|
|
60
|
+
new yourCustomCompatibilityLayer(model),
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
// Automatically applies the first matching compatibility
|
|
64
|
+
const result = applyCompatLayer({
|
|
65
|
+
schema,
|
|
66
|
+
compatLayers,
|
|
67
|
+
mode: 'aiSdkSchema', // or 'jsonSchema'
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Schema Building Utilities
|
|
72
|
+
|
|
73
|
+
The package also provides utility functions for schema conversion:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { convertZodSchemaToAISDKSchema, convertSchemaToZod } from '@mastra/schema-compat';
|
|
77
|
+
import { z } from 'zod';
|
|
78
|
+
import { jsonSchema } from 'ai';
|
|
79
|
+
|
|
80
|
+
const zodSchema = z.object({
|
|
81
|
+
name: z.string(),
|
|
82
|
+
age: z.number(),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Convert Zod to AI SDK Schema
|
|
86
|
+
const aiSchema = convertZodSchemaToAISDKSchema(zodSchema);
|
|
87
|
+
|
|
88
|
+
// Convert AI SDK Schema back to Zod
|
|
89
|
+
const aiSdkSchema = jsonSchema({
|
|
90
|
+
type: 'object',
|
|
91
|
+
properties: {
|
|
92
|
+
name: { type: 'string' },
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const backToZod = convertSchemaToZod(aiSdkSchema);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## API Reference
|
|
99
|
+
|
|
100
|
+
### Classes
|
|
101
|
+
|
|
102
|
+
- `SchemaCompatLayer` - Base abstract class for creating compatibility layers
|
|
103
|
+
- `AnthropicSchemaCompatLayer` - Compatibility for Anthropic Claude models
|
|
104
|
+
- `OpenAISchemaCompatLayer` - Compatibility for OpenAI models (without structured outputs)
|
|
105
|
+
- `OpenAIReasoningSchemaCompatLayer` - Compatibility for OpenAI reasoning models (o1 series)
|
|
106
|
+
- `GoogleSchemaCompatLayer` - Compatibility for Google Gemini models
|
|
107
|
+
- `DeepSeekSchemaCompatLayer` - Compatibility for DeepSeek models
|
|
108
|
+
- `MetaSchemaCompatLayer` - Compatibility for Meta Llama models
|
|
109
|
+
|
|
110
|
+
### Functions
|
|
111
|
+
|
|
112
|
+
- `applyCompatLayer(options)` - Process schema with automatic compatibility detection
|
|
113
|
+
- `convertZodSchemaToAISDKSchema(zodSchema, target?)` - Convert Zod schema to AI SDK Schema
|
|
114
|
+
- `convertSchemaToZod(schema)` - Convert AI SDK Schema to Zod schema
|
|
115
|
+
|
|
116
|
+
### Types and Constants
|
|
117
|
+
|
|
118
|
+
- `StringCheckType`, `NumberCheckType`, `ArrayCheckType` - Check types for validation
|
|
119
|
+
- `UnsupportedZodType`, `SupportedZodType`, `AllZodType` - Zod type classifications
|
|
120
|
+
- `ZodShape`, `ShapeKey`, `ShapeValue` - Utility types for Zod schemas
|
|
121
|
+
- `ALL_STRING_CHECKS`, `ALL_NUMBER_CHECKS`, `ALL_ARRAY_CHECKS` - Validation constraint arrays
|
|
122
|
+
- `SUPPORTED_ZOD_TYPES`, `UNSUPPORTED_ZOD_TYPES` - Type classification arrays
|
|
123
|
+
|
|
124
|
+
## Provider-Specific Behavior
|
|
125
|
+
|
|
126
|
+
Different AI providers have varying levels of support for JSON Schema features. This package handles these differences automatically:
|
|
127
|
+
|
|
128
|
+
- **OpenAI**: Removes certain string validations for models without structured outputs
|
|
129
|
+
- **Anthropic**: Handles complex nested schemas with proper constraint descriptions
|
|
130
|
+
- **Google**: Uses OpenAPI 3.0 schema format for better compatibility
|
|
131
|
+
- **DeepSeek**: Converts advanced string patterns to descriptions
|
|
132
|
+
- **Meta**: Optimizes array and union type handling
|
|
133
|
+
|
|
134
|
+
## Testing
|
|
135
|
+
|
|
136
|
+
The package includes comprehensive tests covering all functionality:
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
# Run tests
|
|
140
|
+
pnpm test
|
|
141
|
+
|
|
142
|
+
# Run tests in watch mode
|
|
143
|
+
pnpm test --watch
|
|
144
|
+
```
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
var zodToJsonSchemaOriginal = require('zod-to-json-schema');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var zodToJsonSchemaOriginal__default = /*#__PURE__*/_interopDefault(zodToJsonSchemaOriginal);
|
|
9
|
+
|
|
10
|
+
// src/zod-to-json.ts
|
|
11
|
+
var PATCHED = Symbol("__mastra_patched__");
|
|
12
|
+
function patchRecordSchemas(schema) {
|
|
13
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
14
|
+
if (schema[PATCHED]) return schema;
|
|
15
|
+
schema[PATCHED] = true;
|
|
16
|
+
const def = schema._zod?.def;
|
|
17
|
+
if (def?.type === "record" && def.keyType && !def.valueType) {
|
|
18
|
+
def.valueType = def.keyType;
|
|
19
|
+
def.keyType = zod.z.string();
|
|
20
|
+
}
|
|
21
|
+
if (!def) return schema;
|
|
22
|
+
if (def.type === "object" && def.shape) {
|
|
23
|
+
const shape = typeof def.shape === "function" ? def.shape() : def.shape;
|
|
24
|
+
for (const key of Object.keys(shape)) {
|
|
25
|
+
patchRecordSchemas(shape[key]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (def.type === "array" && def.element) {
|
|
29
|
+
patchRecordSchemas(def.element);
|
|
30
|
+
}
|
|
31
|
+
if (def.type === "union" && def.options) {
|
|
32
|
+
def.options.forEach(patchRecordSchemas);
|
|
33
|
+
}
|
|
34
|
+
if (def.type === "record") {
|
|
35
|
+
if (def.keyType) patchRecordSchemas(def.keyType);
|
|
36
|
+
if (def.valueType) patchRecordSchemas(def.valueType);
|
|
37
|
+
}
|
|
38
|
+
if (def.type === "intersection") {
|
|
39
|
+
if (def.left) patchRecordSchemas(def.left);
|
|
40
|
+
if (def.right) patchRecordSchemas(def.right);
|
|
41
|
+
}
|
|
42
|
+
if (def.type === "lazy") {
|
|
43
|
+
if (def.getter && typeof def.getter === "function") {
|
|
44
|
+
const originalGetter = def.getter;
|
|
45
|
+
def.getter = function() {
|
|
46
|
+
const innerSchema = originalGetter();
|
|
47
|
+
if (innerSchema) {
|
|
48
|
+
patchRecordSchemas(innerSchema);
|
|
49
|
+
}
|
|
50
|
+
return innerSchema;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (def.innerType) {
|
|
55
|
+
patchRecordSchemas(def.innerType);
|
|
56
|
+
}
|
|
57
|
+
return schema;
|
|
58
|
+
}
|
|
59
|
+
function zodToJsonSchema(zodSchema, target = "jsonSchema7", strategy = "relative") {
|
|
60
|
+
const fn = "toJSONSchema";
|
|
61
|
+
if (fn in zod.z) {
|
|
62
|
+
patchRecordSchemas(zodSchema);
|
|
63
|
+
return zod.z[fn](zodSchema, {
|
|
64
|
+
unrepresentable: "any",
|
|
65
|
+
override: (ctx) => {
|
|
66
|
+
const def = ctx.zodSchema?._def || ctx.zodSchema?._zod?.def;
|
|
67
|
+
if (def && (def.typeName === "ZodDate" || def.type === "date")) {
|
|
68
|
+
ctx.jsonSchema.type = "string";
|
|
69
|
+
ctx.jsonSchema.format = "date-time";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
return zodToJsonSchemaOriginal__default.default(zodSchema, {
|
|
75
|
+
$refStrategy: strategy,
|
|
76
|
+
target
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
exports.zodToJsonSchema = zodToJsonSchema;
|
|
82
|
+
//# sourceMappingURL=chunk-5WM4A32G.cjs.map
|
|
83
|
+
//# sourceMappingURL=chunk-5WM4A32G.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/zod-to-json.ts"],"names":["z","zodToJsonSch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AA;AAAA,MACjB,QAAA,EAAU,CAAC,GAAA,KAAa;AAEtB,QAAA,MAAM,MAAM,GAAA,CAAI,SAAA,EAAW,IAAA,IAAQ,GAAA,CAAI,WAAW,IAAA,EAAM,GAAA;AAExD,QAAA,IAAI,QAAQ,GAAA,CAAI,QAAA,KAAa,SAAA,IAAa,GAAA,CAAI,SAAS,MAAA,CAAA,EAAS;AAC9D,UAAA,GAAA,CAAI,WAAW,IAAA,GAAO,QAAA;AACtB,UAAA,GAAA,CAAI,WAAW,MAAA,GAAS,WAAA;AAAA,QAC1B;AAAA,MACF;AAAA,KACD,CAAA;AAAA,EACH,CAAA,MAAO;AAEL,IAAA,OAAOC,yCAAwB,SAAA,EAA0B;AAAA,MACvD,YAAA,EAAc,QAAA;AAAA,MACd;AAAA,KACD,CAAA;AAAA,EACH;AACF","file":"chunk-5WM4A32G.cjs","sourcesContent":["import type { JSONSchema7 } from 'json-schema';\nimport { z } from 'zod';\nimport type { ZodSchema as ZodSchemaV3 } from 'zod/v3';\nimport type { ZodType as ZodSchemaV4 } from 'zod/v4';\nimport type { Targets } from 'zod-to-json-schema';\nimport zodToJsonSchemaOriginal from 'zod-to-json-schema';\n\n// Symbol to mark schemas as already patched (for idempotency)\nconst PATCHED = Symbol('__mastra_patched__');\n\n/**\n * Recursively patch Zod v4 record schemas that are missing valueType.\n * This fixes a bug in Zod v4 where z.record(valueSchema) doesn't set def.valueType.\n * The single-arg form should set valueType but instead only sets keyType.\n */\nfunction patchRecordSchemas(schema: any): any {\n if (!schema || typeof schema !== 'object') return schema;\n\n // Skip if already patched (idempotency check)\n if ((schema as any)[PATCHED]) return schema;\n (schema as any)[PATCHED] = true;\n\n // Check the _zod.def location (v4 structure)\n const def = schema._zod?.def;\n\n // Fix record schemas with missing valueType\n if (def?.type === 'record' && def.keyType && !def.valueType) {\n // The bug: z.record(valueSchema) puts the value in keyType instead of valueType\n // Fix: move it to valueType and set keyType to string (the default)\n def.valueType = def.keyType;\n def.keyType = (z as any).string();\n }\n\n // Recursively patch nested schemas\n if (!def) return schema;\n\n if (def.type === 'object' && def.shape) {\n const shape = typeof def.shape === 'function' ? def.shape() : def.shape;\n for (const key of Object.keys(shape)) {\n patchRecordSchemas(shape[key]);\n }\n }\n\n if (def.type === 'array' && def.element) {\n patchRecordSchemas(def.element);\n }\n\n if (def.type === 'union' && def.options) {\n def.options.forEach(patchRecordSchemas);\n }\n\n if (def.type === 'record') {\n if (def.keyType) patchRecordSchemas(def.keyType);\n if (def.valueType) patchRecordSchemas(def.valueType);\n }\n\n // Handle intersection types\n if (def.type === 'intersection') {\n if (def.left) patchRecordSchemas(def.left);\n if (def.right) patchRecordSchemas(def.right);\n }\n\n // Handle lazy types - patch the schema returned by the getter\n if (def.type === 'lazy') {\n // For lazy schemas, we need to patch the schema when it's accessed\n // Store the original getter and wrap it\n if (def.getter && typeof def.getter === 'function') {\n const originalGetter = def.getter;\n def.getter = function () {\n const innerSchema = originalGetter();\n if (innerSchema) {\n patchRecordSchemas(innerSchema);\n }\n return innerSchema;\n };\n }\n }\n\n // Handle wrapper types that have innerType\n // This covers: optional, nullable, default, catch, nullish, and any other wrappers\n if (def.innerType) {\n patchRecordSchemas(def.innerType);\n }\n\n return schema;\n}\n\nexport function zodToJsonSchema(\n zodSchema: ZodSchemaV3 | ZodSchemaV4,\n target: Targets = 'jsonSchema7',\n strategy: 'none' | 'seen' | 'root' | 'relative' = 'relative',\n) {\n const fn = 'toJSONSchema';\n\n if (fn in z) {\n // Zod v4 path - patch record schemas before converting\n patchRecordSchemas(zodSchema);\n\n return (z as any)[fn](zodSchema, {\n unrepresentable: 'any',\n override: (ctx: any) => {\n // Handle both Zod v4 structures: _def directly or nested in _zod\n const def = ctx.zodSchema?._def || ctx.zodSchema?._zod?.def;\n // Check for date type using both possible property names\n if (def && (def.typeName === 'ZodDate' || def.type === 'date')) {\n ctx.jsonSchema.type = 'string';\n ctx.jsonSchema.format = 'date-time';\n }\n },\n }) as JSONSchema7;\n } else {\n // Zod v3 path - use the original converter\n return zodToJsonSchemaOriginal(zodSchema as ZodSchemaV3, {\n $refStrategy: strategy,\n target,\n }) as JSONSchema7;\n }\n}\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import zodToJsonSchemaOriginal from 'zod-to-json-schema';
|
|
3
|
+
|
|
4
|
+
// src/zod-to-json.ts
|
|
5
|
+
var PATCHED = Symbol("__mastra_patched__");
|
|
6
|
+
function patchRecordSchemas(schema) {
|
|
7
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
8
|
+
if (schema[PATCHED]) return schema;
|
|
9
|
+
schema[PATCHED] = true;
|
|
10
|
+
const def = schema._zod?.def;
|
|
11
|
+
if (def?.type === "record" && def.keyType && !def.valueType) {
|
|
12
|
+
def.valueType = def.keyType;
|
|
13
|
+
def.keyType = z.string();
|
|
14
|
+
}
|
|
15
|
+
if (!def) return schema;
|
|
16
|
+
if (def.type === "object" && def.shape) {
|
|
17
|
+
const shape = typeof def.shape === "function" ? def.shape() : def.shape;
|
|
18
|
+
for (const key of Object.keys(shape)) {
|
|
19
|
+
patchRecordSchemas(shape[key]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (def.type === "array" && def.element) {
|
|
23
|
+
patchRecordSchemas(def.element);
|
|
24
|
+
}
|
|
25
|
+
if (def.type === "union" && def.options) {
|
|
26
|
+
def.options.forEach(patchRecordSchemas);
|
|
27
|
+
}
|
|
28
|
+
if (def.type === "record") {
|
|
29
|
+
if (def.keyType) patchRecordSchemas(def.keyType);
|
|
30
|
+
if (def.valueType) patchRecordSchemas(def.valueType);
|
|
31
|
+
}
|
|
32
|
+
if (def.type === "intersection") {
|
|
33
|
+
if (def.left) patchRecordSchemas(def.left);
|
|
34
|
+
if (def.right) patchRecordSchemas(def.right);
|
|
35
|
+
}
|
|
36
|
+
if (def.type === "lazy") {
|
|
37
|
+
if (def.getter && typeof def.getter === "function") {
|
|
38
|
+
const originalGetter = def.getter;
|
|
39
|
+
def.getter = function() {
|
|
40
|
+
const innerSchema = originalGetter();
|
|
41
|
+
if (innerSchema) {
|
|
42
|
+
patchRecordSchemas(innerSchema);
|
|
43
|
+
}
|
|
44
|
+
return innerSchema;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (def.innerType) {
|
|
49
|
+
patchRecordSchemas(def.innerType);
|
|
50
|
+
}
|
|
51
|
+
return schema;
|
|
52
|
+
}
|
|
53
|
+
function zodToJsonSchema(zodSchema, target = "jsonSchema7", strategy = "relative") {
|
|
54
|
+
const fn = "toJSONSchema";
|
|
55
|
+
if (fn in z) {
|
|
56
|
+
patchRecordSchemas(zodSchema);
|
|
57
|
+
return z[fn](zodSchema, {
|
|
58
|
+
unrepresentable: "any",
|
|
59
|
+
override: (ctx) => {
|
|
60
|
+
const def = ctx.zodSchema?._def || ctx.zodSchema?._zod?.def;
|
|
61
|
+
if (def && (def.typeName === "ZodDate" || def.type === "date")) {
|
|
62
|
+
ctx.jsonSchema.type = "string";
|
|
63
|
+
ctx.jsonSchema.format = "date-time";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
} else {
|
|
68
|
+
return zodToJsonSchemaOriginal(zodSchema, {
|
|
69
|
+
$refStrategy: strategy,
|
|
70
|
+
target
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { zodToJsonSchema };
|
|
76
|
+
//# sourceMappingURL=chunk-U2HXWNAF.js.map
|
|
77
|
+
//# sourceMappingURL=chunk-U2HXWNAF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/zod-to-json.ts"],"names":[],"mappings":";;;;AAQA,IAAM,OAAA,GAAU,OAAO,oBAAoB,CAAA;AAO3C,SAAS,mBAAmB,MAAA,EAAkB;AAC5C,EAAA,IAAI,CAAC,MAAA,IAAU,OAAO,MAAA,KAAW,UAAU,OAAO,MAAA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QAAA,KAAa,SAAA,IAAa,GAAA,CAAI,SAAS,MAAA,CAAA,EAAS;AAC9D,UAAA,GAAA,CAAI,WAAW,IAAA,GAAO,QAAA;AACtB,UAAA,GAAA,CAAI,WAAW,MAAA,GAAS,WAAA;AAAA,QAC1B;AAAA,MACF;AAAA,KACD,CAAA;AAAA,EACH,CAAA,MAAO;AAEL,IAAA,OAAO,wBAAwB,SAAA,EAA0B;AAAA,MACvD,YAAA,EAAc,QAAA;AAAA,MACd;AAAA,KACD,CAAA;AAAA,EACH;AACF","file":"chunk-U2HXWNAF.js","sourcesContent":["import type { JSONSchema7 } from 'json-schema';\nimport { z } from 'zod';\nimport type { ZodSchema as ZodSchemaV3 } from 'zod/v3';\nimport type { ZodType as ZodSchemaV4 } from 'zod/v4';\nimport type { Targets } from 'zod-to-json-schema';\nimport zodToJsonSchemaOriginal from 'zod-to-json-schema';\n\n// Symbol to mark schemas as already patched (for idempotency)\nconst PATCHED = Symbol('__mastra_patched__');\n\n/**\n * Recursively patch Zod v4 record schemas that are missing valueType.\n * This fixes a bug in Zod v4 where z.record(valueSchema) doesn't set def.valueType.\n * The single-arg form should set valueType but instead only sets keyType.\n */\nfunction patchRecordSchemas(schema: any): any {\n if (!schema || typeof schema !== 'object') return schema;\n\n // Skip if already patched (idempotency check)\n if ((schema as any)[PATCHED]) return schema;\n (schema as any)[PATCHED] = true;\n\n // Check the _zod.def location (v4 structure)\n const def = schema._zod?.def;\n\n // Fix record schemas with missing valueType\n if (def?.type === 'record' && def.keyType && !def.valueType) {\n // The bug: z.record(valueSchema) puts the value in keyType instead of valueType\n // Fix: move it to valueType and set keyType to string (the default)\n def.valueType = def.keyType;\n def.keyType = (z as any).string();\n }\n\n // Recursively patch nested schemas\n if (!def) return schema;\n\n if (def.type === 'object' && def.shape) {\n const shape = typeof def.shape === 'function' ? def.shape() : def.shape;\n for (const key of Object.keys(shape)) {\n patchRecordSchemas(shape[key]);\n }\n }\n\n if (def.type === 'array' && def.element) {\n patchRecordSchemas(def.element);\n }\n\n if (def.type === 'union' && def.options) {\n def.options.forEach(patchRecordSchemas);\n }\n\n if (def.type === 'record') {\n if (def.keyType) patchRecordSchemas(def.keyType);\n if (def.valueType) patchRecordSchemas(def.valueType);\n }\n\n // Handle intersection types\n if (def.type === 'intersection') {\n if (def.left) patchRecordSchemas(def.left);\n if (def.right) patchRecordSchemas(def.right);\n }\n\n // Handle lazy types - patch the schema returned by the getter\n if (def.type === 'lazy') {\n // For lazy schemas, we need to patch the schema when it's accessed\n // Store the original getter and wrap it\n if (def.getter && typeof def.getter === 'function') {\n const originalGetter = def.getter;\n def.getter = function () {\n const innerSchema = originalGetter();\n if (innerSchema) {\n patchRecordSchemas(innerSchema);\n }\n return innerSchema;\n };\n }\n }\n\n // Handle wrapper types that have innerType\n // This covers: optional, nullable, default, catch, nullish, and any other wrappers\n if (def.innerType) {\n patchRecordSchemas(def.innerType);\n }\n\n return schema;\n}\n\nexport function zodToJsonSchema(\n zodSchema: ZodSchemaV3 | ZodSchemaV4,\n target: Targets = 'jsonSchema7',\n strategy: 'none' | 'seen' | 'root' | 'relative' = 'relative',\n) {\n const fn = 'toJSONSchema';\n\n if (fn in z) {\n // Zod v4 path - patch record schemas before converting\n patchRecordSchemas(zodSchema);\n\n return (z as any)[fn](zodSchema, {\n unrepresentable: 'any',\n override: (ctx: any) => {\n // Handle both Zod v4 structures: _def directly or nested in _zod\n const def = ctx.zodSchema?._def || ctx.zodSchema?._zod?.def;\n // Check for date type using both possible property names\n if (def && (def.typeName === 'ZodDate' || def.type === 'date')) {\n ctx.jsonSchema.type = 'string';\n ctx.jsonSchema.format = 'date-time';\n }\n },\n }) as JSONSchema7;\n } else {\n // Zod v3 path - use the original converter\n return zodToJsonSchemaOriginal(zodSchema as ZodSchemaV3, {\n $refStrategy: strategy,\n target,\n }) as JSONSchema7;\n }\n}\n"]}
|