@mastra/schema-compat 1.3.8-alpha.0 → 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/README.md +23 -114
- package/package.json +3 -3
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/schema-compat",
|
|
3
|
-
"version": "1.3.8-alpha.
|
|
3
|
+
"version": "1.3.8-alpha.1",
|
|
4
4
|
"description": "Tool schema compatibility layer for Mastra.ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -117,10 +117,10 @@
|
|
|
117
117
|
"zod-v3": "npm:zod@^3.25.76",
|
|
118
118
|
"zod-to-json-schema": "^3.25.2",
|
|
119
119
|
"zod-from-json-schema-v3": "npm:zod-from-json-schema@^0.0.5",
|
|
120
|
-
"@internal/ai-sdk-v5": "0.0.76",
|
|
121
|
-
"@internal/ai-v6": "0.0.76",
|
|
122
120
|
"@internal/ai-sdk-v4": "0.0.76",
|
|
121
|
+
"@internal/ai-sdk-v5": "0.0.76",
|
|
123
122
|
"@internal/lint": "0.0.129",
|
|
123
|
+
"@internal/ai-v6": "0.0.76",
|
|
124
124
|
"@internal/llm-recorder": "0.0.65",
|
|
125
125
|
"@internal/test-utils": "0.0.65",
|
|
126
126
|
"@internal/types-builder": "0.0.104"
|