langchain.output_parsers.retry.RetryOutputParser

Note

RetryOutputParser implements the standard Runnable Interface. 🏃

class langchain.output_parsers.retry.RetryOutputParser[source]

Bases: BaseOutputParser[T]

Wrap a parser and try to fix parsing errors.

Does this by passing the original prompt and the completion to another LLM, and telling it the completion did not satisfy criteria in the prompt.

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 legacy: bool = True

Whether to use the run or arun method of the retry_chain.

param max_retries: int = 1

The maximum number of times to retry the parse.

param parser: BaseOutputParser[T] [Required]

The parser to use to parse the output.

param retry_chain: Union[RunnableSerializable, Any] = None

The RunnableSerializable to use to retry the completion (Legacy: LLMChain).

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

async aparse_with_prompt(completion: str, prompt_value: PromptValue) T[source]

Parse the output of an LLM call using a wrapped parser.

Parameters
  • completion (str) – The chain completion to parse.

  • prompt_value (PromptValue) – The prompt to use to parse the completion.

Returns

The parsed completion.

Return type

T

classmethod from_llm(llm: BaseLanguageModel, parser: BaseOutputParser[T], prompt: BasePromptTemplate = PromptTemplate(input_variables=['completion', 'prompt'], template='Prompt:\n{prompt}\nCompletion:\n{completion}\n\nAbove, the Completion did not satisfy the constraints given in the Prompt.\nPlease try again:'), max_retries: int = 1) RetryOutputParser[T][source]

Create an RetryOutputParser from a language model and a parser.

Parameters
Returns

RetryOutputParser

Return type

RetryOutputParser[T]

get_format_instructions() str[source]

Instructions on how the LLM output should be formatted.

Return type

str

parse(completion: str) T[source]

Parse a single string model output into some structure.

Parameters
  • text – String output of a language model.

  • completion (str) –

Returns

Structured output.

Return type

T

parse_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

parse_with_prompt(completion: str, prompt_value: PromptValue) T[source]

Parse the output of an LLM call using a wrapped parser.

Parameters
  • completion (str) – The chain completion to parse.

  • prompt_value (PromptValue) – The prompt to use to parse the completion.

Returns

The parsed completion.

Return type

T

Examples using RetryOutputParser