@mastra/schema-compat
Version:
Tool schema compatibility layer for Mastra.ai
74 lines • 2.61 kB
TypeScript
/**
* Standard Schema compatibility utilities
*
* This module provides utilities to apply provider-specific schema transformations
* to StandardSchemaWithJSON types, particularly for OpenAI compatibility.
*/
import type { StandardSchemaWithJSON } from './standard-schema/standard-schema.types.js';
import type { ModelInformation } from './types.js';
/**
* Extracts the underlying Zod schema from a schema value.
*
* Handles both:
* - Raw Zod schemas (z.object(...))
* - StandardSchemaWithJSON wrappers that preserve Zod methods via prototype
*
* @param schema - The schema to extract from
* @returns The underlying Zod schema, or null if not a Zod schema
*/
export declare function extractZodSchema(schema: unknown): unknown;
/**
* Applies OpenAI schema compatibility transforms to a schema.
*
* For OpenAI models, converts `.optional()` fields to `.nullable().transform()`
* to work around OpenAI's limitation with optional fields in structured outputs.
*
* @param schema - The schema to process (raw Zod or StandardSchemaWithJSON)
* @param modelInfo - Information about the target model
* @returns The processed schema as StandardSchemaWithJSON, or original if not applicable
*
* @example
* ```typescript
* const schema = z.object({
* name: z.string(),
* age: z.number().optional(), // Will be converted to .nullable().transform()
* });
*
* const processed = applyOpenAICompatTransforms(schema, {
* provider: 'openai',
* modelId: 'gpt-4',
* supportsStructuredOutputs: false,
* });
* ```
*/
export declare function applyOpenAICompatTransforms<T>(schema: T, modelInfo: ModelInformation): T | StandardSchemaWithJSON;
/**
* Applies OpenAI compat transforms to all tools in a toolset.
*
* Processes each tool's inputSchema (if present) with OpenAI compatibility transforms.
*
* @param tools - The toolset to process
* @param modelInfo - Information about the target model
* @returns The processed toolset with transformed schemas
*
* @example
* ```typescript
* const tools = {
* getTodo: {
* inputSchema: z.object({
* id: z.string(),
* includeCompleted: z.boolean().optional(), // Will be transformed
* }),
* execute: async (input) => { ... },
* },
* };
*
* const processed = applyOpenAICompatToTools(tools, {
* provider: 'openai',
* modelId: 'gpt-4',
* supportsStructuredOutputs: false,
* });
* ```
*/
export declare function applyOpenAICompatToTools<T extends Record<string, any>>(tools: T | undefined, modelInfo: ModelInformation): T | undefined;
//# sourceMappingURL=standard-schema-compat.d.ts.map