Skip to content

Commit 45c1ea2

Browse files
authored
refactoring: introduce FlexibleSchema (#6818)
## Background A generalized schema type is needed for output schema support. ## Summary Replace ToolInputSchema with FlexibleSchema.
1 parent bfdca8d commit 45c1ea2

File tree

10 files changed

+30
-29
lines changed

10 files changed

+30
-29
lines changed

‎.changeset/calm-eels-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/provider-utils': patch
3+
---
4+
5+
refactoring: introduce FlexibleSchema

‎packages/ai/core/tool/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export type {
1414
Tool,
1515
ToolCallOptions,
1616
ToolExecuteFunction,
17-
ToolInputSchema,
1817
InferToolInput,
1918
InferToolOutput,
2019
} from '@ai-sdk/provider-utils';

‎packages/ai/core/tool/mcp/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from 'zod';
22
import { JSONObject } from '@ai-sdk/provider';
3-
import { Tool, ToolInputSchema } from '@ai-sdk/provider-utils';
3+
import { FlexibleSchema, Tool } from '@ai-sdk/provider-utils';
44

55
export const LATEST_PROTOCOL_VERSION = '2024-11-05';
66
export const SUPPORTED_PROTOCOL_VERSIONS = [
@@ -9,7 +9,7 @@ export const SUPPORTED_PROTOCOL_VERSIONS = [
99
];
1010

1111
export type ToolSchemas =
12-
| Record<string, { inputSchema: ToolInputSchema<JSONObject | unknown> }>
12+
| Record<string, { inputSchema: FlexibleSchema<JSONObject | unknown> }>
1313
| 'automatic'
1414
| undefined;
1515

@@ -21,14 +21,14 @@ type MappedTool<T extends Tool | JSONObject, OUTPUT extends any> =
2121
: never;
2222

2323
export type McpToolSet<TOOL_SCHEMAS extends ToolSchemas = 'automatic'> =
24-
TOOL_SCHEMAS extends Record<string, { inputSchema: ToolInputSchema<unknown> }>
24+
TOOL_SCHEMAS extends Record<string, { inputSchema: FlexibleSchema<unknown> }>
2525
? {
2626
[K in keyof TOOL_SCHEMAS]: MappedTool<TOOL_SCHEMAS[K], CallToolResult> &
2727
Required<
2828
Pick<MappedTool<TOOL_SCHEMAS[K], CallToolResult>, 'execute'>
2929
>;
3030
}
31-
: McpToolSet<Record<string, { inputSchema: ToolInputSchema<unknown> }>>;
31+
: McpToolSet<Record<string, { inputSchema: FlexibleSchema<unknown> }>>;
3232

3333
const ClientOrServerImplementationSchema = z
3434
.object({

‎packages/ai/core/tool/tool.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
Tool,
44
ToolCallOptions,
55
ToolExecuteFunction,
6-
ToolInputSchema,
6+
FlexibleSchema,
77
} from '@ai-sdk/provider-utils';
88
import { tool } from './tool';
99

@@ -26,7 +26,7 @@ describe('tool helper', () => {
2626
expectTypeOf(toolType.execute).toEqualTypeOf<undefined>();
2727
expectTypeOf(toolType.execute).not.toEqualTypeOf<Function>();
2828
expectTypeOf(toolType.inputSchema).toEqualTypeOf<
29-
ToolInputSchema<{ number: number }>
29+
FlexibleSchema<{ number: number }>
3030
>();
3131
});
3232

@@ -61,7 +61,7 @@ describe('tool helper', () => {
6161
>();
6262
expectTypeOf(toolType.execute).not.toEqualTypeOf<undefined>();
6363
expectTypeOf(toolType.inputSchema).toEqualTypeOf<
64-
ToolInputSchema<{ number: number }>
64+
FlexibleSchema<{ number: number }>
6565
>();
6666
});
6767
});

‎packages/provider-utils/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ export {
2222
export * from './remove-undefined-entries';
2323
export * from './resolve';
2424
export * from './response-handler';
25-
export { asSchema, jsonSchema, type InferSchema, type Schema } from './schema';
25+
export {
26+
asSchema,
27+
jsonSchema,
28+
type FlexibleSchema,
29+
type InferSchema,
30+
type Schema,
31+
} from './schema';
2632
export * from './uint8-utils';
2733
export * from './validate-types';
2834
export * from './validator';

‎packages/provider-utils/src/parse-json.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export async function parseJSON(options: {
2929
* @returns {Promise<T>} - The parsed object.
3030
*/
3131
export async function parseJSON<
32-
SCHEMA extends z4.$ZodType | z3.Schema | Validator,
33-
VALUE = InferSchema<SCHEMA>,
34-
>(options: { text: string; schema: SCHEMA }): Promise<VALUE>;
32+
VALIDATOR extends z4.$ZodType | z3.Schema | Validator,
33+
VALUE = InferSchema<VALIDATOR>,
34+
>(options: { text: string; schema: VALIDATOR }): Promise<VALUE>;
3535
export async function parseJSON<
3636
SCHEMA extends z4.$ZodType | z3.Schema | Validator,
3737
VALUE = InferSchema<SCHEMA>,

‎packages/provider-utils/src/provider-defined-client-tool-factory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Tool, ToolExecuteFunction, ToolInputSchema } from './types/tool';
1+
import { Tool, ToolExecuteFunction } from './types/tool';
2+
import { FlexibleSchema } from './schema';
23

34
export type ProviderDefinedClientToolFactory<INPUT, ARGS extends object> = <
45
OUTPUT,
@@ -20,7 +21,7 @@ export function createProviderDefinedClientToolFactory<
2021
inputSchema,
2122
}: {
2223
id: string;
23-
inputSchema: ToolInputSchema<INPUT>;
24+
inputSchema: FlexibleSchema<INPUT>;
2425
}): ProviderDefinedClientToolFactory<INPUT, ARGS> {
2526
return <OUTPUT>({
2627
execute,

‎packages/provider-utils/src/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export type Schema<OBJECT = unknown> = Validator<OBJECT> & {
2626
readonly jsonSchema: JSONSchema7;
2727
};
2828

29+
export type FlexibleSchema<T> = z4.$ZodType<T> | z3.Schema<T> | Schema<T>;
30+
2931
export type InferSchema<SCHEMA> = SCHEMA extends z3.Schema
3032
? z3.infer<SCHEMA>
3133
: SCHEMA extends z4.$ZodType

‎packages/provider-utils/src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export type {
1818
Tool,
1919
ToolCallOptions,
2020
ToolExecuteFunction,
21-
ToolInputSchema,
2221
InferToolInput,
2322
InferToolOutput,
2423
} from './tool';

‎packages/provider-utils/src/types/tool.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
import {
2-
JSONObject,
3-
JSONValue,
4-
LanguageModelV2ToolResultPart,
5-
} from '@ai-sdk/provider';
6-
import * as z3 from 'zod/v3';
7-
import * as z4 from 'zod/v4/core';
8-
import { Schema } from '../schema';
1+
import { JSONValue, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2+
import { FlexibleSchema } from '../schema';
93
import { ModelMessage } from './model-message';
104

11-
export type ToolInputSchema<T = JSONObject> =
12-
| z4.$ZodType<T>
13-
| z3.Schema<T>
14-
| Schema<T>;
15-
165
export interface ToolCallOptions {
176
/**
187
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
@@ -66,7 +55,7 @@ The schema of the input that the tool expects. The language model will use this
6655
It is also used to validate the output of the language model.
6756
Use descriptions to make the input understandable for the language model.
6857
*/
69-
inputSchema: ToolInputSchema<INPUT>;
58+
inputSchema: FlexibleSchema<INPUT>;
7059
}
7160
> &
7261
NeverOptional<

0 commit comments

Comments
 (0)