Home / Class/ JsonOutputParser Class — langchain Architecture

JsonOutputParser Class — langchain Architecture

Architecture documentation for the JsonOutputParser class in json.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0d9a7db1_a024_a63e_d6da_4108a6cd2019["JsonOutputParser"]
  73622474_3901_c3da_bce7_e7f2cbdfa6c6["json.py"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|defined in| 73622474_3901_c3da_bce7_e7f2cbdfa6c6
  b7cf60cd_0ad0_3996_3192_df2ded613c7b["_diff()"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|method| b7cf60cd_0ad0_3996_3192_df2ded613c7b
  03884eb2_86e4_9795_d1e4_b84eaa05761e["_get_schema()"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|method| 03884eb2_86e4_9795_d1e4_b84eaa05761e
  06efbece_d7af_13f0_6b96_e81512502f42["parse_result()"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|method| 06efbece_d7af_13f0_6b96_e81512502f42
  7035eca2_a525_23ad_aece_91541acfef4a["parse()"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|method| 7035eca2_a525_23ad_aece_91541acfef4a
  26f39076_94a6_44ef_5396_69f52b286a72["get_format_instructions()"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|method| 26f39076_94a6_44ef_5396_69f52b286a72
  5bfa6954_3564_31d3_d88d_132df690df2a["_type()"]
  0d9a7db1_a024_a63e_d6da_4108a6cd2019 -->|method| 5bfa6954_3564_31d3_d88d_132df690df2a

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/json.py lines 31–127

class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]):
    """Parse the output of an LLM call to a JSON object.

    Probably the most reliable output parser for getting structured data that does *not*
    use function calling.

    When used in streaming mode, it will yield partial JSON objects containing all the
    keys that have been returned so far.

    In streaming, if `diff` is set to `True`, yields `JSONPatch` operations describing
    the difference between the previous and the current object.
    """

    pydantic_object: Annotated[type[TBaseModel] | None, SkipValidation()] = None  # type: ignore[valid-type]
    """The Pydantic object to use for validation.

    If `None`, no validation is performed.
    """

    @override
    def _diff(self, prev: Any | None, next: Any) -> Any:
        return jsonpatch.make_patch(prev, next).patch

    @staticmethod
    def _get_schema(pydantic_object: type[TBaseModel]) -> dict[str, Any]:
        if issubclass(pydantic_object, pydantic.BaseModel):
            return pydantic_object.model_json_schema()
        return pydantic_object.schema()

    @override
    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse the result of an LLM call to a JSON object.

        Args:
            result: The result of the LLM call.
            partial: Whether to parse partial JSON objects.

                If `True`, the output will be a JSON object containing all the keys that
                have been returned so far.

                If `False`, the output will be the full JSON object.

        Returns:
            The parsed JSON object.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        text = result[0].text
        text = text.strip()
        if partial:
            try:
                return parse_json_markdown(text)
            except JSONDecodeError:
                return None
        else:
            try:
                return parse_json_markdown(text)
            except JSONDecodeError as e:
                msg = f"Invalid json output: {text}"
                raise OutputParserException(msg, llm_output=text) from e

    def parse(self, text: str) -> Any:
        """Parse the output of an LLM call to a JSON object.

        Args:
            text: The output of the LLM call.

        Returns:
            The parsed JSON object.
        """
        return self.parse_result([Generation(text=text)])

    def get_format_instructions(self) -> str:
        """Return the format instructions for the JSON output.

        Returns:
            The format instructions for the JSON output.
        """
        if self.pydantic_object is None:
            return "Return a JSON object."

Domain

Frequently Asked Questions

What is the JsonOutputParser class?
JsonOutputParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/json.py.
Where is JsonOutputParser defined?
JsonOutputParser is defined in libs/core/langchain_core/output_parsers/json.py at line 31.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free