@mastra/schema-compat 0.0.0-ai-v5-20250625173645

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/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@mastra/schema-compat",
3
+ "version": "0.0.0-ai-v5-20250625173645",
4
+ "description": "Tool schema compatibility layer for Mastra.ai",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.cts",
16
+ "default": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "keywords": [
22
+ "mastra",
23
+ "schema",
24
+ "tool",
25
+ "compatibility",
26
+ "zod"
27
+ ],
28
+ "author": "",
29
+ "license": "Elastic-2.0",
30
+ "dependencies": {
31
+ "json-schema": "^0.4.0",
32
+ "zod-from-json-schema": "^0.0.5",
33
+ "zod-to-json-schema": "^3.24.5"
34
+ },
35
+ "peerDependencies": {
36
+ "ai": "5.0.0-beta.1",
37
+ "zod": "^3.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/json-schema": "^7.0.15",
41
+ "ai": "5.0.0-beta.1",
42
+ "eslint": "^9.28.0",
43
+ "tsup": "^8.5.0",
44
+ "@types/node": "^20.19.0",
45
+ "typescript": "^5.8.3",
46
+ "vitest": "^3.2.3",
47
+ "zod": "^3.25.57",
48
+ "@internal/lint": "0.0.0-ai-v5-20250625173645"
49
+ },
50
+ "scripts": {
51
+ "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
52
+ "build:watch": "pnpm build --watch",
53
+ "test": "vitest run",
54
+ "lint": "eslint ."
55
+ }
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1,39 @@
1
+ // Schema compatibility base class and related types
2
+ export {
3
+ SchemaCompatLayer,
4
+ // Constants
5
+ ALL_STRING_CHECKS,
6
+ ALL_NUMBER_CHECKS,
7
+ ALL_ARRAY_CHECKS,
8
+ UNSUPPORTED_ZOD_TYPES,
9
+ SUPPORTED_ZOD_TYPES,
10
+ ALL_ZOD_TYPES,
11
+ // Types
12
+ type StringCheckType,
13
+ type NumberCheckType,
14
+ type ArrayCheckType,
15
+ type UnsupportedZodType,
16
+ type SupportedZodType,
17
+ type AllZodType,
18
+ type ZodShape,
19
+ type ShapeKey,
20
+ type ShapeValue,
21
+ // Re-usable type predicates
22
+ isOptional,
23
+ isObj,
24
+ isArr,
25
+ isUnion,
26
+ isString,
27
+ isNumber,
28
+ } from './schema-compatibility';
29
+
30
+ // Utility functions
31
+ export { convertZodSchemaToAISDKSchema, applyCompatLayer, convertSchemaToZod } from './utils';
32
+
33
+ // Provider compatibility implementations
34
+ export { AnthropicSchemaCompatLayer } from './provider-compats/anthropic';
35
+ export { DeepSeekSchemaCompatLayer } from './provider-compats/deepseek';
36
+ export { GoogleSchemaCompatLayer } from './provider-compats/google';
37
+ export { MetaSchemaCompatLayer } from './provider-compats/meta';
38
+ export { OpenAISchemaCompatLayer } from './provider-compats/openai';
39
+ export { OpenAIReasoningSchemaCompatLayer } from './provider-compats/openai-reasoning';
@@ -0,0 +1,48 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ import type { Targets } from 'zod-to-json-schema';
3
+ import { SchemaCompatLayer, isArr, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
+ import type { AllZodType, SchemaCompatModel } from '../schema-compatibility';
5
+
6
+ export class AnthropicSchemaCompatLayer extends SchemaCompatLayer {
7
+ constructor(model: SchemaCompatModel) {
8
+ super(model);
9
+ }
10
+
11
+ getSchemaTarget(): Targets | undefined {
12
+ return 'jsonSchema7';
13
+ }
14
+
15
+ shouldApply(): boolean {
16
+ return this.getModel().modelId.includes('claude');
17
+ }
18
+
19
+ processZodType(value: ZodTypeAny): ZodTypeAny {
20
+ if (isOptional(value)) {
21
+ const handleTypes: AllZodType[] = ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodNever', 'ZodUndefined'];
22
+ if (this.getModel().modelId.includes('claude-3.5-haiku')) handleTypes.push('ZodString');
23
+ if (this.getModel().modelId.includes('claude-3.7')) handleTypes.push('ZodTuple');
24
+ return this.defaultZodOptionalHandler(value, handleTypes);
25
+ } else if (isObj(value)) {
26
+ return this.defaultZodObjectHandler(value);
27
+ } else if (isArr(value)) {
28
+ return this.defaultZodArrayHandler(value, []);
29
+ } else if (isUnion(value)) {
30
+ return this.defaultZodUnionHandler(value);
31
+ } else if (isString(value)) {
32
+ // the claude-3.5-haiku model support these properties but the model doesn't respect them, but it respects them when they're
33
+ // added to the tool description
34
+
35
+ if (this.getModel().modelId.includes('claude-3.5-haiku')) {
36
+ return this.defaultZodStringHandler(value, ['max', 'min']);
37
+ } else {
38
+ return value;
39
+ }
40
+ }
41
+
42
+ if (this.getModel().modelId.includes('claude-3.7')) {
43
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodTuple', 'ZodUndefined']);
44
+ } else {
45
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined']);
46
+ }
47
+ }
48
+ }
@@ -0,0 +1,35 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ import type { Targets } from 'zod-to-json-schema';
3
+ import { SchemaCompatLayer, isArr, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
+ import type { SchemaCompatModel } from '../schema-compatibility';
5
+
6
+ export class DeepSeekSchemaCompatLayer extends SchemaCompatLayer {
7
+ constructor(model: SchemaCompatModel) {
8
+ super(model);
9
+ }
10
+
11
+ getSchemaTarget(): Targets | undefined {
12
+ return 'jsonSchema7';
13
+ }
14
+
15
+ shouldApply(): boolean {
16
+ // Deepseek R1 performs perfectly without this compat layer
17
+ return this.getModel().modelId.includes('deepseek') && !this.getModel().modelId.includes('r1');
18
+ }
19
+
20
+ processZodType(value: ZodTypeAny): ZodTypeAny {
21
+ if (isOptional(value)) {
22
+ return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
23
+ } else if (isObj(value)) {
24
+ return this.defaultZodObjectHandler(value);
25
+ } else if (isArr(value)) {
26
+ return this.defaultZodArrayHandler(value, ['min', 'max']);
27
+ } else if (isUnion(value)) {
28
+ return this.defaultZodUnionHandler(value);
29
+ } else if (isString(value)) {
30
+ return this.defaultZodStringHandler(value);
31
+ }
32
+
33
+ return value;
34
+ }
35
+ }
@@ -0,0 +1,55 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ import type { Targets } from 'zod-to-json-schema';
3
+ import {
4
+ SchemaCompatLayer,
5
+ UNSUPPORTED_ZOD_TYPES,
6
+ isArr,
7
+ isNumber,
8
+ isObj,
9
+ isOptional,
10
+ isString,
11
+ isUnion,
12
+ } from '../schema-compatibility';
13
+ import type { SchemaCompatModel } from '../schema-compatibility';
14
+
15
+ export class GoogleSchemaCompatLayer extends SchemaCompatLayer {
16
+ constructor(model: SchemaCompatModel) {
17
+ super(model);
18
+ }
19
+
20
+ getSchemaTarget(): Targets | undefined {
21
+ return 'jsonSchema7';
22
+ }
23
+
24
+ shouldApply(): boolean {
25
+ return this.getModel().provider.includes('google') || this.getModel().modelId.includes('google');
26
+ }
27
+
28
+ processZodType(value: ZodTypeAny): ZodTypeAny {
29
+ if (isOptional(value)) {
30
+ return this.defaultZodOptionalHandler(value, [
31
+ 'ZodObject',
32
+ 'ZodArray',
33
+ 'ZodUnion',
34
+ 'ZodString',
35
+ 'ZodNumber',
36
+ ...UNSUPPORTED_ZOD_TYPES,
37
+ ]);
38
+ } else if (isObj(value)) {
39
+ return this.defaultZodObjectHandler(value);
40
+ } else if (isArr(value)) {
41
+ return this.defaultZodArrayHandler(value, []);
42
+ } else if (isUnion(value)) {
43
+ return this.defaultZodUnionHandler(value);
44
+ } else if (isString(value)) {
45
+ // Google models support these properties but the model doesn't respect them, but it respects them when they're
46
+ // added to the tool description
47
+ return this.defaultZodStringHandler(value);
48
+ } else if (isNumber(value)) {
49
+ // Google models support these properties but the model doesn't respect them, but it respects them when they're
50
+ // added to the tool description
51
+ return this.defaultZodNumberHandler(value);
52
+ }
53
+ return this.defaultUnsupportedZodTypeHandler(value);
54
+ }
55
+ }
@@ -0,0 +1,36 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ import type { Targets } from 'zod-to-json-schema';
3
+ import { SchemaCompatLayer, isArr, isNumber, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
+ import type { SchemaCompatModel } from '../schema-compatibility';
5
+
6
+ export class MetaSchemaCompatLayer extends SchemaCompatLayer {
7
+ constructor(model: SchemaCompatModel) {
8
+ super(model);
9
+ }
10
+
11
+ getSchemaTarget(): Targets | undefined {
12
+ return 'jsonSchema7';
13
+ }
14
+
15
+ shouldApply(): boolean {
16
+ return this.getModel().modelId.includes('meta');
17
+ }
18
+
19
+ processZodType(value: ZodTypeAny): ZodTypeAny {
20
+ if (isOptional(value)) {
21
+ return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
22
+ } else if (isObj(value)) {
23
+ return this.defaultZodObjectHandler(value);
24
+ } else if (isArr(value)) {
25
+ return this.defaultZodArrayHandler(value, ['min', 'max']);
26
+ } else if (isUnion(value)) {
27
+ return this.defaultZodUnionHandler(value);
28
+ } else if (isNumber(value)) {
29
+ return this.defaultZodNumberHandler(value);
30
+ } else if (isString(value)) {
31
+ return this.defaultZodStringHandler(value);
32
+ }
33
+
34
+ return value;
35
+ }
36
+ }
@@ -0,0 +1,88 @@
1
+ import { z } from 'zod';
2
+ import type { ZodTypeAny } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import {
5
+ SchemaCompatLayer,
6
+ isArr,
7
+ isDate,
8
+ isDefault,
9
+ isNumber,
10
+ isObj,
11
+ isOptional,
12
+ isString,
13
+ isUnion,
14
+ } from '../schema-compatibility';
15
+ import type { SchemaCompatModel } from '../schema-compatibility';
16
+
17
+ export class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
18
+ constructor(model: SchemaCompatModel) {
19
+ super(model);
20
+ }
21
+
22
+ getSchemaTarget(): Targets | undefined {
23
+ return `openApi3`;
24
+ }
25
+
26
+ isReasoningModel(): boolean {
27
+ // there isn't a good way to automatically detect reasoning models besides doing this.
28
+ // in the future when o5 is released this compat wont apply and we'll want to come back and update this class + our tests
29
+ return this.getModel().modelId.includes(`o3`) || this.getModel().modelId.includes(`o4`);
30
+ }
31
+
32
+ shouldApply(): boolean {
33
+ if (
34
+ // (this.getModel().supportsStructuredOutputs || this.isReasoningModel()) && // <- supportsStructuredOutputs no longer exists
35
+ this.isReasoningModel() &&
36
+ (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))
37
+ ) {
38
+ return true;
39
+ }
40
+
41
+ return false;
42
+ }
43
+
44
+ processZodType(value: ZodTypeAny): ZodTypeAny {
45
+ if (isOptional(value)) {
46
+ const innerZodType = this.processZodType(value._def.innerType);
47
+ return innerZodType.nullable();
48
+ } else if (isObj(value)) {
49
+ return this.defaultZodObjectHandler(value);
50
+ } else if (isArr(value)) {
51
+ return this.defaultZodArrayHandler(value);
52
+ } else if (isUnion(value)) {
53
+ return this.defaultZodUnionHandler(value);
54
+ } else if (isDefault(value)) {
55
+ const defaultDef = value._def;
56
+ const innerType = defaultDef.innerType;
57
+ const defaultValue = defaultDef.defaultValue();
58
+ const constraints: { defaultValue?: unknown } = {};
59
+ if (defaultValue !== undefined) {
60
+ constraints.defaultValue = defaultValue;
61
+ }
62
+
63
+ const description = this.mergeParameterDescription(value.description, constraints);
64
+ let result = this.processZodType(innerType);
65
+ if (description) {
66
+ result = result.describe(description);
67
+ }
68
+ return result;
69
+ } else if (isNumber(value)) {
70
+ return this.defaultZodNumberHandler(value);
71
+ } else if (isString(value)) {
72
+ return this.defaultZodStringHandler(value);
73
+ } else if (isDate(value)) {
74
+ return this.defaultZodDateHandler(value);
75
+ } else if (value._def.typeName === 'ZodAny') {
76
+ // It's bad practice in the tool to use any, it's not reasonable for models that don't support that OOTB, to cast every single possible type
77
+ // in the schema. Usually when it's "any" it could be a json object or a union of specific types.
78
+ return z
79
+ .string()
80
+ .describe(
81
+ (value.description ?? '') +
82
+ `\nArgument was an "any" type, but you (the LLM) do not support "any", so it was cast to a "string" type`,
83
+ );
84
+ }
85
+
86
+ return this.defaultUnsupportedZodTypeHandler(value);
87
+ }
88
+ }
@@ -0,0 +1,56 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ import type { Targets } from 'zod-to-json-schema';
3
+ import { SchemaCompatLayer, isArr, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
+ import type { SchemaCompatModel, StringCheckType } from '../schema-compatibility';
5
+
6
+ export class OpenAISchemaCompatLayer extends SchemaCompatLayer {
7
+ constructor(model: SchemaCompatModel) {
8
+ super(model);
9
+ }
10
+
11
+ getSchemaTarget(): Targets | undefined {
12
+ return `jsonSchema7`;
13
+ }
14
+
15
+ shouldApply(): boolean {
16
+ if (
17
+ // !this.getModel().supportsStructuredOutputs && // <- TODO: this no longer exists, do we need it?
18
+ this.getModel().provider.includes(`openai`) ||
19
+ this.getModel().modelId.includes(`openai`)
20
+ ) {
21
+ return true;
22
+ }
23
+
24
+ return false;
25
+ }
26
+
27
+ processZodType(value: ZodTypeAny): ZodTypeAny {
28
+ if (isOptional(value)) {
29
+ return this.defaultZodOptionalHandler(value, [
30
+ 'ZodObject',
31
+ 'ZodArray',
32
+ 'ZodUnion',
33
+ 'ZodString',
34
+ 'ZodNever',
35
+ 'ZodUndefined',
36
+ 'ZodTuple',
37
+ ]);
38
+ } else if (isObj(value)) {
39
+ return this.defaultZodObjectHandler(value);
40
+ } else if (isUnion(value)) {
41
+ return this.defaultZodUnionHandler(value);
42
+ } else if (isArr(value)) {
43
+ return this.defaultZodArrayHandler(value);
44
+ } else if (isString(value)) {
45
+ const model = this.getModel();
46
+ const checks: StringCheckType[] = ['emoji'];
47
+
48
+ if (model.modelId.includes('gpt-4o-mini')) {
49
+ checks.push('regex');
50
+ }
51
+ return this.defaultZodStringHandler(value, checks);
52
+ }
53
+
54
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined', 'ZodTuple']);
55
+ }
56
+ }