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.
Pay attention to the
required
, additionalProperties
, and strict
properties. It’s recommended by OpenAI to set these when defining schemas to get exactly the structure you need.For complex schemas, it’s recommended to use the
structuredOutput()
method instead of the property, as it provides more flexibility and can include dynamic logic.Using trait with
structuredOutput()
method is a good practice to avoid code duplication and make your code more maintainable.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
In most of the cases you need to include all properties in
required
, skip only properties which are optional in ANY case.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