@mastra/schema-compat 1.0.0-beta.8 → 1.1.0-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 +122 -0
- package/dist/_types/@internal_ai-v6/dist/index.d.ts +11769 -0
- package/dist/chunk-5WRI5ZAA.js +29 -0
- package/dist/{chunk-EGRHWZRV.js.map → chunk-5WRI5ZAA.js.map} +1 -1
- package/dist/{chunk-7UW726BK.js → chunk-BXZCXMXO.js} +2 -2
- package/dist/chunk-BXZCXMXO.js.map +1 -0
- package/dist/chunk-DAVEUCUM.cjs +6784 -0
- package/dist/chunk-DAVEUCUM.cjs.map +1 -0
- package/dist/chunk-DGOXVQNP.js +6774 -0
- package/dist/chunk-DGOXVQNP.js.map +1 -0
- package/dist/chunk-DZUJEN5N.cjs +32 -0
- package/dist/{chunk-NKIQRCOM.cjs.map → chunk-DZUJEN5N.cjs.map} +1 -1
- package/dist/{chunk-ZXNJQOPF.cjs → chunk-VC7YNDY2.cjs} +2 -2
- package/dist/chunk-VC7YNDY2.cjs.map +1 -0
- package/dist/index.cjs +553 -95
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +554 -96
- package/dist/index.js.map +1 -1
- package/dist/json-schema/utils.d.ts +53 -0
- package/dist/json-schema/utils.d.ts.map +1 -0
- package/dist/provider-compats/anthropic.d.ts +5 -4
- package/dist/provider-compats/anthropic.d.ts.map +1 -1
- package/dist/provider-compats/deepseek.d.ts +3 -0
- package/dist/provider-compats/deepseek.d.ts.map +1 -1
- package/dist/provider-compats/google.d.ts +3 -0
- package/dist/provider-compats/google.d.ts.map +1 -1
- package/dist/provider-compats/meta.d.ts +3 -0
- package/dist/provider-compats/meta.d.ts.map +1 -1
- package/dist/provider-compats/openai-reasoning.d.ts +5 -4
- package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
- package/dist/provider-compats/openai.d.ts +4 -2
- package/dist/provider-compats/openai.d.ts.map +1 -1
- package/dist/schema-compatibility-v3.d.ts +13 -1
- package/dist/schema-compatibility-v3.d.ts.map +1 -1
- package/dist/schema-compatibility-v4.d.ts +9 -1
- package/dist/schema-compatibility-v4.d.ts.map +1 -1
- package/dist/schema-compatibility.d.ts +78 -149
- package/dist/schema-compatibility.d.ts.map +1 -1
- package/dist/schema.cjs +20 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.ts +4 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +3 -0
- package/dist/schema.js.map +1 -0
- package/dist/schema.types.d.ts +17 -0
- package/dist/schema.types.d.ts.map +1 -0
- package/dist/standard-schema/adapters/ai-sdk.d.ts +85 -0
- package/dist/standard-schema/adapters/ai-sdk.d.ts.map +1 -0
- package/dist/standard-schema/adapters/json-schema.d.ts +100 -0
- package/dist/standard-schema/adapters/json-schema.d.ts.map +1 -0
- package/dist/standard-schema/adapters/zod-v3.d.ts +32 -0
- package/dist/standard-schema/adapters/zod-v3.d.ts.map +1 -0
- package/dist/standard-schema/standard-schema.d.ts +107 -0
- package/dist/standard-schema/standard-schema.d.ts.map +1 -0
- package/dist/standard-schema/standard-schema.types.d.ts +24 -0
- package/dist/standard-schema/standard-schema.types.d.ts.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/dist/zod-to-json.cjs +2 -2
- package/dist/zod-to-json.d.ts +1 -3
- package/dist/zod-to-json.d.ts.map +1 -1
- package/dist/zod-to-json.js +1 -1
- package/dist/zodTypes.d.ts +66 -0
- package/dist/zodTypes.d.ts.map +1 -1
- package/package.json +22 -6
- package/dist/chunk-7UW726BK.js.map +0 -1
- package/dist/chunk-EGRHWZRV.js +0 -3
- package/dist/chunk-NKIQRCOM.cjs +0 -4
- package/dist/chunk-ZXNJQOPF.cjs.map +0 -1
- package/dist/utils-test-suite.d.ts +0 -2
- package/dist/utils-test-suite.d.ts.map +0 -1
- package/dist/zod-to-json-test-suite.d.ts +0 -6
- package/dist/zod-to-json-test-suite.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,127 @@
|
|
|
1
1
|
# @mastra/schema-compat
|
|
2
2
|
|
|
3
|
+
## 1.1.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added [Standard Schema](https://github.com/standard-schema/standard-schema) support to `@mastra/schema-compat`. This enables interoperability with any schema library that implements the Standard Schema specification. ([#12527](https://github.com/mastra-ai/mastra/pull/12527))
|
|
8
|
+
|
|
9
|
+
**New exports:**
|
|
10
|
+
- `toStandardSchema()` - Convert Zod, JSON Schema, or AI SDK schemas to Standard Schema format
|
|
11
|
+
- `StandardSchemaWithJSON` - Type for schemas implementing both validation and JSON Schema conversion
|
|
12
|
+
- `InferInput`, `InferOutput` - Utility types for type inference
|
|
13
|
+
|
|
14
|
+
**Example usage:**
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { toStandardSchema } from '@mastra/schema-compat/schema';
|
|
18
|
+
import { z } from 'zod';
|
|
19
|
+
|
|
20
|
+
// Convert a Zod schema to Standard Schema
|
|
21
|
+
const zodSchema = z.object({ name: z.string(), age: z.number() });
|
|
22
|
+
const standardSchema = toStandardSchema(zodSchema);
|
|
23
|
+
|
|
24
|
+
// Use validation
|
|
25
|
+
const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });
|
|
26
|
+
|
|
27
|
+
// Get JSON Schema
|
|
28
|
+
const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 1.0.0
|
|
32
|
+
|
|
33
|
+
### Major Changes
|
|
34
|
+
|
|
35
|
+
- Bump minimum required Node.js version to 22.13.0 ([#9706](https://github.com/mastra-ai/mastra/pull/9706))
|
|
36
|
+
|
|
37
|
+
- Mark as stable ([`83d5942`](https://github.com/mastra-ai/mastra/commit/83d5942669ce7bba4a6ca4fd4da697a10eb5ebdc))
|
|
38
|
+
|
|
39
|
+
### Patch Changes
|
|
40
|
+
|
|
41
|
+
- Fix Zod v4 toJSONSchema bug with z.record() single-argument form ([#9265](https://github.com/mastra-ai/mastra/pull/9265))
|
|
42
|
+
|
|
43
|
+
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.
|
|
44
|
+
|
|
45
|
+
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.
|
|
46
|
+
|
|
47
|
+
- Embed AI types to fix peerdeps mismatches ([`9650cce`](https://github.com/mastra-ai/mastra/commit/9650cce52a1d917ff9114653398e2a0f5c3ba808))
|
|
48
|
+
|
|
49
|
+
- Fixed agent network mode failing with "Cannot read properties of undefined" error when tools or workflows don't have an `inputSchema` defined. ([#12063](https://github.com/mastra-ai/mastra/pull/12063))
|
|
50
|
+
- **@mastra/core:** Fixed `getRoutingAgent()` to handle tools and workflows without `inputSchema` by providing a default empty schema fallback.
|
|
51
|
+
- **@mastra/schema-compat:** Fixed Zod v4 optional/nullable fields producing invalid JSON schema for OpenAI structured outputs. OpenAI now correctly receives `type: ["string", "null"]` instead of `anyOf` patterns that were rejected with "must have a 'type' key" error.
|
|
52
|
+
|
|
53
|
+
- Fixed OpenAI schema validation error when using passthrough schemas with tools like `vectorQueryTool`. ([#11846](https://github.com/mastra-ai/mastra/pull/11846))
|
|
54
|
+
|
|
55
|
+
**What was happening:** Tools using `.passthrough()` or `z.looseObject()` schemas (like the RAG `vectorQueryTool`) would fail with OpenAI models, returning the error: "Invalid schema for function: In context=('additionalProperties',), schema must have a 'type' key."
|
|
56
|
+
|
|
57
|
+
**What changed:** The OpenAI schema compatibility layer now converts passthrough schemas to strict object schemas, producing valid `additionalProperties: false` instead of the invalid empty object `{}` that Zod v4 generates.
|
|
58
|
+
|
|
59
|
+
Fixes #11823
|
|
60
|
+
|
|
61
|
+
- Fixed "Transforms cannot be represented in JSON Schema" error when using Zod v4 with structuredOutput ([#11466](https://github.com/mastra-ai/mastra/pull/11466))
|
|
62
|
+
|
|
63
|
+
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.
|
|
64
|
+
|
|
65
|
+
The fix uses Mastra's transform-safe `zodToJsonSchema` function which gracefully handles transforms by using the `unrepresentable: 'any'` option.
|
|
66
|
+
|
|
67
|
+
Also exported `isZodType` utility from `@mastra/schema-compat` and updated it to detect both Zod v3 (`_def`) and Zod v4 (`_zod`) schemas.
|
|
68
|
+
|
|
69
|
+
- Improved reliability of string field types in tool schema compatibility ([#9266](https://github.com/mastra-ai/mastra/pull/9266))
|
|
70
|
+
|
|
71
|
+
- Fix OpenAI structured output compatibility for fields with `.default()` values ([#11434](https://github.com/mastra-ai/mastra/pull/11434))
|
|
72
|
+
|
|
73
|
+
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.
|
|
74
|
+
|
|
75
|
+
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.
|
|
76
|
+
|
|
77
|
+
- fix(schema-compat): handle undefined values in optional fields for OpenAI compat layers ([#11469](https://github.com/mastra-ai/mastra/pull/11469))
|
|
78
|
+
|
|
79
|
+
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`.
|
|
80
|
+
|
|
81
|
+
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).
|
|
82
|
+
|
|
83
|
+
Fixes #11457
|
|
84
|
+
|
|
85
|
+
- Fix discriminatedUnion schema information lost when json schema is converted to zod ([#10500](https://github.com/mastra-ai/mastra/pull/10500))
|
|
86
|
+
|
|
87
|
+
- Fix oneOf schema conversion generating invalid JavaScript ([#11626](https://github.com/mastra-ai/mastra/pull/11626))
|
|
88
|
+
|
|
89
|
+
The upstream json-schema-to-zod library generates TypeScript syntax (`reduce<z.ZodError[]>`) when converting oneOf schemas. This TypeScript generic annotation fails when evaluated at runtime with Function(), causing schema resolution to fail.
|
|
90
|
+
|
|
91
|
+
The fix removes TypeScript generic syntax from the generated output, producing valid JavaScript that can be evaluated at runtime. This resolves issues where MCP tools with oneOf in their output schemas would fail validation.
|
|
92
|
+
|
|
93
|
+
- Fixed OpenAI schema compatibility when using `agent.generate()` or `agent.stream()` with `structuredOutput`. ([#10366](https://github.com/mastra-ai/mastra/pull/10366))
|
|
94
|
+
|
|
95
|
+
**Changes**
|
|
96
|
+
- **Automatic transformation**: Zod schemas are now automatically transformed for OpenAI strict mode compatibility when using OpenAI models (including reasoning models like o1, o3, o4)
|
|
97
|
+
- **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
|
|
98
|
+
- **Preserves nullable fields**: Intentionally `.nullable()` fields remain unchanged
|
|
99
|
+
- **Deep transformation**: Handles `.optional()` fields at any nesting level (objects, arrays, unions, etc.)
|
|
100
|
+
- **JSON Schema objects**: Not transformed, only Zod schemas
|
|
101
|
+
|
|
102
|
+
**Example**
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const agent = new Agent({
|
|
106
|
+
name: 'data-extractor',
|
|
107
|
+
model: { provider: 'openai', modelId: 'gpt-4o' },
|
|
108
|
+
instructions: 'Extract user information',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const schema = z.object({
|
|
112
|
+
name: z.string(),
|
|
113
|
+
age: z.number().optional(),
|
|
114
|
+
deletedAt: z.date().nullable(),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Schema is automatically transformed for OpenAI compatibility
|
|
118
|
+
const result = await agent.generate('Extract: John, deleted yesterday', {
|
|
119
|
+
structuredOutput: { schema },
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Result: { name: 'John', age: undefined, deletedAt: null }
|
|
123
|
+
```
|
|
124
|
+
|
|
3
125
|
## 1.0.0-beta.8
|
|
4
126
|
|
|
5
127
|
### Patch Changes
|