vercel/ai · error · NoObjectGeneratedError
No object generated: response did not match schema.
Error message
No object generated: response did not match schema.
What it means
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.
Source
Thrown at packages/ai/src/generate-object/parse-and-validate-object-result.ts:53
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,
},
);
if (!validationResult.success) {
throw new NoObjectGeneratedError({
message: 'No object generated: response did not match schema.',
cause: validationResult.error,
text: result,
response: context.response,
usage: context.usage,
finishReason: context.finishReason,
});
}
return validationResult.value;
}
/**
* Parses and validates a result string by parsing it as JSON and validating against the output strategy.
* If the result cannot be parsed, it attempts to repair the result using the repairText function.
*
* @param result - The result string to parse and validate
* @param outputStrategy - The output strategy containing validation logicView on GitHub (pinned to 69428b1f8b)
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.
Example fix
// before
const schema = z.object({ age: z.number() }); // model returns "age": "42"
// after
const schema = z.object({ age: z.coerce.number().nullable() });
// plus enable structured outputs:
generateObject({ model: openai('gpt-4o', { structuredOutputs: true }), schema, prompt }); Defensive patterns
Strategy: validation
Validate before calling
// validate the expected shape before relying on output
const schema = z.object({ name: z.string(), age: z.number().nullable().optional() });
// test with: schema.safeParse(JSON.parse(sampleModelOutput)) Type guard
import { NoObjectGeneratedError } from 'ai';
function isSchemaMismatch(e: unknown): e is NoObjectGeneratedError {
return NoObjectGeneratedError.isInstance(e) && e.message.includes('did not match schema');
} Try / catch
try {
const { object } = await generateObject({ model, schema, prompt });
return object;
} catch (e) {
if (NoObjectGeneratedError.isInstance(e)) {
console.error('validation cause:', e.cause, 'text:', e.text);
return fallbackExtraction(e.text); // manual/looser parse
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- No object generated: could not parse the response.
- No object generated: could not parse the response.
- No object generated: response did not match schema.
- Tool "${toolName}" returned structuredContent that does not
- Harness 'claude-code' requires a JSON schema for structured
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/162656baa2b50129.
Report an issue: GitHub.