Home / Class/ JsonOutputFunctionsParser Class — langchain Architecture

JsonOutputFunctionsParser Class — langchain Architecture

Architecture documentation for the JsonOutputFunctionsParser class in openai_functions.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9["JsonOutputFunctionsParser"]
  fb3554e0_291b_93d2_d325_51461432ed8a["ChatGeneration"]
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9 -->|extends| fb3554e0_291b_93d2_d325_51461432ed8a
  d083bb96_f642_0585_b68e_6b3fea430962["openai_functions.py"]
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9 -->|defined in| d083bb96_f642_0585_b68e_6b3fea430962
  c527f73f_798a_60a2_707a_18f2b7e87420["_type()"]
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9 -->|method| c527f73f_798a_60a2_707a_18f2b7e87420
  0d933ccf_bbc1_fe27_4bef_ba30eddf9afe["_diff()"]
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9 -->|method| 0d933ccf_bbc1_fe27_4bef_ba30eddf9afe
  1f105d8e_9ce4_fd69_f790_74e3e27af01a["parse_result()"]
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9 -->|method| 1f105d8e_9ce4_fd69_f790_74e3e27af01a
  cb4a9bc7_4e6d_36b3_89f5_e94c387f9c18["parse()"]
  a82d17ab_c7c5_fd70_58ed_c083cfd5e7d9 -->|method| cb4a9bc7_4e6d_36b3_89f5_e94c387f9c18

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/openai_functions.py lines 58–154

class JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):
    """Parse an output as the JSON object."""

    strict: bool = False
    """Whether to allow non-JSON-compliant strings.

    See: https://docs.python.org/3/library/json.html#encoders-and-decoders

    Useful when the parsed output may include unicode characters or new lines.
    """

    args_only: bool = True
    """Whether to only return the arguments to the function call."""

    @property
    def _type(self) -> str:
        return "json_functions"

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

    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.

        Returns:
            The parsed JSON object.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        if len(result) != 1:
            msg = f"Expected exactly one result, but got {len(result)}"
            raise OutputParserException(msg)
        generation = result[0]
        if not isinstance(generation, ChatGeneration):
            msg = "This output parser can only be used with a chat generation."
            raise OutputParserException(msg)
        message = generation.message
        try:
            function_call = message.additional_kwargs["function_call"]
        except KeyError as exc:
            if partial:
                return None
            msg = f"Could not parse function call: {exc}"
            raise OutputParserException(msg) from exc
        try:
            if partial:
                try:
                    if self.args_only:
                        return parse_partial_json(
                            function_call["arguments"], strict=self.strict
                        )
                    return {
                        **function_call,
                        "arguments": parse_partial_json(
                            function_call["arguments"], strict=self.strict
                        ),
                    }
                except json.JSONDecodeError:
                    return None
            elif self.args_only:
                try:
                    return json.loads(function_call["arguments"], strict=self.strict)
                except (json.JSONDecodeError, TypeError) as exc:
                    msg = f"Could not parse function call data: {exc}"
                    raise OutputParserException(msg) from exc
            else:
                try:
                    return {
                        **function_call,
                        "arguments": json.loads(
                            function_call["arguments"], strict=self.strict
                        ),
                    }
                except (json.JSONDecodeError, TypeError) as exc:
                    msg = f"Could not parse function call data: {exc}"

Domain

Extends

Frequently Asked Questions

What is the JsonOutputFunctionsParser class?
JsonOutputFunctionsParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/openai_functions.py.
Where is JsonOutputFunctionsParser defined?
JsonOutputFunctionsParser is defined in libs/core/langchain_core/output_parsers/openai_functions.py at line 58.
What does JsonOutputFunctionsParser extend?
JsonOutputFunctionsParser extends ChatGeneration.

Analyze Your Own Codebase

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

Try Supermodel Free