Skip to content

robust_output_parser

JSON output parser with robust error handling

Warning

This module was written to parse sloppy JSON emitted by the Mixtral-8/7xb family of models and is currently deprecated and not being actively maintained.

Attributes

FORMAT_INSTRUCTIONS module-attribute

FORMAT_INSTRUCTIONS = 'The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```. JSON must be parsable by Python\'s json.loads() method:\n\n```json\n{{\n{format}\n}}\n```'

Classes

RobustOutputParser

Bases: StructuredOutputParser

Enhancement to the StructuredOutputParser class that allows for more flexible parsing of JSON output from LLMs.

Attributes

default_on_error class-attribute instance-attribute
default_on_error: Optional[Dict] = None

Functions

from_response_schemas classmethod
from_response_schemas(
    response_schemas: List[ResponseSchema],
    default_on_error: Optional[Dict] = None,
) -> StructuredOutputParser
get_format_instructions
get_format_instructions(only_json: bool = False) -> str
parse
parse(text: str) -> Any

Parses the given text and returns the parsed output.

Parameters:

  • text (str) –

    The text to be parsed.

Returns:

  • Any ( Any ) –

    The parsed output.

Raises:

  • Exception

    If an error occurs during parsing and no default response is provided.

Functions

get_clean_json

get_clean_json(text: str) -> Dict[Any, Any]

Extracts a clean JSON string from the given text and returns it as a dictionary.

Parameters:

  • text (str) –

    The input text containing the JSON.

Returns:

  • dict ( Dict[Any, Any] ) –

    The extracted JSON as a dictionary.

Raises:

  • ValueError

    If the JSON cannot be extracted or parsed.