Prerequisites
- Familiarity with Running Model Inference
- Basic understanding of JSON Schema
Why structured output matters
Without structured output, LLM responses are free-form text that requires parsing and error handling. With structured output:| Without Schema | With Schema |
|---|---|
| Free-form text response | Guaranteed JSON structure |
| Manual parsing required | Direct property access |
| Inconsistent formats | Consistent field names and types |
| Runtime type errors | TypeScript type safety |
Defining a schema
Theoutput_format_schema field accepts a JSON Schema object that defines your expected response structure:
Common schema patterns
Simple object with required fields
Arrays of items
Nested objects
Enum constraints
Array of typed objects
TypeScript integration
Define TypeScript interfaces that match your schema for type-safe access:Handling optional fields
Userequired array to specify which fields must be present:
Validating responses
While the model is constrained to the schema, you may want additional runtime validation:Best practices
| Practice | Description |
|---|---|
| Keep schemas focused | Define only the fields you need |
| Use enums for known values | Constrain strings to valid options |
| Set numeric bounds | Use minimum/maximum for numbers |
| Make fields required explicitly | Don’t rely on defaults |
| Match TypeScript interfaces | Keep schema and types in sync |
| Add descriptions | Help the model understand field purpose |
Adding descriptions for clarity
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| Missing required fields | Schema mismatch or unclear prompt | Verify required array, improve prompt |
| Wrong types | Schema not enforced | Check property types match expectations |
| Empty arrays | Model unsure what to include | Add minItems or clearer prompt |
| Enum violations | Value not in enum list | Verify enum covers all possibilities |

