Home / Class/ ToolsOutputParser Class — langchain Architecture

ToolsOutputParser Class — langchain Architecture

Architecture documentation for the ToolsOutputParser class in output_parsers.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c0648688_ea16_89e6_d980_3faef9185745["ToolsOutputParser"]
  7e9c2bbb_e497_e7d1_8c0f_e01706224ee5["BaseGenerationOutputParser"]
  c0648688_ea16_89e6_d980_3faef9185745 -->|extends| 7e9c2bbb_e497_e7d1_8c0f_e01706224ee5
  2bdc4d81_5592_3e8d_1079_4eb50790f1d0["ChatGeneration"]
  c0648688_ea16_89e6_d980_3faef9185745 -->|extends| 2bdc4d81_5592_3e8d_1079_4eb50790f1d0
  3702dbed_edb1_9f35_9019_2f747b6a4112["output_parsers.py"]
  c0648688_ea16_89e6_d980_3faef9185745 -->|defined in| 3702dbed_edb1_9f35_9019_2f747b6a4112
  de78512b_344e_3561_9fd0_66bcf46d91d3["parse_result()"]
  c0648688_ea16_89e6_d980_3faef9185745 -->|method| de78512b_344e_3561_9fd0_66bcf46d91d3
  15e58607_fd55_2228_f86b_4cc17f42fa97["_pydantic_parse()"]
  c0648688_ea16_89e6_d980_3faef9185745 -->|method| 15e58607_fd55_2228_f86b_4cc17f42fa97

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/output_parsers.py lines 14–70

class ToolsOutputParser(BaseGenerationOutputParser):
    """Output parser for tool calls."""

    first_tool_only: bool = False
    """Whether to return only the first tool call."""
    args_only: bool = False
    """Whether to return only the arguments of the tool calls."""
    pydantic_schemas: list[type[BaseModel]] | None = None
    """Pydantic schemas to parse tool calls into."""

    model_config = ConfigDict(
        extra="forbid",
    )

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse a list of candidate model Generations into a specific format.

        Args:
            result: A list of `Generation` to be parsed. The Generations are assumed
                to be different candidate outputs for a single model input.
            partial: (Not used) Whether the result is a partial result. If `True`, the
                parser may return a partial result, which may not be complete or valid.

        Returns:
            Structured output.

        """
        if not result or not isinstance(result[0], ChatGeneration):
            return None if self.first_tool_only else []
        message = cast("AIMessage", result[0].message)
        tool_calls: list = [
            dict(tc) for tc in _extract_tool_calls_from_message(message)
        ]
        if isinstance(message.content, list):
            # Map tool call id to index
            id_to_index = {
                block["id"]: i
                for i, block in enumerate(message.content)
                if isinstance(block, dict) and block["type"] == "tool_use"
            }
            tool_calls = [{**tc, "index": id_to_index[tc["id"]]} for tc in tool_calls]
        if self.pydantic_schemas:
            tool_calls = [self._pydantic_parse(tc) for tc in tool_calls]
        elif self.args_only:
            tool_calls = [tc["args"] for tc in tool_calls]
        else:
            pass

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

    def _pydantic_parse(self, tool_call: dict) -> BaseModel:
        cls_ = {schema.__name__: schema for schema in self.pydantic_schemas or []}[
            tool_call["name"]
        ]
        return cls_(**tool_call["args"])

Frequently Asked Questions

What is the ToolsOutputParser class?
ToolsOutputParser is a class in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/output_parsers.py.
Where is ToolsOutputParser defined?
ToolsOutputParser is defined in libs/partners/anthropic/langchain_anthropic/output_parsers.py at line 14.
What does ToolsOutputParser extend?
ToolsOutputParser extends BaseGenerationOutputParser, ChatGeneration.

Analyze Your Own Codebase

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

Try Supermodel Free