AI SDK - Am I going insane?

Literally cannot get it to work all day, no matter whether I use the vercel agent otr the gemini agent, every single respnse comes back with an error

Even a simple app with a text box and a generate button, and the EXACT example from the documnetation produces an error.

import { generateText } from "ai"

import { openai } from "@ai-sdk/openai"




export async function POST(req: Request) {

  try {

    const { prompt } = await req.json()




    if (!prompt) {

      return Response.json({ error: "Prompt is required" }, { status: 400 })

    }




    const { text } = await generateText({

      model: openai("gpt-5"),

      prompt: prompt,

    })




    return Response.json({ text })

  } catch (error) {

    console.error("Error generating text:", error)

    return Response.json({ error: "Internal Server Error" }, { status: 500 })

  }

}

I have the same OpenAI api key in place in other places, and so that’s not the issue - What on earth is going on with this?

Your try/catch is wrapping the entire function so anything that errors inside it is going to trigger the same error. In this case it’s not the AI Generation that is failing

"Invalid JSON Response" is thrown when you try to use either Response.json() or request.json() on a body that is not JSON. Internally it’s failing on the JSON.parse/stringify steps.

Try const body = await req.text() at the top of the handler to see what actual input body you’re getting

1 Like