vercel/ai · error · NoObjectGeneratedError
No object generated: could not parse the response.
Error message
No object generated: could not parse the response.
What it means
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`.
Source
Thrown at packages/ai/src/generate-object/parse-and-validate-object-result.ts:33
* @param result - The result string to parse and validate
* @param outputStrategy - The output strategy containing validation logic
* @param context - Additional context for error reporting
* @returns The validated result
* @throws NoObjectGeneratedError if parsing or validation fails
*/
async function parseAndValidateObjectResult<RESULT>(
result: string,
outputStrategy: OutputStrategy<any, RESULT, any>,
context: {
response: Omit<LanguageModelResponseMetadata, 'messages'>;
usage: LanguageModelUsage;
finishReason: FinishReason;
},
): Promise<RESULT> {
const parseResult = await safeParseJSON({ text: result });
if (!parseResult.success) {
throw new NoObjectGeneratedError({
message: 'No object generated: could not parse the response.',
cause: parseResult.error,
text: result,
response: context.response,
usage: context.usage,
finishReason: context.finishReason,
});
}
const validationResult = await outputStrategy.validateFinalResult(
parseResult.value,
{
text: result,
response: context.response,
usage: context.usage,
},
);
View on GitHub (pinned to 69428b1f8b)
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).
Example fix
// before
generateObject({ model: openai('gpt-4o'), schema, prompt: 'List 3 users', maxOutputTokens: 50 }); // truncated JSON
// after
generateObject({
model: openai('gpt-4o', { structuredOutputs: true }),
schema,
prompt: 'List 3 users. Respond with JSON only.',
maxOutputTokens: 1000,
experimental_repairText: FixableJSONRepair,
}); Defensive patterns
Strategy: try-catch
Validate before calling
// after the fact, inspect attached diagnostics:
// error.text (raw model output), error.cause (parse error), error.finishReason ('length' => truncated) Type guard
import { NoObjectGeneratedError } from 'ai';
function isNoObjectGenerated(e: unknown): e is NoObjectGeneratedError {
return NoObjectGeneratedError.isInstance(e);
} Try / catch
try {
const { object } = await generateObject({ model, schema, prompt });
return object;
} catch (e) {
if (NoObjectGeneratedError.isInstance(e)) {
console.error('raw text:', e.text, 'cause:', e.cause, 'finishReason:', e.finishReason);
return retryWithJsonOnlyPrompt(); // or repair path
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- No object generated: response did not match schema.
- No object generated: could not parse the response.
- No object generated: response did not match schema.
- Harness 'claude-code' requires a JSON schema for structured
- Harness 'cline' requires a JSON schema for structured output
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/2623891521ced003.
Report an issue: GitHub.