@mastra/schema-compat 0.10.7 → 0.11.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.
Files changed (67) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +40 -0
  3. package/README.md +0 -4
  4. package/dist/chunk-FTKGHMGD.js +27 -0
  5. package/dist/chunk-FTKGHMGD.js.map +1 -0
  6. package/dist/chunk-LNR4XKDU.cjs +33 -0
  7. package/dist/chunk-LNR4XKDU.cjs.map +1 -0
  8. package/dist/index.cjs +846 -84
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.ts +10 -8
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +845 -85
  13. package/dist/index.js.map +1 -1
  14. package/dist/provider-compats/anthropic.d.ts +6 -4
  15. package/dist/provider-compats/anthropic.d.ts.map +1 -1
  16. package/dist/provider-compats/deepseek.d.ts +6 -4
  17. package/dist/provider-compats/deepseek.d.ts.map +1 -1
  18. package/dist/provider-compats/google.d.ts +6 -4
  19. package/dist/provider-compats/google.d.ts.map +1 -1
  20. package/dist/provider-compats/meta.d.ts +6 -4
  21. package/dist/provider-compats/meta.d.ts.map +1 -1
  22. package/dist/provider-compats/openai-reasoning.d.ts +6 -4
  23. package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
  24. package/dist/provider-compats/openai.d.ts +6 -4
  25. package/dist/provider-compats/openai.d.ts.map +1 -1
  26. package/dist/schema-compatibility-v3.d.ts +319 -0
  27. package/dist/schema-compatibility-v3.d.ts.map +1 -0
  28. package/dist/schema-compatibility-v4.d.ts +310 -0
  29. package/dist/schema-compatibility-v4.d.ts.map +1 -0
  30. package/dist/schema-compatibility.d.ts +80 -134
  31. package/dist/schema-compatibility.d.ts.map +1 -1
  32. package/dist/types.d.ts +6 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/utils-test-suite.d.ts +2 -0
  35. package/dist/utils-test-suite.d.ts.map +1 -0
  36. package/dist/utils.d.ts +17 -5
  37. package/dist/utils.d.ts.map +1 -1
  38. package/dist/zod-to-json.cjs +12 -0
  39. package/dist/zod-to-json.cjs.map +1 -0
  40. package/dist/zod-to-json.d.ts +6 -0
  41. package/dist/zod-to-json.d.ts.map +1 -0
  42. package/dist/zod-to-json.js +3 -0
  43. package/dist/zod-to-json.js.map +1 -0
  44. package/dist/zodTypes.d.ts +21 -0
  45. package/dist/zodTypes.d.ts.map +1 -0
  46. package/package.json +16 -6
  47. package/src/index.ts +4 -3
  48. package/src/provider-compats/anthropic.ts +29 -11
  49. package/src/provider-compats/deepseek.ts +14 -9
  50. package/src/provider-compats/google.ts +18 -32
  51. package/src/provider-compats/meta.ts +15 -10
  52. package/src/provider-compats/openai-reasoning.ts +18 -24
  53. package/src/provider-compats/openai.ts +22 -12
  54. package/src/schema-compatibility-v3.ts +664 -0
  55. package/src/schema-compatibility-v4.test.ts +476 -0
  56. package/src/schema-compatibility-v4.ts +706 -0
  57. package/src/schema-compatibility.test.ts +12 -28
  58. package/src/schema-compatibility.ts +263 -383
  59. package/src/types.ts +5 -0
  60. package/src/utils-test-suite.ts +467 -0
  61. package/src/utils-v3.test.ts +9 -0
  62. package/src/utils-v4.test.ts +9 -0
  63. package/src/utils.ts +30 -24
  64. package/src/zod-to-json.ts +27 -0
  65. package/src/zodTypes.ts +56 -0
  66. package/tsup.config.ts +8 -3
  67. package/src/utils.test.ts +0 -460
@@ -1,7 +1,10 @@
1
- import type { ZodTypeAny } from 'zod';
1
+ import { z } from 'zod';
2
+ import type { ZodType as ZodTypeV3 } from 'zod/v3';
3
+ import type { ZodType as ZodTypeV4 } from 'zod/v4';
2
4
  import type { Targets } from 'zod-to-json-schema';
