@mastra/schema-compat 1.3.7 → 1.3.8-alpha.1
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/LICENSE.md +6 -4
- package/README.md +23 -114
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/provider-compats/anthropic.d.ts.map +1 -1
- package/dist/provider-compats/deepseek.d.ts.map +1 -1
- package/dist/provider-compats/google.d.ts.map +1 -1
- package/dist/provider-compats/meta.d.ts.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 +5 -5
- package/dist/schema-compatibility-v3.d.ts.map +1 -1
- package/dist/schema-compatibility-v4.d.ts +5 -5
- package/dist/schema-compatibility-v4.d.ts.map +1 -1
- package/dist/schema-compatibility.d.ts.map +1 -1
- package/dist/standard-schema/adapters/ai-sdk.d.ts.map +1 -1
- package/dist/standard-schema/adapters/json-schema.d.ts.map +1 -1
- package/dist/standard-schema/standard-schema.d.ts +1 -1
- package/dist/standard-schema/standard-schema.d.ts.map +1 -1
- package/dist/zodTypes.d.ts +5 -5
- package/dist/zodTypes.d.ts.map +1 -1
- package/package.json +10 -11
- package/CHANGELOG.md +0 -1013
package/LICENSE.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
Portions of this software are licensed as follows:
|
|
2
2
|
|
|
3
|
-
- All content that resides under any directory named
|
|
3
|
+
- All content that resides under any directory named `ee/` within this
|
|
4
4
|
repository, including but not limited to:
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
|
|
5
|
+
- `@mastra/core/auth/ee`
|
|
6
|
+
- `@mastra/core/agent-builder/ee`
|
|
7
|
+
- `@mastra/editor/ee`
|
|
8
|
+
|
|
9
|
+
is licensed under the license defined in [`ee/LICENSE`](https://github.com/mastra-ai/mastra/blob/main/ee/LICENSE).
|
|
8
10
|
|
|
9
11
|
- All third-party components incorporated into the Mastra Software are
|
|
10
12
|
licensed under the original license provided by the owner of the
|
package/README.md
CHANGED
|
@@ -1,144 +1,53 @@
|
|
|
1
1
|
# @mastra/schema-compat
|
|
2
2
|
|
|
3
|
-
Schema compatibility
|
|
3
|
+
Schema compatibility utilities that adapt Zod and JSON schemas to the different subsets and formats supported by AI model providers. Mastra uses these layers when tool inputs or structured outputs contain schema features a provider cannot accept directly.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
|
|
8
|
+
npm install @mastra/schema-compat
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
|
|
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:
|
|
13
|
+
Apply a provider layer to a Zod schema before passing it to an AI SDK API:
|
|
46
14
|
|
|
47
15
|
```typescript
|
|
48
|
-
import { applyCompatLayer, OpenAISchemaCompatLayer
|
|
49
|
-
import { yourCustomCompatibilityLayer } from './customCompatibilityLayer';
|
|
16
|
+
import { applyCompatLayer, OpenAISchemaCompatLayer } from '@mastra/schema-compat';
|
|
50
17
|
import { z } from 'zod';
|
|
51
18
|
|
|
19
|
+
const model = {
|
|
20
|
+
provider: 'openai.chat',
|
|
21
|
+
modelId: 'gpt-5.2',
|
|
22
|
+
supportsStructuredOutputs: true,
|
|
23
|
+
};
|
|
24
|
+
|
|
52
25
|
const schema = z.object({
|
|
53
26
|
name: z.string().email(),
|
|
54
|
-
|
|
27
|
+
tags: z.array(z.string()).min(1),
|
|
55
28
|
});
|
|
56
29
|
|
|
57
|
-
const
|
|
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({
|
|
30
|
+
const compatibleSchema = applyCompatLayer({
|
|
65
31
|
schema,
|
|
66
|
-
compatLayers,
|
|
67
|
-
mode: 'aiSdkSchema',
|
|
32
|
+
compatLayers: [new OpenAISchemaCompatLayer(model)],
|
|
33
|
+
mode: 'aiSdkSchema',
|
|
68
34
|
});
|
|
69
35
|
```
|
|
70
36
|
|
|
71
|
-
|
|
37
|
+
## Documentation
|
|
72
38
|
|
|
73
|
-
The package
|
|
39
|
+
The package includes compatibility layers for Anthropic, DeepSeek, Google, Meta, OpenAI, and OpenAI reasoning models. A layer decides whether it applies to the supplied model information, selects the provider's schema target, and rewrites unsupported checks or structures without changing the application's source schema.
|
|
74
40
|
|
|
75
|
-
|
|
76
|
-
import { convertZodSchemaToAISDKSchema, convertSchemaToZod } from '@mastra/schema-compat';
|
|
77
|
-
import { z } from 'zod';
|
|
78
|
-
import { jsonSchema } from 'ai';
|
|
41
|
+
`applyCompatLayer()` chooses the first applicable layer and returns either an AI SDK schema or JSON Schema. `convertZodSchemaToAISDKSchema()` converts Zod directly, while `convertSchemaToZod()` converts supported schema inputs back to Zod. Standard Schema helpers are available for extracting schemas, converting them to JSON Schema, and applying OpenAI compatibility transforms to tools.
|
|
79
42
|
|
|
80
|
-
|
|
81
|
-
name: z.string(),
|
|
82
|
-
age: z.number(),
|
|
83
|
-
});
|
|
43
|
+
The package supports both Zod v3 and v4. `SchemaCompatLayerV3` and `SchemaCompatLayerV4` expose version-specific APIs, while `SchemaCompatLayer` dispatches based on the schema version. Utilities such as `prepareJsonSchemaForOpenAIStrictMode()` and `ensureAllPropertiesRequired()` handle strict structured-output requirements.
|
|
84
44
|
|
|
85
|
-
|
|
86
|
-
const aiSchema = convertZodSchemaToAISDKSchema(zodSchema);
|
|
45
|
+
Provider layers commonly remove unsupported refinements, convert validation rules into descriptions, normalize nullable and union types, and target JSON Schema 7 or OpenAPI-compatible output as required by the provider.
|
|
87
46
|
|
|
88
|
-
|
|
89
|
-
const aiSdkSchema = jsonSchema({
|
|
90
|
-
type: 'object',
|
|
91
|
-
properties: {
|
|
92
|
-
name: { type: 'string' },
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
const backToZod = convertSchemaToZod(aiSdkSchema);
|
|
96
|
-
```
|
|
47
|
+
## Changelog
|
|
97
48
|
|
|
98
|
-
|
|
49
|
+
See the [package changelog](https://github.com/mastra-ai/mastra/blob/main/packages/schema-compat/CHANGELOG.md) for version history and release notes.
|
|
99
50
|
|
|
100
|
-
|
|
51
|
+
## Support
|
|
101
52
|
|
|
102
|
-
|
|
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
|
-
```
|
|
53
|
+
We have an [open community Discord](https://discord.gg/mastra-ai). Come and say hello and let us know if you have any questions or need any help getting things running.
|
package/dist/index.cjs
CHANGED
|
@@ -281,7 +281,7 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
|
281
281
|
return (size = defaultSize) => {
|
|
282
282
|
let id = "";
|
|
283
283
|
let i = size | 0;
|
|
284
|
-
while (i--) id += alphabet[Math.random() * alphabet.length | 0];
|
|
284
|
+
while (i-- > 0) id += alphabet[Math.random() * alphabet.length | 0];
|
|
285
285
|
return id;
|
|
286
286
|
};
|
|
287
287
|
};
|