{"record":{"id":"2623891521ced003","repo":"vercel/ai","slug":"no-object-generated-could-not-parse-the-response","errorCode":null,"errorMessage":"No object generated: could not parse the response.","messagePattern":"No object generated: could not parse the response\\.","errorType":"exception","errorClass":"NoObjectGeneratedError","httpStatus":null,"severity":"error","filePath":"packages/ai/src/generate-object/parse-and-validate-object-result.ts","lineNumber":33,"sourceCode":" * @param result - The result string to parse and validate\n * @param outputStrategy - The output strategy containing validation logic\n * @param context - Additional context for error reporting\n * @returns The validated result\n * @throws NoObjectGeneratedError if parsing or validation fails\n */\nasync function parseAndValidateObjectResult<RESULT>(\n  result: string,\n  outputStrategy: OutputStrategy<any, RESULT, any>,\n  context: {\n    response: Omit<LanguageModelResponseMetadata, 'messages'>;\n    usage: LanguageModelUsage;\n    finishReason: FinishReason;\n  },\n): Promise<RESULT> {\n  const parseResult = await safeParseJSON({ text: result });\n\n  if (!parseResult.success) {\n    throw new NoObjectGeneratedError({\n      message: 'No object generated: could not parse the response.',\n      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","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/ai/src/generate-object/parse-and-validate-object-result.ts#L15-L51","documentation":"This NoObjectGeneratedError is thrown when the model's text output cannot be parsed as JSON at all (`safeParseJSON` fails). generateObject/streamObject expect the model to emit valid JSON matching the structured-output prompt/schema; prose, markdown fences, truncated output, or refusals all cause this error. The original parse error and raw text are attached as `cause` and `text`.","triggerScenarios":"Model returns non-JSON text (refusal, explanation, markdown code fences); output truncated by `maxOutputTokens`/`maxTokens`; in array/enum mode the model omits the required `elements`/`result` wrapper so the overall payload is invalid JSON; `output: 'no-schema'` where the prompt alone fails to elicit JSON.","commonSituations":"Models without native JSON/structured-output support; small token limits cutting off mid-JSON; temperature too high producing chatty preambles; using `generateObject` with `output: 'no-schema'` and a prompt that yields prose.","solutions":["Inspect `error.text` and `error.cause` to see the raw output and parse failure; adjust the prompt to demand raw JSON only.","Use a model/provider that supports native structured output or JSON mode (e.g. provider `structuredOutputs`/`response_format` options).","Increase `maxOutputTokens` if the JSON is truncated (check `finishReason` for 'length').","Lower temperature and add 'respond with JSON only' instructions; use `experimental_repairText` (parseAndValidateObjectResultWithRepair path) to auto-fix near-miss JSON.","If using array/enum mode, confirm the schema wrapping is being sent (don't override the mode's JSON schema)."],"exampleFix":"// before\ngenerateObject({ model: openai('gpt-4o'), schema, prompt: 'List 3 users', maxOutputTokens: 50 }); // truncated JSON\n\n// after\ngenerateObject({\n  model: openai('gpt-4o', { structuredOutputs: true }),\n  schema,\n  prompt: 'List 3 users. Respond with JSON only.',\n  maxOutputTokens: 1000,\n  experimental_repairText: FixableJSONRepair,\n});","handlingStrategy":"try-catch","validationCode":"// after the fact, inspect attached diagnostics:\n// error.text (raw model output), error.cause (parse error), error.finishReason ('length' => truncated)","typeGuard":"import { NoObjectGeneratedError } from 'ai';\nfunction isNoObjectGenerated(e: unknown): e is NoObjectGeneratedError {\n  return NoObjectGeneratedError.isInstance(e);\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('raw text:', e.text, 'cause:', e.cause, 'finishReason:', e.finishReason);\n    return retryWithJsonOnlyPrompt(); // or repair path\n  }\n  throw e;\n}","preventionTips":["Use providers/models with native structured output or JSON mode.","Add 'respond with JSON only, no markdown' to prompts.","Set maxOutputTokens high enough for the full JSON.","Use experimental_repairText for near-miss JSON.","Check error.finishReason === 'length' to detect truncation."],"tags":["json-parsing","llm-output","structured-output","no-object-generated"],"backgroundTag":"no-object-generated","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}