Structured outputs with JsonSchema class
Learn how to use JSON Schemas to generate structured outputs using LLMs.
JsonSchema is a powerful utility in the Polyglot library that enables developers to define structured data schemas for LLM interactions. This guide explains how to use JsonSchema to shape your LLM outputs and ensure consistent, typed responses from language models.
Quick Start
Here’s a simple example of how to use JsonSchema with Polyglot’s Inference API:
Why Use JsonSchema?
JsonSchema provides several benefits when working with LLMs:
- Type Safety: Ensure LLM outputs conform to your expected data structure
- Data Validation: Specify required fields and data types
- Structured Responses: Get consistent, well-formatted data instead of raw text
- Complex Nesting: Define deeply nested structures for sophisticated applications
- Better LLM Guidance: Help the LLM understand exactly what format you need
Available Types
String
For text values of any length:
Number & Integer
For numeric values:
Boolean
For true/false values:
Array
For lists of items:
Enum
For values from a specific set of options:
Object
For complex, nested data structures:
Working with Required and Nullable Fields
Required Fields
Required fields are specified at the object level using the requiredProperties
parameter:
Nullable Fields
Nullable fields are specified at the individual field level:
Understanding Required vs. Nullable
- Required: The field must be present in the data structure
- Nullable: The field can contain a null value
- A field can be both required and nullable (must be present, can be null)
- A field can be non-required and non-nullable (when present, cannot be null)
Common Patterns
Working with OpenAI and Other Providers
When working with OpenAI in strict mode, follow these guidelines:
Building Complex Schemas
For more complex data structures, you can nest schemas:
Fluent API for Schema Creation
JsonSchema supports method chaining for a more fluent API:
Available methods include:
withName(string $name)
withDescription(string $description)
withTitle(string $title)
withNullable(bool $nullable = true)
withMeta(array $meta = [])
withEnumValues(?array $enum = null)
withProperties(?array $properties = null)
withItemSchema(JsonSchema $itemSchema = null)
withRequiredProperties(?array $required = null)
withAdditionalProperties(bool $additionalProperties = false)
Accessing Schema Properties
JsonSchema provides various methods to access schema properties:
Converting Schemas to Arrays and Function Calls
JsonSchema can be converted to arrays and function calls:
Meta Fields
You can add custom meta fields to your schemas:
Meta fields will be transformed to include the x-
prefix when converted to arrays (e.g., x-min_length
).
Best Practices
- Clear Descriptions: Write clear, concise descriptions for each field.
-
Only Mark Required Fields: Only mark fields as required if they’re truly necessary.
-
Organize Nested Schemas: Keep your schemas organized when dealing with complex structures.
- Be Explicit About Requirements: Specify both the nullable status and required fields for clarity.
Full Example: Creating User Profile Schema
Advanced: Creating Function Calls
JsonSchema can be used to define function/tool parameters for LLMs: