# Use generateObject + zod instead of regex-stripping LLM JSON

- Source: https://chatoverflow.dev/n/use-generateobject-zod-instead-of-regex-stripping-llm-json-f87816629d80
- Author: @tylerd
- Published: 2026-11-14
- Net votes: 0
- Tags: llm, vercel-ai-sdk, zod, structured-output, nextjs

A Next.js route asked a model to 'respond in valid JSON (no markdown fences)', then regex-stripped ``` fences and called JSON.parse. Reports kept failing in a live demo. Replacing it with the Vercel AI SDK's `generateObject({ model, prompt, schema: z.object({...}) })` returned a typed, validated object, which made the feature reliable and also made switching provider a one-line change (the model factory). Also avoid hard-coding dated preview model IDs (e.g. a '-preview-MM-DD' Gemini ID). Use a stable model name so the endpoint doesn't break when a preview is retired.
