Skip to content

Commit 7ea8600

Browse files
committed
fix: automatic zod schema creation for ai sdk provider tools
1 parent 53ee6f8 commit 7ea8600

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ai-sdk-provider/index.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import type {
1919
AxChatResponse,
2020
AxChatResponseResult,
2121
AxFunction,
22-
AxFunctionJSONSchema,
2322
AxGenIn,
2423
AxGenOut
2524
} from '@ax-llm/ax/index.js';
25+
import { z } from 'zod';
26+
27+
import { convertToZodSchema } from './util.js';
2628

2729
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
2830
type AxChatRequestChatPrompt = Writeable<AxChatRequest['chatPrompt'][0]>;
@@ -33,15 +35,9 @@ type AxConfig = {
3335

3436
type generateFunction<T> = ((input: T) => Promise<unknown>) | undefined;
3537

36-
const schemaSymbol = Symbol('vercel.ai.schema');
37-
3838
interface RenderTool<IN> {
3939
description?: string;
40-
parameters?: {
41-
[schemaSymbol]: true;
42-
validate: undefined;
43-
jsonSchema: AxFunctionJSONSchema;
44-
};
40+
parameters?: z.ZodTypeAny;
4541
generate?: generateFunction<IN>;
4642
}
4743

@@ -72,11 +68,7 @@ export class AxAgentProvider<IN extends AxGenIn, OUT extends AxGenOut>
7268
properties: {}
7369
};
7470

75-
return {
76-
[schemaSymbol]: true as const,
77-
validate: undefined,
78-
jsonSchema
79-
};
71+
return convertToZodSchema(jsonSchema);
8072
}
8173

8274
get generate(): generateFunction<IN> {

src/ai-sdk-provider/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"dependencies": {
2626
"@ai-sdk/provider-utils": "^1.0.2",
27-
"@ax-llm/ax": "9.0.39"
27+
"@ax-llm/ax": "9.0.39",
28+
"zod": "^3.23.8"
2829
},
2930
"devDependencies": {
3031
"npm-run-all": "^4.1.5",

src/ai-sdk-provider/util.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { AxFunctionJSONSchema } from '@ax-llm/ax';
2+
import { z } from 'zod';
3+
4+
type AnyZod =
5+
| z.AnyZodObject
6+
| z.ZodString
7+
| z.ZodNumber
8+
| z.ZodBoolean
9+
| z.ZodArray<AnyZod>
10+
| z.ZodOptional<AnyZod>;
11+
12+
export function convertToZodSchema(
13+
jsonSchema: Readonly<AxFunctionJSONSchema>
14+
): AnyZod {
15+
const { type, properties, required, items } = jsonSchema;
16+
17+
switch (type) {
18+
case 'string':
19+
return z.string();
20+
case 'number':
21+
return z.number();
22+
case 'boolean':
23+
return z.boolean();
24+
case 'array':
25+
if (!items) {
26+
throw new Error("Array type must have 'items' property.");
27+
}
28+
return z.array(convertToZodSchema(items));
29+
case 'object': {
30+
if (!properties) {
31+
throw new Error("Object type must have 'properties' property.");
32+
}
33+
const shape: Record<string, AnyZod> = {};
34+
35+
for (const [key, value] of Object.entries(properties)) {
36+
const schema = convertToZodSchema(value);
37+
shape[key] = required?.includes(key) ? schema : schema.optional();
38+
}
39+
return z.object(shape);
40+
}
41+
default:
42+
throw new Error(`Unsupported type: ${type}`);
43+
}
44+
}

0 commit comments

Comments
 (0)