@mastra/schema-compat 0.10.2-alpha.2

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.10.2-alpha.2",
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
+ "zod-from-json-schema": "^0.0.5",
32
+ "zod-to-json-schema": "^3.24.5",
33
+ "json-schema": "^0.4.0"
34
+ },
35
+ "peerDependencies": {
36
+ "zod": "^3.0.0",
37
+ "ai": "^4.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^20.17.57",
41
+ "@types/json-schema": "^7.0.15",
42
+ "eslint": "^9.28.0",
43
+ "tsup": "^8.5.0",
44
+ "typescript": "^5.8.2",
45
+ "vitest": "^3.1.2",
46
+ "zod": "^3.24.3",
47
+ "ai": "4.3.16",
48
+ "@internal/lint": "0.0.10"
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,32 @@
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
+ } from './schema-compatibility';
22
+
23
+ // Utility functions
24
+ export { convertZodSchemaToAISDKSchema, applyCompatLayer, convertSchemaToZod } from './utils';
25
+
26
+ // Provider compatibility implementations
27
+ export { AnthropicSchemaCompatLayer } from './provider-compats/anthropic';
28
+ export { DeepSeekSchemaCompatLayer } from './provider-compats/deepseek';
29
+ export { GoogleSchemaCompatLayer } from './provider-compats/google';
30
+ export { MetaSchemaCompatLayer } from './provider-compats/meta';
31
+ export { OpenAISchemaCompatLayer } from './provider-compats/openai';
32
+ export { OpenAIReasoningSchemaCompatLayer } from './provider-compats/openai-reasoning';
@@ -0,0 +1,54 @@
1
+ import type { LanguageModelV1 } from 'ai';
2
+ import type { z } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import { SchemaCompatLayer } from '../schema-compatibility';
5
+ import type { ShapeValue, AllZodType } from '../schema-compatibility';
6
+
7
+ export class AnthropicSchemaCompatLayer extends SchemaCompatLayer {
8
+ constructor(model: LanguageModelV1) {
9
+ super(model);
10
+ }
11
+
12
+ getSchemaTarget(): Targets | undefined {
13
+ return 'jsonSchema7';
14
+ }
15
+
16
+ shouldApply(): boolean {
17
+ return this.getModel().modelId.includes('claude');
18
+ }
19
+
20
+ processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
21
+ switch (value._def.typeName) {
22
+ case 'ZodOptional':
23
+ const handleTypes: AllZodType[] = ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodNever', 'ZodUndefined'];
24
+ if (this.getModel().modelId.includes('claude-3.5-haiku')) handleTypes.push('ZodString');
25
+ if (this.getModel().modelId.includes('claude-3.7')) handleTypes.push('ZodTuple');
26
+
27
+ return this.defaultZodOptionalHandler(value, handleTypes);
28
+ case 'ZodObject': {
29
+ return this.defaultZodObjectHandler(value);
30
+ }
31
+ case 'ZodArray': {
32
+ return this.defaultZodArrayHandler(value, []);
33
+ }
34
+ case 'ZodUnion': {
35
+ return this.defaultZodUnionHandler(value);
36
+ }
37
+ // the claude-3.5-haiku model support these properties but the model doesn't respect them, but it respects them when they're
38
+ // added to the tool description
39
+ case 'ZodString': {
40
+ if (this.getModel().modelId.includes('claude-3.5-haiku')) {
41
+ return this.defaultZodStringHandler(value, ['max', 'min']);
42
+ } else {
43
+ return value as ShapeValue<T>;
44
+ }
45
+ }
46
+ default:
47
+ if (this.getModel().modelId.includes('claude-3.7')) {
48
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodTuple', 'ZodUndefined']);
49
+ } else {
50
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined']);
51
+ }
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,41 @@
1
+ import type { LanguageModelV1 } from 'ai';
2
+ import type { z } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import { SchemaCompatLayer } from '../schema-compatibility';
5
+ import type { ShapeValue } from '../schema-compatibility';
6
+
7
+ export class DeepSeekSchemaCompatLayer extends SchemaCompatLayer {
8
+ constructor(model: LanguageModelV1) {
9
+ super(model);
10
+ }
11
+
12
+ getSchemaTarget(): Targets | undefined {
13
+ return 'jsonSchema7';
14
+ }
15
+
16
+ shouldApply(): boolean {
17
+ // Deepseek R1 performs perfectly without this compat layer
18
+ return this.getModel().modelId.includes('deepseek') && !this.getModel().modelId.includes('r1');
19
+ }
20
+
21
+ processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
22
+ switch (value._def.typeName) {
23
+ case 'ZodOptional':
24
+ return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
25
+ case 'ZodObject': {
26
+ return this.defaultZodObjectHandler(value);
27
+ }
28
+ case 'ZodArray': {
29
+ return this.defaultZodArrayHandler(value, ['min', 'max']);
30
+ }
31
+ case 'ZodUnion': {
32
+ return this.defaultZodUnionHandler(value);
33
+ }
34
+ case 'ZodString': {
35
+ return this.defaultZodStringHandler(value);
36
+ }
37
+ default:
38
+ return value as ShapeValue<T>;
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,54 @@
1
+ import type { LanguageModelV1 } from 'ai';
2
+ import type { z } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import { SchemaCompatLayer, UNSUPPORTED_ZOD_TYPES } from '../schema-compatibility';
5
+ import type { ShapeValue } from '../schema-compatibility';
6
+
7
+ export class GoogleSchemaCompatLayer extends SchemaCompatLayer {
8
+ constructor(model: LanguageModelV1) {
9
+ super(model);
10
+ }
11
+
12
+ getSchemaTarget(): Targets | undefined {
13
+ return 'jsonSchema7';
14
+ }
15
+
16
+ shouldApply(): boolean {
17
+ return this.getModel().provider.includes('google') || this.getModel().modelId.includes('google');
18
+ }
19
+
20
+ processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
21
+ switch (value._def.typeName) {
22
+ case 'ZodOptional':
23
+ return this.defaultZodOptionalHandler(value, [
24
+ 'ZodObject',
25
+ 'ZodArray',
26
+ 'ZodUnion',
27
+ 'ZodString',
28
+ 'ZodNumber',
29
+ ...UNSUPPORTED_ZOD_TYPES,
30
+ ]);
31
+ case 'ZodObject': {
32
+ return this.defaultZodObjectHandler(value);
33
+ }
34
+ case 'ZodArray': {
35
+ return this.defaultZodArrayHandler(value, []);
36
+ }
37
+ case 'ZodUnion': {
38
+ return this.defaultZodUnionHandler(value);
39
+ }
40
+ // Google models support these properties but the model doesn't respect them, but it respects them when they're
41
+ // added to the tool description
42
+ case 'ZodString': {
43
+ return this.defaultZodStringHandler(value);
44
+ }
45
+ case 'ZodNumber': {
46
+ // Google models support these properties but the model doesn't respect them, but it respects them when they're
47
+ // added to the tool description
48
+ return this.defaultZodNumberHandler(value);
49
+ }
50
+ default:
51
+ return this.defaultUnsupportedZodTypeHandler(value);
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,43 @@
1
+ import type { LanguageModelV1 } from 'ai';
2
+ import type { z } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import { SchemaCompatLayer } from '../schema-compatibility';
5
+ import type { ShapeValue } from '../schema-compatibility';
6
+
7
+ export class MetaSchemaCompatLayer extends SchemaCompatLayer {
8
+ constructor(model: LanguageModelV1) {
9
+ super(model);
10
+ }
11
+
12
+ getSchemaTarget(): Targets | undefined {
13
+ return 'jsonSchema7';
14
+ }
15
+
16
+ shouldApply(): boolean {
17
+ return this.getModel().modelId.includes('meta');
18
+ }
19
+
20
+ processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
21
+ switch (value._def.typeName) {
22
+ case 'ZodOptional':
23
+ return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
24
+ case 'ZodObject': {
25
+ return this.defaultZodObjectHandler(value);
26
+ }
27
+ case 'ZodArray': {
28
+ return this.defaultZodArrayHandler(value, ['min', 'max']);
29
+ }
30
+ case 'ZodUnion': {
31
+ return this.defaultZodUnionHandler(value);
32
+ }
33
+ case 'ZodNumber': {
34
+ return this.defaultZodNumberHandler(value);
35
+ }
36
+ case 'ZodString': {
37
+ return this.defaultZodStringHandler(value);
38
+ }
39
+ default:
40
+ return value as ShapeValue<T>;
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,86 @@
1
+ import type { LanguageModelV1 } from 'ai';
2
+ import { z } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import { SchemaCompatLayer } from '../schema-compatibility';
5
+ import type { ShapeValue } from '../schema-compatibility';
6
+
7
+ export class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
8
+ constructor(model: LanguageModelV1) {
9
+ super(model);
10
+ }
11
+
12
+ getSchemaTarget(): Targets | undefined {
13
+ return `openApi3`;
14
+ }
15
+
16
+ isReasoningModel(): boolean {
17
+ // there isn't a good way to automatically detect reasoning models besides doing this.
18
+ // in the future when o5 is released this compat wont apply and we'll want to come back and update this class + our tests
19
+ return this.getModel().modelId.includes(`o3`) || this.getModel().modelId.includes(`o4`);
20
+ }
21
+
22
+ shouldApply(): boolean {
23
+ if (
24
+ (this.getModel().supportsStructuredOutputs || this.isReasoningModel()) &&
25
+ (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))
26
+ ) {
27
+ return true;
28
+ }
29
+
30
+ return false;
31
+ }
32
+
33
+ processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
34
+ switch (value._def.typeName) {
35
+ case 'ZodOptional':
36
+ const innerZodType = this.processZodType(value._def.innerType);
37
+ return innerZodType.nullable() as ShapeValue<T>;
38
+ case 'ZodObject': {
39
+ return this.defaultZodObjectHandler(value);
40
+ }
41
+ case 'ZodArray': {
42
+ return this.defaultZodArrayHandler(value);
43
+ }
44
+ case 'ZodUnion': {
45
+ return this.defaultZodUnionHandler(value);
46
+ }
47
+ case 'ZodDefault': {
48
+ const defaultDef = (value as z.ZodDefault<any>)._def;
49
+ const innerType = defaultDef.innerType;
50
+ const defaultValue = defaultDef.defaultValue();
51
+ const constraints: { defaultValue?: unknown } = {};
52
+ if (defaultValue !== undefined) {
53
+ constraints.defaultValue = defaultValue;
54
+ }
55
+
56
+ const description = this.mergeParameterDescription(value.description, constraints);
57
+ let result = this.processZodType<T>(innerType);
58
+ if (description) {
59
+ result = result.describe(description);
60
+ }
61
+ return result;
62
+ }
63
+ case 'ZodNumber': {
64
+ return this.defaultZodNumberHandler(value);
65
+ }
66
+ case 'ZodString': {
67
+ return this.defaultZodStringHandler(value);
68
+ }
69
+ case 'ZodDate': {
70
+ return this.defaultZodDateHandler(value);
71
+ }
72
+ case 'ZodAny': {
73
+ // 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
74
+ // in the schema. Usually when it's "any" it could be a json object or a union of specific types.
75
+ return z
76
+ .string()
77
+ .describe(
78
+ (value.description ?? '') +
79
+ `\nArgument was an "any" type, but you (the LLM) do not support "any", so it was cast to a "string" type`,
80
+ );
81
+ }
82
+ default:
83
+ return this.defaultUnsupportedZodTypeHandler(value);
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,61 @@
1
+ import type { LanguageModelV1 } from 'ai';
2
+ import type { z } from 'zod';
3
+ import type { Targets } from 'zod-to-json-schema';
4
+ import { SchemaCompatLayer } from '../schema-compatibility';
5
+ import type { ShapeValue, StringCheckType } from '../schema-compatibility';
6
+
7
+ export class OpenAISchemaCompatLayer extends SchemaCompatLayer {
8
+ constructor(model: LanguageModelV1) {
9
+ super(model);
10
+ }
11
+
12
+ getSchemaTarget(): Targets | undefined {
13
+ return `jsonSchema7`;
14
+ }
15
+
16
+ shouldApply(): boolean {
17
+ if (
18
+ !this.getModel().supportsStructuredOutputs &&
19
+ (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))
20
+ ) {
21
+ return true;
22
+ }
23
+
24
+ return false;
25
+ }
26
+
27
+ processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
28
+ switch (value._def.typeName) {
29
+ case 'ZodOptional':
30
+ return this.defaultZodOptionalHandler(value, [
31
+ 'ZodObject',
32
+ 'ZodArray',
33
+ 'ZodUnion',
34
+ 'ZodString',
35
+ 'ZodNever',
36
+ 'ZodUndefined',
37
+ 'ZodTuple',
38
+ ]);
39
+ case 'ZodObject': {
40
+ return this.defaultZodObjectHandler(value);
41
+ }
42
+ case 'ZodUnion': {
43
+ return this.defaultZodUnionHandler(value);
44
+ }
45
+ case 'ZodArray': {
46
+ return this.defaultZodArrayHandler(value);
47
+ }
48
+ case 'ZodString': {
49
+ const model = this.getModel();
50
+ const checks: StringCheckType[] = ['emoji'];
51
+
52
+ if (model.modelId.includes('gpt-4o-mini')) {
53
+ checks.push('regex');
54
+ }
55
+ return this.defaultZodStringHandler(value, checks);
56
+ }
57
+ default:
58
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined', 'ZodTuple']);
59
+ }
60
+ }
61
+ }