{"slug": "json-schema-doesn-t-prevent-ai-hallucinations-and-that-s-okay", "title": "JSON Schema Doesn't Prevent AI Hallucinations (And That's Okay)", "summary": "A developer building ShapeCraft, an open-source structured output library, argues that JSON Schema validation prevents malformed responses but does not guarantee factual accuracy, as demonstrated by an invoice extraction example where the model returns a structurally valid but incorrect value. The developer distinguishes between structural correctness and information trustworthiness, calling for a second verification layer in production AI systems.", "body_md": "A few months ago, if you had asked me whether Structured Outputs solved hallucinations, I probably would have said yes.\n\nToday, I wouldn't.\n\nNot because Structured Outputs don't work-they absolutely do.\n\nBut because they solve a **different problem**.\n\nAfter building ShapeCraft, an open-source structured output library supporting OpenAI, Groq, Ollama (GBNF), and Anthropic, I realized something that fundamentally changed how I think about production AI.\n\n**Structure and correctness are two completely different guarantees.**\n\nLet's say you're extracting data from an invoice.\n\nThe invoice says:\n\n```\nInvoice Total: $900\n```\n\nYour model returns:\n\n```\n{\n  \"invoiceTotal\": 1200\n}\n```\n\nLet's validate it.\n\n| Validation | Result |\n|---|---|\n| Valid JSON | ✅ |\n| Matches JSON Schema | ✅ |\n| Required field present | ✅ |\n| Correct data type | ✅ |\n| Correct answer | ❌ |\n\nThe response is structurally perfect.\n\nThe extracted value is still wrong.\n\nJSON Schema didn't fail.\n\nIt did exactly what it was designed to do.\n\nJSON Schema is responsible for ensuring your application receives data in a predictable format.\n\nIt guarantees things like:\n\nThat makes applications dramatically easier to build.\n\nBut it does **not** guarantee:\n\nThose are different problems.\n\nI've started thinking about AI validation as two independent layers.\n\nCan my application safely consume this response?\n\nExamples:\n\nJSON Schema solves this extremely well.\n\nCan I trust the information?\n\nQuestions become:\n\nThis layer is much harder.\n\nAnd I think it's where the next generation of AI tooling needs to evolve.\n\nAnother interesting thing I learned while building ShapeCraft is that \"structured output\" isn't implemented the same way everywhere.\n\n| Provider | Implementation | Guarantee |\n|---|---|---|\n| OpenAI | Server-side schema enforcement | Native |\n| Groq | Server-side schema enforcement | Native |\n| Ollama | Grammar-constrained decoding (GBNF) | Constrained |\n| Anthropic | Prompt + parsing + retries | Best-effort |\n\nEvery provider can return valid structured output.\n\nBut they don't achieve it the same way.\n\nUnderstanding those differences helps you make better architectural decisions.\n\nOne design decision I made was to expose the guarantee level directly instead of hiding it.\n\n``` js\nconst result = await generate(model, schema, prompt);\n\nconsole.log(result.guaranteeLevel);\n\n// native\n// constrained\n// best-effort\n```\n\nThis doesn't tell you whether the answer is correct.\n\nIt tells you **how confident you can be that the response structurally matches the schema.**\n\nI think that's an important distinction.\n\nStructured output solved one of the biggest problems in LLM development.\n\nBefore it, we spent hours fixing malformed JSON.\n\nToday, that problem is largely solved.\n\nThe next challenge isn't generating valid JSON.\n\nIt's generating information we can trust.\n\nImagine every response including:\n\nThat's where I believe production AI is heading.\n\nI no longer think of AI validation as a single step.\n\nIt's two completely different layers.\n\nFirst:\n\nIs the response structurally correct?\n\nThen:\n\nIs the information actually correct?\n\nThose are different questions.\n\nAnd understanding that distinction has changed how I design AI systems.\n\nI'm curious how you're handling this in production.\n\nDo you rely solely on schema validation, or are you adding another verification layer after the model responds?\n\nIf you're interested in structured generation across multiple providers, ShapeCraft is open source.\n\nIt supports:\n\nWith support for:\n\nGitHub:\n\n[https://github.com/aviasoletechnologies/shapecraft](https://github.com/aviasoletechnologies/shapecraft)\n\n```\n# npm\nnpm install @aviasole/shapecraft\n\n# pnpm\npnpm add @aviasole/shapecraft\n```\n\nProvider SDKs:\n\n```\nnpm install openai\nnpm install groq-sdk\nnpm install @anthropic-ai/sdk\n\n# Ollama requires no additional SDK\njs\nimport { generate, openai } from \"@aviasole/shapecraft\";\nimport { z } from \"zod\";\n\nconst schema = z.object({\n  name: z.string(),\n  age: z.number(),\n});\n\nconst result = await generate(\n  openai({ model: \"gpt-4.1-mini\" }),\n  schema,\n  \"Generate a fictional user.\"\n);\n\nconsole.log(result.data);\nconsole.log(result.guaranteeLevel);\n```\n\n", "url": "https://wpnews.pro/news/json-schema-doesn-t-prevent-ai-hallucinations-and-that-s-okay", "canonical_source": "https://dev.to/imhardikmehta/json-schema-doesnt-prevent-ai-hallucinations-and-thats-okay-4db7", "published_at": "2026-07-24 15:35:00+00:00", "updated_at": "2026-07-24 16:03:28.519074+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-tools", "developer-tools"], "entities": ["ShapeCraft", "OpenAI", "Groq", "Ollama", "Anthropic", "Aviasole Technologies"], "alternates": {"html": "https://wpnews.pro/news/json-schema-doesn-t-prevent-ai-hallucinations-and-that-s-okay", "markdown": "https://wpnews.pro/news/json-schema-doesn-t-prevent-ai-hallucinations-and-that-s-okay.md", "text": "https://wpnews.pro/news/json-schema-doesn-t-prevent-ai-hallucinations-and-that-s-okay.txt", "jsonld": "https://wpnews.pro/news/json-schema-doesn-t-prevent-ai-hallucinations-and-that-s-okay.jsonld"}}