{"record":{"id":"c2db444cbe324125","repo":"vercel/ai","slug":"no-object-generated-response-did-not-match-schema-c2db44","errorCode":null,"errorMessage":"No object generated: response did not match schema.","messagePattern":"No object generated: response did not match schema\\.","errorType":"validation","errorClass":"NoObjectGeneratedError","httpStatus":null,"severity":"error","filePath":"packages/ai/src/generate-text/output.ts","lineNumber":149,"sourceCode":"\n      if (!parseResult.success) {\n        throw new NoObjectGeneratedError({\n          message: 'No object generated: could not parse the response.',\n          cause: parseResult.error,\n          text,\n          response: context.response,\n          usage: context.usage,\n          finishReason: context.finishReason,\n        });\n      }\n\n      const validationResult = await safeValidateTypes({\n        value: parseResult.value,\n        schema,\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,\n          response: context.response,\n          usage: context.usage,\n          finishReason: context.finishReason,\n        });\n      }\n\n      return validationResult.value;\n    },\n\n    async parsePartialOutput({ text }: { text: string }) {\n      const result = await parsePartialJson(text);\n\n      switch (result.state) {\n        case 'failed-parse':\n        case 'undefined-input': {","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/ai/src/generate-text/output.ts#L131-L167","documentation":"NoObjectGeneratedError thrown when the response text parsed as JSON but the resulting value failed validation against the requested zod/JSON schema. The validation error is attached as `cause` along with the raw text and response metadata.","triggerScenarios":"generateText with Output.object({ schema }) where the model returned syntactically valid JSON whose shape violates the schema (missing required fields, wrong types, extra constraints like min/max or enum values not satisfied).","commonSituations":"Overly strict schema (e.g. .min() constraints the model ignores); model omits optional-but-required-by-schema fields; schema changed after prompt was written; model returns strings where numbers expected.","solutions":["Read err.cause (the TypeValidationError/ZodError) to see which fields failed.","Loosen over-strict schema constraints (drop min/max/regex or make fields optional/nullish).","Include the JSON schema in the prompt or use native structured-output mode so the provider constrains generation.","Use Output.object with a schema the model can realistically satisfy; simplify nested structures.","Retry with a repair prompt including the failed text and validation errors."],"exampleFix":"// before\nschema = z4.object({ age: z4.number().min(0).max(120) });\n// after\nschema = z4.object({ age: z4.number().min(0).max(120).nullable().optional() });\n// and add prompt: 'Return JSON matching this schema: ...'","handlingStrategy":"validation","validationCode":"import { safeValidateTypes } from '@ai-sdk/provider-utils';\n// pre-check parsed text against schema before calling result.output\nconst parsed = JSON.parse(result.text);\nconst check = await safeValidateTypes({ value: parsed, schema });\nif (!check.success) console.error(check.error); // shows which fields the model got wrong","typeGuard":"import { NoObjectGeneratedError } from 'ai';\nfunction isSchemaMismatch(e): e is NoObjectGeneratedError {\n  return NoObjectGeneratedError.isInstance(e) &&\n    e.message.includes('did not match schema');\n}","tryCatchPattern":"try {\n  const obj = result.output;\n} catch (e) {\n  if (NoObjectGeneratedError.isInstance(e) && e.message.includes('did not match schema')) {\n    console.error('Validation failures:', e.cause);\n    // retry with repair prompt containing e.text and e.cause\n  } else throw e;\n}","preventionTips":["Keep schemas permissive: optional/nullish fields, avoid strict min/max the model can ignore.","Include the JSON schema in the prompt or use native structured output.","Read e.cause to pinpoint failing fields before changing the schema.","Test schemas against real model outputs before shipping."],"tags":["schema-validation","structured-output","zod"],"backgroundTag":"schema-validation-failed","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}