Home / Class/ JsonOutputToolsParser Class — langchain Architecture

JsonOutputToolsParser Class — langchain Architecture

Architecture documentation for the JsonOutputToolsParser class in openai_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  866f9561_d4a8_d272_8901_6d748e78c2d8["JsonOutputToolsParser"]
  fb3554e0_291b_93d2_d325_51461432ed8a["ChatGeneration"]
  866f9561_d4a8_d272_8901_6d748e78c2d8 -->|extends| fb3554e0_291b_93d2_d325_51461432ed8a
  fcfa55b0_4a86_fa31_a156_3c38c76a0a9b["AIMessage"]
  866f9561_d4a8_d272_8901_6d748e78c2d8 -->|extends| fcfa55b0_4a86_fa31_a156_3c38c76a0a9b
  7d2ea6eb_ed7a_3052_c920_cb5cdc943964["openai_tools.py"]
  866f9561_d4a8_d272_8901_6d748e78c2d8 -->|defined in| 7d2ea6eb_ed7a_3052_c920_cb5cdc943964
  50407279_dc51_1a35_d5ad_3f6ad6318238["parse_result()"]
  866f9561_d4a8_d272_8901_6d748e78c2d8 -->|method| 50407279_dc51_1a35_d5ad_3f6ad6318238
  f7065362_41be_755f_d85d_3ea02b9016de["parse()"]
  866f9561_d4a8_d272_8901_6d748e78c2d8 -->|method| f7065362_41be_755f_d85d_3ea02b9016de

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/openai_tools.py lines 140–222

class JsonOutputToolsParser(BaseCumulativeTransformOutputParser[Any]):
    """Parse tools from OpenAI response."""

    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.
    """

    return_id: bool = False
    """Whether to return the tool call id."""

    first_tool_only: bool = False
    """Whether to return only the first tool call.

    If `False`, the result will be a list of tool calls, or an empty list if no tool
    calls are found.

    If `True`, and multiple tool calls are found, only the first one will be returned,
    and the other tool calls will be ignored.

    If no tool calls are found, `None` will be returned.
    """

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

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

                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 tool calls.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        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
        if isinstance(message, AIMessage) and message.tool_calls:
            tool_calls = [dict(tc) for tc in message.tool_calls]
            for tool_call in tool_calls:
                if not self.return_id:
                    _ = tool_call.pop("id")
        else:
            try:
                raw_tool_calls = copy.deepcopy(message.additional_kwargs["tool_calls"])
            except KeyError:
                return []
            tool_calls = parse_tool_calls(
                raw_tool_calls,
                partial=partial,
                strict=self.strict,
                return_id=self.return_id,
            )
        # for backwards compatibility
        for tc in tool_calls:
            tc["type"] = tc.pop("name")

        if self.first_tool_only:
            return tool_calls[0] if tool_calls else None
        return tool_calls

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

        Args:
            text: The output of the LLM call.

        Returns:
            The parsed tool calls.

Domain

Frequently Asked Questions

What is the JsonOutputToolsParser class?
JsonOutputToolsParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/openai_tools.py.
Where is JsonOutputToolsParser defined?
JsonOutputToolsParser is defined in libs/core/langchain_core/output_parsers/openai_tools.py at line 140.
What does JsonOutputToolsParser extend?
JsonOutputToolsParser extends ChatGeneration, AIMessage.

Analyze Your Own Codebase

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

Try Supermodel Free