Skip to main content

What Is Structured Output?

Structured output lets you define exactly how you want your AI responses formatted. Instead of parsing free-form text, you get clean, predictable data structures that are ready to use in your application.

Type-Safe Responses

Define schemas and get responses that match your expected structure

No Parsing Required

Skip the regex and string parsing - data comes pre-formatted

Provider Validation

Providers like OpenAI enforce schema compliance at generation time

Works With Tools

Combine structured output with tool calls for powerful workflows

Quick Start

To enable structured output, override the getSchema() method in your agent:
app/Agents/MovieReviewAgent.php
When you run this agent, instead of free-form text you’ll get structured data:
Using the Structured Agent

Available Schema Types

Vizra ADK uses Prism PHP’s schema system. Here are all available types:

StringSchema

For text values of any length.

NumberSchema

For integers and floating-point numbers.

BooleanSchema

For true/false values.

EnumSchema

For values restricted to a specific set of options.

ArraySchema

For lists of items following a specific schema.

ObjectSchema

For complex nested structures. This should be your root schema.

AnyOfSchema

For flexible data that can match one of several schemas.
Provider Compatibility: AnyOfSchema works with OpenAI and Gemini, but is not supported by Anthropic. Design alternative schema patterns when targeting Anthropic.

Real-World Examples

Data Extraction Agent

Extract structured data from unstructured text:
app/Agents/ContactExtractorAgent.php

Sentiment Analysis Agent

Analyze text sentiment with detailed breakdown:
app/Agents/SentimentAnalysisAgent.php

Content Classification Agent

Classify content into categories with tags:
app/Agents/ContentClassifierAgent.php

Nullable Fields

Mark fields as nullable when they might not have a value:
Nullable Fields
OpenAI Strict Mode: When using OpenAI’s strict mode, all fields must be listed in requiredFields. Use nullable: true to indicate optional fields that can have null values.

Nested Schemas

For complex data structures, nest ObjectSchemas:
Nested Schemas

Combining With Tools

Structured output works seamlessly with tools. The agent can call tools to gather information, then return a structured response:
app/Agents/WeatherReportAgent.php
When using tools with structured output, set $maxSteps to at least 2. The agent needs multiple steps: one to call tools, and another to return the structured result.

Provider Considerations

Different providers have varying levels of structured output support:

OpenAI Strict Mode

Enable strict mode for tighter schema validation:
OpenAI Strict Mode

Anthropic Tool Calling Mode

For Anthropic, use tool calling mode for more reliable structured output:
Anthropic Tool Calling

Best Practices

Use ObjectSchema as Root

Always use ObjectSchema as your top-level schema. Providers like OpenAI require this in strict mode

Write Clear Descriptions

Descriptive field descriptions help the LLM understand what data to provide

Mark Optional Fields Nullable

Use nullable: true for fields that might not have values

Validate Responses

Even with structured output, validate the response in your application
Schema Limitations: While structured output provides schema enforcement at generation time, it doesn’t guarantee semantic correctness. Always validate that the data makes sense for your use case.

API Reference

Schema Types

Agent Method

ObjectSchema Parameters


Tools

Learn how to create tools for your agents

Agents

Understanding agent architecture