schema must be a JSON Schema of 'type: "object"', got 'type: "string"
Description
I updated the follows dependencies today, but when I use OpenAI, it threw: Invalid schema for function 'calculator': schema must be a JSON Schema of 'type: "object"', got 'type: "string"'.
- ai: 4.3.19
- @ai-sdk/openai: 1.3.23
- @ai-sdk/react": 1.2.12
import { tool } from 'ai'
import { Parser } from 'expr-eval'
import { z } from 'zod'
export const calculator = tool({
description:
'Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.',
parameters: z.object({ expression: z.string() }),
execute: async ({ expression }) => {
try {
return Parser.evaluate(expression).toString()
} catch (e) {
return e instanceof Error
? e.message
: 'Failed to calculate your expression'
}
}
})
AI SDK Version
- ai: 4.3.19
- @ai-sdk/openai: 1.3.23
- @ai-sdk/react": 1.2.12
how did u solve it? im also getting the same error
how did u solve it? im also getting the same error
I was facing the same issue. The fix for me was to downgrade zod to v3
You can also use the beta v5 ai sdk to use zod 4 https://v5.ai-sdk.dev/docs/getting-started/nextjs-app-router
We're facing the same issue, unfortunately v5 doesn't work for us just yet as it's not yet supported by Mastra, and we can't downgrade to Zod v3 because whole app is already updated to v4.
Getting this fixed in v4 would be much appreciated.
try using zod/v3 for this from v4 package
I've tried that with no luck, similar error just... got 'type: "Null"
@YanceyOfficial which zod version are you using? ai sdk v4 is not compatible with zod v4
Yes, I upgraded zod to v4 and lead to this issue, I suggest official doc can clarify this.
To convert a Zod schema to JSON Schema, use the z.toJSONSchema() function.
`import * as z from "zod";
const schema = z.object({ name: z.string(), age: z.number(), });
z.toJSONSchema(schema) // => { // type: 'object', // properties: { name: { type: 'string' }, age: { type: 'number' } }, // required: [ 'name', 'age' ], // additionalProperties: false, // }`
troubleshooting docs added https://ai-sdk.dev/docs/troubleshooting/zod-v4-json-schema-type-error
troubleshooting docs added https://ai-sdk.dev/docs/troubleshooting/zod-v4-json-schema-type-error
This page is now gone. Is zod v3 still required for ai sdk 6 beta?
Identify issue type
Identified as Documentation
Confidence: High
Rationale
The discussion identifies an unsupported version combination rather than broken promised behavior, and the requested/resolved action is to document the Zod compatibility requirement and troubleshooting guidance.
Evidence
- The error occurs after upgrading to Zod v4 while using AI SDK v4.
- A maintainer clarified that AI SDK v4 is not compatible with Zod v4.
- The reporter requested that the incompatibility be clarified in official documentation.
- A maintainer responded by adding a troubleshooting document, though a later comment reports that the page is now unavailable.