{"record":{"id":"162656baa2b50129","repo":"vercel/ai","slug":"no-object-generated-response-did-not-match-schema","errorCode":null,"errorMessage":"No object generated: response did not match schema.","messagePattern":"No object generated: response did not match schema\\.","errorType":"exception","errorClass":"NoObjectGeneratedError","httpStatus":null,"severity":"error","filePath":"packages/ai/src/generate-object/parse-and-validate-object-result.ts","lineNumber":53,"sourceCode":"      cause: parseResult.error,\n      text: result,\n      response: context.response,\n      usage: context.usage,\n      finishReason: context.finishReason,\n    });\n  }\n\n  const validationResult = await outputStrategy.validateFinalResult(\n    parseResult.value,\n    {\n      text: result,\n      response: context.response,\n      usage: context.usage,\n    },\n  );\n\n  if (!validationResult.success) {\n    throw new NoObjectGeneratedError({\n      message: 'No object generated: response did not match schema.',\n      cause: validationResult.error,\n      text: result,\n      response: context.response,\n      usage: context.usage,\n      finishReason: context.finishReason,\n    });\n  }\n\n  return validationResult.value;\n}\n\n/**\n * Parses and validates a result string by parsing it as JSON and validating against the output strategy.\n * If the result cannot be parsed, it attempts to repair the result using the repairText function.\n *\n * @param result - The result string to parse and validate\n * @param outputStrategy - The output strategy containing validation logic","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/ai/src/generate-object/parse-and-validate-object-result.ts#L35-L71","documentation":"This NoObjectGeneratedError is thrown when the model produced parseable JSON but the value failed schema (or type) validation. In array mode each element is validated against the element schema; in enum mode the `result` string must be one of the enum values; in object mode the JSON must satisfy the zod/JSON schema. The validation error, raw text, response metadata, usage, and finishReason are attached to the error.","triggerScenarios":"Model returns JSON whose shape/types don't match the schema (missing required fields, wrong types, extra mismatched content); an array element fails element-schema validation on the final delta; enum output yields a string outside `enumValues`; empty/`undefined` final value in no-schema mode.","commonSituations":"Overly strict schemas (strict `.optional()`/nullable mismatches); model hallucinating extra or differently-named fields; enum values too semantically similar so the model picks a near-miss; provider not enforcing structured output so the JSON is freeform.","solutions":["Read `error.cause` (validation details) and `error.text` to see exactly which fields failed; loosen or correct the schema accordingly.","Enable native structured outputs on the provider so the model is constrained to the schema.","Improve the prompt: describe the expected shape explicitly and give a JSON example.","Make schema fields `.nullable()`/`.optional()` where the model legitimately omits them; use `z.enum` with distinct values for enum mode.","Retry with `maxRetries` or add `experimental_repairText` for near-miss outputs."],"exampleFix":"// before\nconst schema = z.object({ age: z.number() }); // model returns \"age\": \"42\"\n\n// after\nconst schema = z.object({ age: z.coerce.number().nullable() });\n// plus enable structured outputs:\ngenerateObject({ model: openai('gpt-4o', { structuredOutputs: true }), schema, prompt });","handlingStrategy":"validation","validationCode":"// validate the expected shape before relying on output\nconst schema = z.object({ name: z.string(), age: z.number().nullable().optional() });\n// test with: schema.safeParse(JSON.parse(sampleModelOutput))","typeGuard":"import { NoObjectGeneratedError } from 'ai';\nfunction isSchemaMismatch(e: unknown): e is NoObjectGeneratedError {\n  return NoObjectGeneratedError.isInstance(e) && e.message.includes('did not match schema');\n}","tryCatchPattern":"try {\n  const { object } = await generateObject({ model, schema, prompt });\n  return object;\n} catch (e) {\n  if (NoObjectGeneratedError.isInstance(e)) {\n    console.error('validation cause:', e.cause, 'text:', e.text);\n    return fallbackExtraction(e.text); // manual/looser parse\n  }\n  throw e;\n}","preventionTips":["Enable native structured outputs so the model is schema-constrained.","Mark genuinely optional fields .optional()/.nullable() in the schema.","Include a concrete JSON example in the prompt.","For enum output, keep enum values distinct and describe each in the prompt.","Log error.cause + error.text to iterate on schema/prompt quickly."],"tags":["schema-validation","structured-output","llm-output","no-object-generated"],"backgroundTag":"schema-validation-failed","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}