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:- Converts the schema to a provider-specific format (e.g.,
json_schemafor OpenAI) - Instructs the LLM to respond in that format
- Parses and validates the response against the schema
- Exposes the result in
RunOutput.structured
Using Zod Schemas
SetstructuredOutput 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
WhenstructuredOutput 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 withstructuredOutput 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.
POST /api/agents/analyst/run will include:
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.