3
- import { SchemaCompatLayer, isArr, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
- import type { ModelInformation } from '../schema-compatibility';
5
+ import { SchemaCompatLayer } from '../schema-compatibility';
6
+ import type { ModelInformation } from '../types';
7
+ import { isOptional, isObj, isArr, isUnion, isString } from '../zodTypes';
5
8
 
6
9
  export class DeepSeekSchemaCompatLayer extends SchemaCompatLayer {
7
10
  constructor(model: ModelInformation) {
@@ -17,16 +20,18 @@ export class DeepSeekSchemaCompatLayer extends SchemaCompatLayer {
17
20
  return this.getModel().modelId.includes('deepseek') && !this.getModel().modelId.includes('r1');
18
21
  }
19
22
 
20
- processZodType(value: ZodTypeAny): ZodTypeAny {
21
- if (isOptional(value)) {
23
+ processZodType(value: ZodTypeV3): ZodTypeV3;
24
+ processZodType(value: ZodTypeV4): ZodTypeV4;
25
+ processZodType(value: ZodTypeV3 | ZodTypeV4): ZodTypeV3 | ZodTypeV4 {
26
+ if (isOptional(z)(value)) {
22
27
  return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
23
- } else if (isObj(value)) {
28
+ } else if (isObj(z)(value)) {
24
29
  return this.defaultZodObjectHandler(value);
25
- } else if (isArr(value)) {
30
+ } else if (isArr(z)(value)) {
26
31
  return this.defaultZodArrayHandler(value, ['min', 'max']);
27
- } else if (isUnion(value)) {
32
+ } else if (isUnion(z)(value)) {
28
33
  return this.defaultZodUnionHandler(value);
29
- } else if (isString(value)) {
34
+ } else if (isString(z)(value)) {
30
35
  return this.defaultZodStringHandler(value);
31
36
  }
32
37
 
@@ -1,18 +1,10 @@
1
- import type { ZodTypeAny } from 'zod';
2
1
  import { z } from 'zod';
2
+ import type { ZodType as ZodTypeV3, ZodObject as ZodObjectV3 } from 'zod/v3';
3
+ import type { ZodType as ZodTypeV4, ZodObject as ZodObjectV4 } from 'zod/v4';
3
4
  import type { Targets } from 'zod-to-json-schema';
4
- import type { ModelInformation } from '../schema-compatibility';
5
- import {
6
- SchemaCompatLayer,
7
- UNSUPPORTED_ZOD_TYPES,
8
- isArr,
9
- isNull,
10
- isNumber,
11
- isObj,
12
- isOptional,
13
- isString,
14
- isUnion,
15
- } from '../schema-compatibility';
5
+ import { SchemaCompatLayer } from '../schema-compatibility';
6
+ import type { ModelInformation } from '../types';
7
+ import { isOptional, isNull, isObj, isArr, isUnion, isString, isNumber } from '../zodTypes';
16
8
 
17
9
  export class GoogleSchemaCompatLayer extends SchemaCompatLayer {
18
10
  constructor(model: ModelInformation) {
@@ -26,38 +18,32 @@ export class GoogleSchemaCompatLayer extends SchemaCompatLayer {
26
18
  shouldApply(): boolean {
27
19
  return this.getModel().provider.includes('google') || this.getModel().modelId.includes('google');
28
20
  }
29
-
30
- processZodType(value: ZodTypeAny): ZodTypeAny {
31
- if (isOptional(value)) {
32
- return this.defaultZodOptionalHandler(value, [
33
- 'ZodObject',
34
- 'ZodArray',
35
- 'ZodUnion',
36
- 'ZodString',
37
- 'ZodNumber',
38
- ...UNSUPPORTED_ZOD_TYPES,
39
- ]);
40
- } else if (isNull(value)) {
21
+ processZodType(value: ZodTypeV3): ZodTypeV3;
22
+ processZodType(value: ZodTypeV4): ZodTypeV4;
23
+ processZodType(value: ZodTypeV3 | ZodTypeV4): ZodTypeV3 | ZodTypeV4 {
24
+ if (isOptional(z)(value)) {
25
+ return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
26
+ } else if (isNull(z)(value)) {
41
27
  // Google models don't support null, so we need to convert it to any and then refine it to null
42
28
  return z
43
29
  .any()
44
30
  .refine(v => v === null, { message: 'must be null' })
45
- .describe(value._def.description || 'must be null');
46
- } else if (isObj(value)) {
31
+ .describe(value.description || 'must be null');
32
+ } else if (isObj(z)(value)) {
47
33
  return this.defaultZodObjectHandler(value);
48
- } else if (isArr(value)) {
34
+ } else if (isArr(z)(value)) {
49
35
  return this.defaultZodArrayHandler(value, []);
50
- } else if (isUnion(value)) {
36
+ } else if (isUnion(z)(value)) {
51
37
  return this.defaultZodUnionHandler(value);
52
- } else if (isString(value)) {
38
+ } else if (isString(z)(value)) {
53
39
  // Google models support these properties but the model doesn't respect them, but it respects them when they're
54
40
  // added to the tool description
55
41
  return this.defaultZodStringHandler(value);
56
- } else if (isNumber(value)) {
42
+ } else if (isNumber(z)(value)) {
57
43
  // Google models support these properties but the model doesn't respect them, but it respects them when they're
58
44
  // added to the tool description
59
45
  return this.defaultZodNumberHandler(value);
60
46
  }
61
- return this.defaultUnsupportedZodTypeHandler(value);
47
+ return this.defaultUnsupportedZodTypeHandler(value as ZodObjectV4<any> | ZodObjectV3<any>);
62
48
  }
63
49
  }
@@ -1,7 +1,10 @@
1
- import type { ZodTypeAny } from 'zod';
1
+ import { z } from 'zod';
2
+ import type { ZodType as ZodTypeV3 } from 'zod/v3';
3
+ import type { ZodType as ZodTypeV4 } from 'zod/v4';
2
4
  import type { Targets } from 'zod-to-json-schema';
3
- import { SchemaCompatLayer, isArr, isNumber, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
- import type { ModelInformation } from '../schema-compatibility';
5
+ import { SchemaCompatLayer } from '../schema-compatibility';
6
+ import type { ModelInformation } from '../types';
7
+ import { isOptional, isObj, isArr, isUnion, isNumber, isString } from '../zodTypes';
5
8
 
6
9
  export class MetaSchemaCompatLayer extends SchemaCompatLayer {
7
10
  constructor(model: ModelInformation) {
@@ -16,18 +19,20 @@ export class MetaSchemaCompatLayer extends SchemaCompatLayer {
16
19
  return this.getModel().modelId.includes('meta');
17
20
  }
18
21
 
19
- processZodType(value: ZodTypeAny): ZodTypeAny {
20
- if (isOptional(value)) {
22
+ processZodType(value: ZodTypeV3): ZodTypeV3;
23
+ processZodType(value: ZodTypeV4): ZodTypeV4;
24
+ processZodType(value: ZodTypeV3 | ZodTypeV4): ZodTypeV3 | ZodTypeV4 {
25
+ if (isOptional(z)(value)) {
21
26
  return this.defaultZodOptionalHandler(value, ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodString', 'ZodNumber']);
22
- } else if (isObj(value)) {
27
+ } else if (isObj(z)(value)) {
23
28
  return this.defaultZodObjectHandler(value);
24
- } else if (isArr(value)) {
29
+ } else if (isArr(z)(value)) {
25
30
  return this.defaultZodArrayHandler(value, ['min', 'max']);
26
- } else if (isUnion(value)) {
31
+ } else if (isUnion(z)(value)) {
27
32
  return this.defaultZodUnionHandler(value);
28
- } else if (isNumber(value)) {
33
+ } else if (isNumber(z)(value)) {
29
34
  return this.defaultZodNumberHandler(value);
30
- } else if (isString(value)) {
35
+ } else if (isString(z)(value)) {
31
36
  return this.defaultZodStringHandler(value);
32
37
  }
33
38
 
@@ -1,18 +1,10 @@
1
1
  import { z } from 'zod';
2
- import type { ZodTypeAny } from 'zod';
2
+ import type { ZodType as ZodTypeV3, ZodObject as ZodObjectV3 } from 'zod/v3';
3
+ import type { ZodType as ZodTypeV4, ZodObject as ZodObjectV4 } from 'zod/v4';
3
4
  import type { Targets } from 'zod-to-json-schema';
4
- import type { ModelInformation } from '../schema-compatibility';
5
- import {
6
- SchemaCompatLayer,
7
- isArr,
8
- isDate,
9
- isDefault,
10
- isNumber,
11
- isObj,
12
- isOptional,
13
- isString,
14
- isUnion,
15
- } from '../schema-compatibility';
5
+ import { SchemaCompatLayer } from '../schema-compatibility';
6
+ import type { ModelInformation } from '../types';
7
+ import { isOptional, isObj, isArr, isUnion, isDefault, isNumber, isString, isDate } from '../zodTypes';
16
8
 
17
9
  export class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
18
10
  constructor(model: ModelInformation) {
@@ -44,17 +36,19 @@ export class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
44
36
  return false;
45
37
  }
46
38
 
47
- processZodType(value: ZodTypeAny): ZodTypeAny {
48
- if (isOptional(value)) {
39
+ processZodType(value: ZodTypeV3): ZodTypeV3;
40
+ processZodType(value: ZodTypeV4): ZodTypeV4;
41
+ processZodType(value: ZodTypeV3 | ZodTypeV4): ZodTypeV3 | ZodTypeV4 {
42
+ if (isOptional(z)(value)) {
49
43
  const innerZodType = this.processZodType(value._def.innerType);
50
44
  return innerZodType.nullable();
51
- } else if (isObj(value)) {
45
+ } else if (isObj(z)(value)) {
52
46
  return this.defaultZodObjectHandler(value, { passthrough: false });
53
- } else if (isArr(value)) {
47
+ } else if (isArr(z)(value)) {
54
48
  return this.defaultZodArrayHandler(value);
55
- } else if (isUnion(value)) {
49
+ } else if (isUnion(z)(value)) {
56
50
  return this.defaultZodUnionHandler(value);
57
- } else if (isDefault(value)) {
51
+ } else if (isDefault(z)(value)) {
58
52
  const defaultDef = value._def;
59
53
  const innerType = defaultDef.innerType;
60
54
  const defaultValue = defaultDef.defaultValue();
@@ -69,13 +63,13 @@ export class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
69
63
  result = result.describe(description);
70
64
  }
71
65
  return result;
72
- } else if (isNumber(value)) {
66
+ } else if (isNumber(z)(value)) {
73
67
  return this.defaultZodNumberHandler(value);
74
- } else if (isString(value)) {
68
+ } else if (isString(z)(value)) {
75
69
  return this.defaultZodStringHandler(value);
76
- } else if (isDate(value)) {
70
+ } else if (isDate(z)(value)) {
77
71
  return this.defaultZodDateHandler(value);
78
- } else if (value._def.typeName === 'ZodAny') {
72
+ } else if (value.constructor.name === 'ZodAny') {
79
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
80
74
  // in the schema. Usually when it's "any" it could be a json object or a union of specific types.
81
75
  return z
@@ -86,6 +80,6 @@ export class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
86
80
  );
87
81
  }
88
82
 
89
- return this.defaultUnsupportedZodTypeHandler(value);
83
+ return this.defaultUnsupportedZodTypeHandler(value as ZodObjectV4<any> | ZodObjectV3<any>);
90
84
  }
91
85
  }
@@ -1,7 +1,10 @@
1
- import type { ZodTypeAny } from 'zod';
1
+ import { z } from 'zod';
2
+ import type { ZodType as ZodTypeV3, ZodObject as ZodObjectV3 } from 'zod/v3';
3
+ import type { ZodType as ZodTypeV4, ZodObject as ZodObjectV4 } from 'zod/v4';
2
4
  import type { Targets } from 'zod-to-json-schema';
3
- import { SchemaCompatLayer, isArr, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
4
- import type { ModelInformation, StringCheckType } from '../schema-compatibility';
5
+ import { SchemaCompatLayer } from '../schema-compatibility';
6
+ import type { ModelInformation } from '../types';
7
+ import { isOptional, isObj, isUnion, isArr, isString } from '../zodTypes';
5
8
 
6
9
  export class OpenAISchemaCompatLayer extends SchemaCompatLayer {
7
10
  constructor(model: ModelInformation) {
@@ -23,8 +26,10 @@ export class OpenAISchemaCompatLayer extends SchemaCompatLayer {
23
26
  return false;
24
27
  }
25
28
 
26
- processZodType(value: ZodTypeAny): ZodTypeAny {
27
- if (isOptional(value)) {
29
+ processZodType(value: ZodTypeV3): ZodTypeV3;
30
+ processZodType(value: ZodTypeV4): ZodTypeV4;
31
+ processZodType(value: ZodTypeV3 | ZodTypeV4): ZodTypeV3 | ZodTypeV4 {
32
+ if (isOptional(z)(value)) {
28
33
  return this.defaultZodOptionalHandler(value, [
29
34
  'ZodObject',
30
35
  'ZodArray',
@@ -34,22 +39,27 @@ export class OpenAISchemaCompatLayer extends SchemaCompatLayer {
34
39
  'ZodUndefined',
35
40
  'ZodTuple',
36
41
  ]);
37
- } else if (isObj(value)) {
42
+ } else if (isObj(z)(value)) {
38
43
  return this.defaultZodObjectHandler(value);
39
- } else if (isUnion(value)) {
44
+ } else if (isUnion(z)(value)) {
40
45
  return this.defaultZodUnionHandler(value);
41
- } else if (isArr(value)) {
46
+ } else if (isArr(z)(value)) {
42
47
  return this.defaultZodArrayHandler(value);
43
- } else if (isString(value)) {
48
+ } else if (isString(z)(value)) {
44
49
  const model = this.getModel();
45
- const checks: StringCheckType[] = ['emoji'];
50
+ const checks = ['emoji'] as const;
46
51
 
47
52
  if (model.modelId.includes('gpt-4o-mini')) {
48
- checks.push('regex');
53
+ return this.defaultZodStringHandler(value, ['emoji', 'regex']);
49
54
  }
55
+
50
56
  return this.defaultZodStringHandler(value, checks);
51
57
  }
52
58
 
53
- return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined', 'ZodTuple']);
59
+ return this.defaultUnsupportedZodTypeHandler(value as ZodObjectV4<any> | ZodObjectV3<any>, [
60
+ 'ZodNever',
61
+ 'ZodUndefined',
62
+ 'ZodTuple',
63
+ ]);
54
64
  }
55
65
  }