{"_id":"@caleblawson/schema-compat","name":"@caleblawson/schema-compat","dist-tags":{"latest":"0.10.2"},"versions":{"0.10.2":{"name":"@caleblawson/schema-compat","version":"0.10.2","description":"Tool schema compatibility layer for Mastra.ai","type":"module","main":"dist/index.js","types":"dist/index.d.ts","exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}},"./package.json":"./package.json"},"keywords":["mastra","schema","tool","compatibility","zod"],"author":"","license":"Elastic-2.0","dependencies":{"json-schema":"^0.4.0","zod-from-json-schema":"^0.0.5","zod-to-json-schema":"^3.24.5"},"peerDependencies":{"ai":"^4.0.0","zod":"^3.0.0"},"devDependencies":{"@types/json-schema":"^7.0.15","@types/node":"^20.19.0","ai":"4.3.16","eslint":"^9.28.0","tsup":"^8.5.0","typescript":"^5.8.3","vitest":"^3.2.3","zod":"^3.25.57","@internal/lint":"0.0.13"},"scripts":{"build":"tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting","build:watch":"pnpm build --watch","test":"vitest run","lint":"eslint ."},"gitHead":"c31e90130a5c9765c23f688ecea70adc6164543d","_id":"@caleblawson/schema-compat@0.10.2","_integrity":"sha512-VTNX01tkzPFxHYaOvhv+xpVp+uFo85uPovdw7fdRaXrrCeXKi13Pq+kz5lhOoDyvTf6+H5XfxV5e23TJG6mcTw==","_resolved":"C:\\Users\\caleb\\AppData\\Local\\Temp\\901bf9580edb2e1fabc9489b19d94e0b\\caleblawson-schema-compat-0.10.2.tgz","_from":"file:caleblawson-schema-compat-0.10.2.tgz","_nodeVersion":"21.2.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-VTNX01tkzPFxHYaOvhv+xpVp+uFo85uPovdw7fdRaXrrCeXKi13Pq+kz5lhOoDyvTf6+H5XfxV5e23TJG6mcTw==","shasum":"cb500fdf2fc898b1af85a938e4530144631cacad","tarball":"https://registry.npmjs.org/@caleblawson/schema-compat/-/schema-compat-0.10.2.tgz","fileCount":26,"unpackedSize":173774,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCIf2V9XPrJ9I5v6LZurWbZ30oW1ayQHYAQFfvKNy4eFAIhANyx8WoapSntxbcMwF/kV42d2hTZepUl0TgaeKJhtx5e"}]},"_npmUser":{"name":"caleblawson","email":"caleb.lawson@dynapt.com","actor":{"name":"caleblawson","email":"caleb.lawson@dynapt.com","type":"user"}},"directories":{},"maintainers":[{"name":"caleblawson","email":"caleb.lawson@dynapt.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/schema-compat_0.10.2_1750619776296_0.058590214796361284"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-22T19:16:16.199Z","0.10.2":"2025-06-22T19:16:16.499Z","modified":"2025-06-22T19:16:16.771Z"},"maintainers":[{"name":"caleblawson","email":"caleb.lawson@dynapt.com"}],"description":"Tool schema compatibility layer for Mastra.ai","keywords":["mastra","schema","tool","compatibility","zod"],"license":"Elastic-2.0","readme":"# @mastra/schema-compat\r\n\r\nSchema compatibility layer for Mastra.ai that provides compatibility fixes for different AI model providers when using Zod schemas with tools.\r\n\r\n## Installation\r\n\r\n```bash\r\npnpm add @mastra/schema-compat\r\n```\r\n\r\n## Usage\r\n\r\n### Basic Usage\r\n\r\nThe package provides a base `SchemaCompatLayer` class that you can extend to create custom compatibility layers for different AI model providers:\r\n\r\n```typescript\r\nimport { SchemaCompatLayer } from '@mastra/schema-compat';\r\nimport type { LanguageModelV1 } from 'ai';\r\n\r\nclass MyCustomCompat extends SchemaCompatLayer {\r\n  constructor(model: LanguageModelV1) {\r\n    super(model);\r\n  }\r\n\r\n  shouldApply(): boolean {\r\n    return this.getModel().provider === 'my-provider';\r\n  }\r\n\r\n  getSchemaTarget() {\r\n    return 'jsonSchema7';\r\n  }\r\n\r\n  processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {\r\n    // Your custom processing logic here\r\n    return value;\r\n  }\r\n}\r\n```\r\n\r\n### Schema Processing\r\n\r\nThe package includes pre-built compatibility layers for popular AI providers:\r\n\r\nUse the `applyCompatLayer` function to automatically apply the right compatibility layer:\r\n\r\n```typescript\r\nimport { applyCompatLayer, OpenAISchemaCompatLayer, AnthropicSchemaCompatLayer } from '@mastra/schema-compat';\r\nimport { yourCustomCompatibilityLayer } from './customCompatibilityLayer';\r\nimport { z } from 'zod';\r\n\r\nconst schema = z.object({\r\n  name: z.string().email(),\r\n  preferences: z.array(z.string()).min(1),\r\n});\r\n\r\nconst compatLayers = [\r\n  new OpenAISchemaCompatLayer(model),\r\n  new AnthropicSchemaCompatLayer(model),\r\n  new yourCustomCompatibilityLayer(model),\r\n];\r\n\r\n// Automatically applies the first matching compatibility\r\nconst result = applyCompatLayer({\r\n  schema,\r\n  compatLayers,\r\n  mode: 'aiSdkSchema', // or 'jsonSchema'\r\n});\r\n```\r\n\r\n### Schema Building Utilities\r\n\r\nThe package also provides utility functions for schema conversion:\r\n\r\n```typescript\r\nimport { convertZodSchemaToAISDKSchema, convertSchemaToZod } from '@mastra/schema-compat';\r\nimport { z } from 'zod';\r\nimport { jsonSchema } from 'ai';\r\n\r\nconst zodSchema = z.object({\r\n  name: z.string(),\r\n  age: z.number(),\r\n});\r\n\r\n// Convert Zod to AI SDK Schema\r\nconst aiSchema = convertZodSchemaToAISDKSchema(zodSchema);\r\n\r\n// Convert AI SDK Schema back to Zod\r\nconst aiSdkSchema = jsonSchema({\r\n  type: 'object',\r\n  properties: {\r\n    name: { type: 'string' },\r\n  },\r\n});\r\nconst backToZod = convertSchemaToZod(aiSdkSchema);\r\n```\r\n\r\n## API Reference\r\n\r\n### Classes\r\n\r\n- `SchemaCompatLayer` - Base abstract class for creating compatibility layers\r\n- `AnthropicSchemaCompatLayer` - Compatibility for Anthropic Claude models\r\n- `OpenAISchemaCompatLayer` - Compatibility for OpenAI models (without structured outputs)\r\n- `OpenAIReasoningSchemaCompatLayer` - Compatibility for OpenAI reasoning models (o1 series)\r\n- `GoogleSchemaCompatLayer` - Compatibility for Google Gemini models\r\n- `DeepSeekSchemaCompatLayer` - Compatibility for DeepSeek models\r\n- `MetaSchemaCompatLayer` - Compatibility for Meta Llama models\r\n\r\n### Functions\r\n\r\n- `applyCompatLayer(options)` - Process schema with automatic compatibility detection\r\n- `convertZodSchemaToAISDKSchema(zodSchema, target?)` - Convert Zod schema to AI SDK Schema\r\n- `convertSchemaToZod(schema)` - Convert AI SDK Schema to Zod schema\r\n\r\n### Types and Constants\r\n\r\n- `StringCheckType`, `NumberCheckType`, `ArrayCheckType` - Check types for validation\r\n- `UnsupportedZodType`, `SupportedZodType`, `AllZodType` - Zod type classifications\r\n- `ZodShape`, `ShapeKey`, `ShapeValue` - Utility types for Zod schemas\r\n- `ALL_STRING_CHECKS`, `ALL_NUMBER_CHECKS`, `ALL_ARRAY_CHECKS` - Validation constraint arrays\r\n- `SUPPORTED_ZOD_TYPES`, `UNSUPPORTED_ZOD_TYPES` - Type classification arrays\r\n\r\n## Provider-Specific Behavior\r\n\r\nDifferent AI providers have varying levels of support for JSON Schema features. This package handles these differences automatically:\r\n\r\n- **OpenAI**: Removes certain string validations for models without structured outputs\r\n- **Anthropic**: Handles complex nested schemas with proper constraint descriptions\r\n- **Google**: Uses OpenAPI 3.0 schema format for better compatibility\r\n- **DeepSeek**: Converts advanced string patterns to descriptions\r\n- **Meta**: Optimizes array and union type handling\r\n\r\n## Testing\r\n\r\nThe package includes comprehensive tests covering all functionality:\r\n\r\n```bash\r\n# Run tests\r\npnpm test\r\n\r\n# Run tests in watch mode\r\npnpm test --watch\r\n```\r\n\r\n## License\r\n\r\nElastic-2.0\r\n","readmeFilename":"README.md","_rev":"1-bb4e240e95fbc6857bed7155362bfdd7"}