langchain_core.output_parsers.pydantic.PydanticOutputParser

Note

PydanticOutputParser implements the standard Runnable Interface. 🏃

class langchain_core.output_parsers.pydantic.PydanticOutputParser[source]

Bases: JsonOutputParser, Generic[TBaseModel]

Parse an output using a pydantic model.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param diff: bool = False

In streaming mode, whether to yield diffs between the previous and current parsed output, or just the current parsed output.

param pydantic_object: Type[TBaseModel] [Required]

The pydantic model to parse.

async aparse(text: str) T

Parse a single string model output into some structure.

Parameters

text (str) – String output of a language model.

Returns

Structured output.

Return type

T

async aparse_result(result: List[Generation], *, partial: bool = False) T

Parse a list of candidate model Generations into a specific format.

The return value is parsed from only the first Generation in the result, which

is assumed to be the highest-likelihood Generation.

Parameters
  • result (List[Generation]) – A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input.

  • partial (bool) –

Returns

Structured output.

Return type

T

get_format_instructions() str[source]

Instructions on how the LLM output should be formatted.

Return type

str

parse(text: str) TBaseModel[source]

Parse a single string model output into some structure.

Parameters

text (str) – String output of a language model.

Returns

Structured output.

Return type

TBaseModel

parse_result(result: List[Generation], *, partial: bool = False) TBaseModel[source]

Parse a list of candidate model Generations into a specific format.

The return value is parsed from only the first Generation in the result, which

is assumed to be the highest-likelihood Generation.

Parameters
  • result (List[Generation]) – A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input.

  • partial (bool) –

Returns

Structured output.

Return type

TBaseModel

parse_with_prompt(completion: str, prompt: PromptValue) Any

Parse the output of an LLM call with the input prompt for context.

The prompt is largely provided in the event the OutputParser wants to retry or fix the output in some way, and needs information from the prompt to do so.

Parameters
  • completion (str) – String output of a language model.

  • prompt (PromptValue) – Input PromptValue.

Returns

Structured output

Return type

Any

Examples using PydanticOutputParser