Skip to main content

Structured Output

Structured output lets you enforce a JSON schema on the LLM’s response. Instead of free-form text, the agent returns validated, typed data—ideal for sentiment analysis, entity extraction, form filling, and API integrations.

What is Structured Output?

With structured output, you define a Zod schema that describes the expected shape of the response. Agentium:
  1. Converts the schema to a provider-specific format (e.g., json_schema for OpenAI)
  2. Instructs the LLM to respond in that format
  3. Parses and validates the response against the schema
  4. Exposes the result in RunOutput.structured

Using Zod Schemas

Set structuredOutput in AgentConfig:

How It Works

1

Schema

Define a Zod schema (object, array, nested, etc.).
2

responseFormat

Agentium converts the schema to the provider’s responseFormat (e.g., { type: "json_schema", schema: {...} }).
3

LLM

The model is instructed to return valid JSON matching the schema.
4

Parse & Validate

The raw response is parsed and validated with schema.safeParse(). Invalid output may throw or be handled by the provider.

RunOutput.structured

When structuredOutput is set, the parsed and validated result is available on RunOutput.structured:

Example: Sentiment Analysis Agent


Example: City Info with Rich Schema


Structured Output in Express / Swagger

When you expose an agent with structuredOutput via Express, Agentium automatically generates a typed response schema in the OpenAPI spec. Swagger UI will display the exact shape of the structured field — consumers of your API see the full contract without extra documentation.
The generated OpenAPI spec for POST /api/agents/analyst/run will include:
Agents without structuredOutput use the generic RunOutput schema where structured is untyped.

Provider Support

Agentium abstracts provider differences. Use a Zod schema and the framework handles the rest.