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

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.

Source

Thrown at packages/ai/src/generate-text/output.ts:149

      if (!parseResult.success) {
        throw new NoObjectGeneratedError({
          message: 'No object generated: could not parse the response.',
          cause: parseResult.error,
          text,
          response: context.response,
          usage: context.usage,
          finishReason: context.finishReason,
        });
      }

      const validationResult = await safeValidateTypes({
        value: parseResult.value,
        schema,
      });

      if (!validationResult.success) {
        throw new NoObjectGeneratedError({
          message: 'No object generated: response did not match schema.',
          cause: validationResult.error,
          text,
          response: context.response,
          usage: context.usage,
          finishReason: context.finishReason,
        });
      }

      return validationResult.value;
    },

    async parsePartialOutput({ text }: { text: string }) {
      const result = await parsePartialJson(text);

      switch (result.state) {
        case 'failed-parse':
        case 'undefined-input': {

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Read err.cause (the TypeValidationError/ZodError) to see which fields failed.
  2. Loosen over-strict schema constraints (drop min/max/regex or make fields optional/nullish).
  3. Include the JSON schema in the prompt or use native structured-output mode so the provider constrains generation.
  4. Use Output.object with a schema the model can realistically satisfy; simplify nested structures.
  5. Retry with a repair prompt including the failed text and validation errors.

Example fix

// before
schema = z4.object({ age: z4.number().min(0).max(120) });
// after
schema = z4.object({ age: z4.number().min(0).max(120).nullable().optional() });
// and add prompt: 'Return JSON matching this schema: ...'
Defensive patterns

Strategy: validation

Validate before calling

import { safeValidateTypes } from '@ai-sdk/provider-utils';
// pre-check parsed text against schema before calling result.output
const parsed = JSON.parse(result.text);
const check = await safeValidateTypes({ value: parsed, schema });
if (!check.success) console.error(check.error); // shows which fields the model got wrong

Type guard

import { NoObjectGeneratedError } from 'ai';
function isSchemaMismatch(e): e is NoObjectGeneratedError {
  return NoObjectGeneratedError.isInstance(e) &&
    e.message.includes('did not match schema');
}

Try / catch

try {
  const obj = result.output;
} catch (e) {
  if (NoObjectGeneratedError.isInstance(e) && e.message.includes('did not match schema')) {
    console.error('Validation failures:', e.cause);
    // retry with repair prompt containing e.text and e.cause
  } else throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

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


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/c2db444cbe324125. Report an issue: GitHub.