Home / File/ function.py — langchain Source File

function.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  e7574e5e_5760_2a55_9908_a679fe8dfedf["function.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  e7574e5e_5760_2a55_9908_a679fe8dfedf --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  e7574e5e_5760_2a55_9908_a679fe8dfedf --> 91721f45_4909_e489_8c1f_084f8bd87145
  a1369c93_b21f_2edb_d15c_ec3e09ac1e42["langchain_core.messages.base"]
  e7574e5e_5760_2a55_9908_a679fe8dfedf --> a1369c93_b21f_2edb_d15c_ec3e09ac1e42
  053c6d65_7a74_9819_7c2a_c7357c95d2b8["langchain_core.utils._merge"]
  e7574e5e_5760_2a55_9908_a679fe8dfedf --> 053c6d65_7a74_9819_7c2a_c7357c95d2b8
  style e7574e5e_5760_2a55_9908_a679fe8dfedf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Function Message."""

from typing import Any, Literal

from typing_extensions import override

from langchain_core.messages.base import (
    BaseMessage,
    BaseMessageChunk,
    merge_content,
)
from langchain_core.utils._merge import merge_dicts


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

    `FunctionMessage` are an older version of the `ToolMessage` schema, and
    do not contain the `tool_call_id` field.

    The `tool_call_id` field 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.

    """

    name: str
    """The name of the function that was executed."""

    type: Literal["function"] = "function"
    """The type of the message (used for serialization)."""


class FunctionMessageChunk(FunctionMessage, BaseMessageChunk):
    """Function Message chunk."""

    # Ignoring mypy re-assignment here since we're overriding the value
    # to make sure that the chunk variant can be discriminated from the
    # non-chunk variant.
    type: Literal["FunctionMessageChunk"] = "FunctionMessageChunk"  # type: ignore[assignment]
    """The type of the message (used for serialization)."""

    @override
    def __add__(self, other: Any) -> BaseMessageChunk:  # type: ignore[override]
        if isinstance(other, FunctionMessageChunk):
            if self.name != other.name:
                msg = "Cannot concatenate FunctionMessageChunks with different names."
                raise ValueError(msg)

            return self.__class__(
                name=self.name,
                content=merge_content(self.content, other.content),
                additional_kwargs=merge_dicts(
                    self.additional_kwargs, other.additional_kwargs
                ),
                response_metadata=merge_dicts(
                    self.response_metadata, other.response_metadata
                ),
                id=self.id,
            )

        return super().__add__(other)

Subdomains

Dependencies

  • langchain_core.messages.base
  • langchain_core.utils._merge
  • typing
  • typing_extensions

Frequently Asked Questions

What does function.py do?
function.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What does function.py depend on?
function.py imports 4 module(s): langchain_core.messages.base, langchain_core.utils._merge, typing, typing_extensions.
Where is function.py in the architecture?
function.py is located at libs/core/langchain_core/messages/function.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