Implementation:Run llama Llama index PydanticOutputParser
Overview
The PydanticOutputParser module provides a structured output parser that converts LLM text output into validated Pydantic model instances. It extracts JSON from LLM responses, validates it against a Pydantic schema, and returns a typed model object. This module is located at llama-index-core/llama_index/core/output_parsers/pydantic.py (67 lines).
Purpose
This module enables type-safe structured output from LLMs. By appending JSON schema instructions to prompts and parsing the LLM's JSON response into a Pydantic model, it ensures that LLM output conforms to a predefined data contract. This is essential for applications requiring reliable, programmatically consumable output from LLM calls.
Constants
| Name | Description |
|---|---|
PYDANTIC_FORMAT_TMPL |
A format template string instructing the LLM to output valid JSON conforming to a given schema. Contains a {schema} placeholder that is filled with the Pydantic model's JSON schema.
|
The template reads:
Here's a JSON schema to follow:
{schema}
Output a valid JSON object but do not repeat the schema.
Key Components
Class: PydanticOutputParser
A generic output parser (BaseOutputParser, Generic[Model]) that validates LLM output against a Pydantic model class.
Constructor
def __init__(
self,
output_cls: Type[Model],
excluded_schema_keys_from_format: Optional[List] = None,
pydantic_format_tmpl: str = PYDANTIC_FORMAT_TMPL,
) -> None
| Parameter | Type | Description |
|---|---|---|
output_cls |
Type[Model] |
The target Pydantic model class that LLM output should conform to. |
excluded_schema_keys_from_format |
Optional[List] |
Schema keys to exclude from the format instructions shown to the LLM. Defaults to empty list. |
pydantic_format_tmpl |
str |
The template string for formatting schema instructions. Defaults to PYDANTIC_FORMAT_TMPL.
|
Properties
| Property | Return Type | Description |
|---|---|---|
output_cls |
Type[Model] |
Returns the target Pydantic output class. |
format_string |
str |
Returns the format instruction string with escaped JSON curly braces (for safe use in prompt templates). |
Methods
| Method | Parameters | Return Type | Description |
|---|---|---|---|
get_format_string |
escape_json: bool = True |
str |
Generates the format instruction string. Retrieves the JSON schema from the Pydantic model, removes excluded keys, serializes to JSON, and fills the template. If escape_json is True, curly braces are doubled for safe use in format strings.
|
parse |
text: str |
Any |
Extracts a JSON string from the LLM output using extract_json_str(), then validates it against the Pydantic model using model_validate_json(). Returns a validated model instance.
|
format |
query: str |
str |
Appends the format instructions to the query string, separated by two newlines. |
Processing Pipeline
The module operates in two phases:
Prompt Formatting Phase:
format()is called with the user's query.- The Pydantic model's JSON schema is generated via
model_json_schema(). - Excluded keys are removed from the schema dictionary.
- The schema is serialized to a JSON string and inserted into the format template.
- Curly braces are escaped (
{{ }}) for safe use in prompt templates. - The formatted instructions are appended to the query.
Parsing Phase:
parse()receives the raw LLM text output.extract_json_str()extracts the JSON portion from the text.model_validate_json()validates and deserializes the JSON into a Pydantic model instance.
Dependencies
| Module | Items Imported |
|---|---|
json |
JSON serialization for the schema string. |
llama_index.core.output_parsers |
BaseOutputParser base class.
|
llama_index.core.output_parsers.utils |
extract_json_str utility for extracting JSON from LLM output.
|
llama_index.core.types |
Model type variable for generic Pydantic models.
|
Design Notes
- The parser uses Pydantic v2 APIs (
model_json_schema(),model_validate_json()) for schema generation and validation. - The
excluded_schema_keys_from_formatparameter allows hiding internal or complex schema fields from the LLM prompt to reduce confusion while still validating the full schema on output. - Curly brace escaping is critical because format strings with literal braces would conflict with Python's
str.format()or prompt template systems. - The parser is intentionally simple: it does not include retry logic or error correction. If the LLM produces invalid JSON or a non-conforming structure, the Pydantic validation will raise an exception.