vercel/ai · error · InvalidArgumentError

Invalid argument for parameter schema: Element schema is req

Error message

Invalid argument for parameter schema: Element schema is required for array output.

What it means

With output: 'array', generateObject/streamObject generate an array of elements, and a schema describing the ELEMENT type is required. The library throws InvalidArgumentError when schema is missing because it cannot construct the array-of-schema request.

Source

Thrown at packages/ai/src/generate-object/validate-object-generation-input.ts:85

      throw new InvalidArgumentError({
        parameter: 'schema',
        value: schema,
        message: 'Schema is required for object output.',
      });
    }

    if (enumValues != null) {
      throw new InvalidArgumentError({
        parameter: 'enumValues',
        value: enumValues,
        message: 'Enum values are not supported for object output.',
      });
    }
  }

  if (output === 'array') {
    if (schema == null) {
      throw new InvalidArgumentError({
        parameter: 'schema',
        value: schema,
        message: 'Element schema is required for array output.',
      });
    }

    if (enumValues != null) {
      throw new InvalidArgumentError({
        parameter: 'enumValues',
        value: enumValues,
        message: 'Enum values are not supported for array output.',
      });
    }
  }

  if (output === 'enum') {
    if (schema != null) {
      throw new InvalidArgumentError({

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Provide a schema for the array element type, e.g. schema: z.object({ name: z.string() }) with output: 'array'.
  2. Do not pass an array schema (z.array(...)); the library wraps the element schema itself.
  3. If a structured array is not what you want, change output mode accordingly.

Example fix

// before
generateObject({ model, output: 'array', prompt });
// after
generateObject({ model, output: 'array', schema: z.object({ name: z.string() }), prompt });
Defensive patterns

Strategy: validation

Validate before calling

if (opts.output === 'array' && opts.schema == null) {
  throw new Error("an element schema is required when output is 'array'");
}

Type guard

function hasElementSchema(opts) {
  return opts.output !== 'array' || opts.schema != null;
}

Try / catch

try {
  return await generateObject(opts);
} catch (error) {
  if (InvalidArgumentError.isInstance(error) && error.parameter === 'schema' && opts.output === 'array') {
    throw new Error('array output needs an element schema');
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling generateObject({ model, output: 'array', prompt }) or streamObject with output: 'array' but schema undefined/null; the schema must describe the element, not the array itself.

Common situations: Forgetting to switch the schema when converting a single-object call to array output; dynamically built options where the schema lookup failed; plain JS callers without type checking.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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