Home / File/ tool.py — langchain Source File

tool.py — langchain Source File

Architecture documentation for tool.py, a python file in the langchain codebase. 9 imports, 0 dependents.

File python CoreAbstractions MessageSchema 9 imports 6 functions 5 classes

Entity Profile

Dependency Diagram

graph LR
  210f83c5_4be3_e20e_b877_98a194178520["tool.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  8dfa0cac_d802_3ccd_f710_43a5e70da3a5["uuid"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 8dfa0cac_d802_3ccd_f710_43a5e70da3a5
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 91721f45_4909_e489_8c1f_084f8bd87145
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  210f83c5_4be3_e20e_b877_98a194178520 --> d758344f_537f_649e_f467_b9d7442e86df
  a1369c93_b21f_2edb_d15c_ec3e09ac1e42["langchain_core.messages.base"]
  210f83c5_4be3_e20e_b877_98a194178520 --> a1369c93_b21f_2edb_d15c_ec3e09ac1e42
  67d9204c_49ef_a623_53ad_d78199dd66bc["langchain_core.messages.content"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 67d9204c_49ef_a623_53ad_d78199dd66bc
  053c6d65_7a74_9819_7c2a_c7357c95d2b8["langchain_core.utils._merge"]
  210f83c5_4be3_e20e_b877_98a194178520 --> 053c6d65_7a74_9819_7c2a_c7357c95d2b8
  style 210f83c5_4be3_e20e_b877_98a194178520 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Messages for tools."""

import json
from typing import Any, Literal, cast, overload
from uuid import UUID

from pydantic import Field, model_validator
from typing_extensions import NotRequired, TypedDict, override

from langchain_core.messages import content as types
from langchain_core.messages.base import BaseMessage, BaseMessageChunk, merge_content
from langchain_core.messages.content import InvalidToolCall
from langchain_core.utils._merge import merge_dicts, merge_obj


class ToolOutputMixin:
    """Mixin for objects that tools can return directly.

    If a custom BaseTool is invoked with a `ToolCall` and the output of custom code is
    not an instance of `ToolOutputMixin`, the output will automatically be coerced to
    a string and wrapped in a `ToolMessage`.

    """


class ToolMessage(BaseMessage, ToolOutputMixin):
    """Message for passing the result of executing a tool back to a model.

    `ToolMessage` objects contain the result of a tool invocation. Typically, the result
    is encoded inside the `content` field.

    `tool_call_id` is used to associate the tool call request with the tool call
    response. Useful in situations where a chat model is able to request multiple tool
    calls in parallel.

    Example:
        A `ToolMessage` representing a result of `42` from a tool call with id

        ```python
        from langchain_core.messages import ToolMessage

        ToolMessage(content="42", tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL")
        ```

    Example:
        A `ToolMessage` where only part of the tool output is sent to the model
        and the full output is passed in to artifact.

        ```python
        from langchain_core.messages import ToolMessage

        tool_output = {
            "stdout": "From the graph we can see that the correlation between "
            "x and y is ...",
            "stderr": None,
            "artifacts": {"type": "image", "base64_data": "/9j/4gIcSU..."},
        }

        ToolMessage(
            content=tool_output["stdout"],
// ... (357 more lines)

Subdomains

Dependencies

  • json
  • langchain_core.messages
  • langchain_core.messages.base
  • langchain_core.messages.content
  • langchain_core.utils._merge
  • pydantic
  • typing
  • typing_extensions
  • uuid

Frequently Asked Questions

What does tool.py do?
tool.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in tool.py?
tool.py defines 6 function(s): _merge_status, default_tool_chunk_parser, default_tool_parser, invalid_tool_call, tool_call, tool_call_chunk.
What does tool.py depend on?
tool.py imports 9 module(s): json, langchain_core.messages, langchain_core.messages.base, langchain_core.messages.content, langchain_core.utils._merge, pydantic, typing, typing_extensions, and 1 more.
Where is tool.py in the architecture?
tool.py is located at libs/core/langchain_core/messages/tool.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core/messages).

Analyze Your Own Codebase

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

Try Supermodel Free