Skip to main content
core-ai generates structured outputs from a schema you provide. Pass a Standard Schema with JSON Schema support, such as a Zod 4.2+ schema, to get a validated object with full TypeScript type inference. Pass plain JSON Schema to get the parsed JSON and validate it yourself.

Generate Structured Objects

Use generateObject() to get validated JSON responses:

Stream Structured Objects

Stream JSON as it’s being generated:

Schemas

schema accepts two kinds of schemas. Tool parameters accept the same two kinds. core-ai converts a Standard Schema with its ~standard.jsonSchema.input() method (draft 2020-12), once per schema object. Because the input type is converted, transforms such as z.string().transform(Number) work, and object holds their output. A schema that cannot be converted, such as one containing z.date(), throws a ValidationError before any provider call. Call toJsonSchema() to see the JSON Schema core-ai sends.

Supported libraries

Libraries without Standard JSON Schema support, such as TypeBox, Yup, Superstruct, io-ts, zod/mini, and Zod before 4.2, work through plain JSON Schema. Pass the JSON Schema they produce, for example z.toJSONSchema(schema) for an older Zod version, and validate the result with the library yourself.

Plain JSON Schema

core-ai uses plain JSON Schema as is and never validates the output against it. object is the parsed JSON, typed unknown:
core-ai does not ship a JSON Schema validator. To have core-ai validate the output, wrap your validator, such as Ajv, in the Standard Schema interface (see JSON Schema tools for an example).

Object stream events

The streamObject() function returns an ObjectStream — an async iterable with .result and .events. It emits these event types:
The object event is emitted once, when the full object payload has been parsed and, for a Standard Schema, validated. object-delta events carry raw, unvalidated text. Use them for progressive UI updates while JSON is still streaming.

Complex Schema Examples

OpenAI and Anthropic structured outputs accept only closed objects, so they reject open objects such as z.record() or .catchall(). Model key/value data as an array of objects instead.

Schema Descriptions

Add descriptions to help the model understand your schema:

Type Inference

result.object is typed with the Standard Schema’s output type (InferSchemaOutput<TSchema>). For plain JSON Schema it is unknown.

Error Handling

generateObject() and streamObject() throw these structured output errors:
  • StructuredOutputValidationError when the output does not match a Standard Schema. issues lists the schema library’s messages as path.to.key: message. When the schema’s validator throws instead, such as a transform calling new URL(), its error is the cause.
  • StructuredOutputParseError when the output is not valid JSON.
  • StructuredOutputNoObjectGeneratedError when the model returns no object.
Plain JSON Schema output is never validated, so it raises only the parse and no-object errors.

Streaming with UI Updates

Update your UI as the object is built:

Configuration Options

Customize structured output generation:

Best Practices

Help the model understand what you want:
Reduce temperature for more predictable JSON structure:
Build previews from raw JSON chunks, then switch to the validated object when it arrives:
Add validation for constraints the schema does not express:

Next Steps

Chat Completion

Generate text responses without structure

Tool Calling

Combine structured outputs with tool calls

Streaming

Stream regular text responses

Standard Schema

Find schema libraries that implement Standard Schema