Structured output allows you to define a specific JSON schema that your AI responses must conform to, making it easier to integrate AI-generated content with your application logic.
Defining Response Schemas
You can define the response schema in your agent class using the$responseSchema property or add the structuredOutput method in your agent class for defining more complex schemas.
Schema Configuration Options
The schema follows the JSON Schema specification and supports all its features:Basic Types
string, number, boolean, array, object
Required Properties
Specify which fields must be present
Nested Structures
Complex objects and arrays
Property Descriptions
Guide the AI on what each field should contain
Using Structured Output
When structured output is defined, the agent’s response will be automatically formatted and returned as an array according to the schema:Runtime Schema Management
The schema can be accessed or modified at runtime.Access schema
The schema can be accessed using thestructuredOutput() method:
Modify or set schema
The schema can be modified using theresponseSchema() method:
Example Use Cases
User Profile Generation
User Profile Generation
Product Recommendations
Product Recommendations
Content Analysis
Content Analysis
Best Practices
Do make your schema as specific as possible
Do include descriptive property descriptions to guide the AI
Do set
additionalProperties to false when you want to restrict the output to only the defined propertiesrequired property for fields that must be present
