ai icon indicating copy to clipboard operation
ai copied to clipboard

schema must be a JSON Schema of 'type: "object"', got 'type: "string"

Open YanceyOfficial opened this issue 1 year ago • 12 comments

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

YanceyOfficial avatar Jul 16 '25 05:07 YanceyOfficial

how did u solve it? im also getting the same error

imranbarbhuiya avatar Jul 16 '25 16:07 imranbarbhuiya

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

sandiiarov avatar Jul 16 '25 22:07 sandiiarov

You can also use the beta v5 ai sdk to use zod 4 https://v5.ai-sdk.dev/docs/getting-started/nextjs-app-router

SeanCox avatar Jul 17 '25 20:07 SeanCox

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.

jakubriedl avatar Jul 19 '25 11:07 jakubriedl

try using zod/v3 for this from v4 package

imranbarbhuiya avatar Jul 19 '25 18:07 imranbarbhuiya

I've tried that with no luck, similar error just... got 'type: "Null"

jakubriedl avatar Jul 20 '25 10:07 jakubriedl

@YanceyOfficial which zod version are you using? ai sdk v4 is not compatible with zod v4

lgrammel avatar Jul 24 '25 06:07 lgrammel

Yes, I upgraded zod to v4 and lead to this issue, I suggest official doc can clarify this.

YanceyOfficial avatar Jul 24 '25 07:07 YanceyOfficial

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, // }`

GGHansome avatar Jul 24 '25 08:07 GGHansome

troubleshooting docs added https://ai-sdk.dev/docs/troubleshooting/zod-v4-json-schema-type-error

nicoalbanese avatar Jul 24 '25 11:07 nicoalbanese

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?

danielo515 avatar Dec 10 '25 06:12 danielo515

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.

lgrammel avatar Jul 22 '26 14:07 lgrammel