For the complete documentation index, see llms.txt. This page is also available as Markdown.

Structured Output

Get type-safe, validated responses from AI using BoxLang classes, struct templates, or JSON schemas. Eliminate manual parsing with automatic data extraction.

Get type-safe, validated responses from AI providers by defining expected output schemas. The module automatically converts AI responses into properly typed objects, eliminating manual parsing and validation.

πŸ“‹ Table of Contents


Why Use Structured Output?

πŸ”„ Data Extraction Flow

spinner

Benefits:

  • Type Safety: Get validated objects with proper types, not generic structs

  • Automatic Validation: Schema constraints ensure correct data structure

  • Better Reliability: Reduces hallucinations by strictly constraining format

  • IDE Support: Full autocomplete and type checking with classes

  • No Manual Parsing: Direct access to typed properties and methods

Quick Start

πŸ“Š Class vs Struct Template

spinner

Recommendation: Use classes for production code, struct templates for quick prototypes.

Using a Class

Using a Struct Template

For quick prototyping without defining classes:

Extracting Arrays

Extract multiple items of the same type:

Multiple Schemas

Extract different entity types from the same text:

With Conversations

Use structured output in multi-turn conversations:

With Tools

Combine structured output with function calling:

Manual Population with aiPopulate()

Convert JSON or structs into typed objects without making AI calls:

Perfect for:

  • Testing with mock data

  • Using cached AI responses

  • Converting existing JSON to typed objects

  • Validating data structures

Comparison: Structured Output vs JSON Return Format

Feature
Structured Output
JSON Return Format

Type Safety

βœ… Full type safety

❌ Generic structs

Validation

βœ… Schema validation

⚠️ Manual validation

IDE Support

βœ… Autocomplete

❌ No type info

Reliability

βœ… Strict schema

⚠️ May return invalid JSON

Best For

Production code

Quick prototypes

Use Structured Output when:

  • Building production applications

  • Type safety is important

  • Working with complex nested data

  • Consistency is critical

Use JSON Return Format when:

  • Quick prototypes or scripts

  • Dynamic/unknown data structures

  • Flexibility is more important than type safety

Provider Support

All providers support structured output:

Provider
Support
Notes

OpenAI

βœ… Native

Best support with strict validation

Claude

βœ… JSON Mode

Excellent results

Gemini

βœ… JSON Mode

Good support

Ollama

βœ… JSON Mode

Model dependent

Others

βœ… JSON Mode

Schema-guided responses

Note: OpenAI provides native structured output with strict schema validation. Other providers use JSON mode with schema constraints, which provides excellent results.

Best Practices

1. Keep Schemas Simple

2. Use Descriptive Property Names

3. Handle Missing Data

4. Cache Pipelines with Structured Output

Error Handling

Next Steps

Last updated