Prerequisites
First, create a Pydantic model and guard for the structured data. The model should describe the data structure to be returned. Pydantic Fields support Guardrails validators and are initialized at the same time as validators. We’ve included here a sample prompt and data asking an LLM to produce some structured data. Learn more about pydantic here.Options
Function/tool calling structured response formatting
For models that support openai tool/function calling(gpt-4o, gpt-4-turbo, or gpt-3.5-turbo).
Prompt Updates
For models that support JSON through prompt engineering and hinting (most models).Constrained decoding structured response formatting
For Hugging Face models structured JSON output maybe returned utilizing constrained decoding. Constrained decoding is a technique that allows you to guide the model to generate a specific type of output, a little bit like JSON ad-libs. Learn more about constrained decoding here.JSON Mode
For models that support JSON through an input argument(gpt-4o, gpt-4-turbo, or gpt-3.5-turbo)