Home / Function/ coerce_args() — langchain Function Reference

coerce_args() — langchain Function Reference

Architecture documentation for the coerce_args() function in tool.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5a6d4844_9d19_db15_a029_88df4c2b1cc5["coerce_args()"]
  511a8f91_080f_9750_8245_6c31ee0b22eb["ToolMessage"]
  5a6d4844_9d19_db15_a029_88df4c2b1cc5 -->|defined in| 511a8f91_080f_9750_8245_6c31ee0b22eb
  style 5a6d4844_9d19_db15_a029_88df4c2b1cc5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/tool.py lines 92–133

    def coerce_args(cls, values: dict) -> dict:
        """Coerce the model arguments to the correct types.

        Args:
            values: The model arguments.

        """
        content = values["content"]
        if isinstance(content, tuple):
            content = list(content)

        if not isinstance(content, (str, list)):
            try:
                values["content"] = str(content)
            except ValueError as e:
                msg = (
                    "ToolMessage content should be a string or a list of string/dicts. "
                    f"Received:\n\n{content=}\n\n which could not be coerced into a "
                    "string."
                )
                raise ValueError(msg) from e
        elif isinstance(content, list):
            values["content"] = []
            for i, x in enumerate(content):
                if not isinstance(x, (str, dict)):
                    try:
                        values["content"].append(str(x))
                    except ValueError as e:
                        msg = (
                            "ToolMessage content should be a string or a list of "
                            "string/dicts. Received a list but "
                            f"element ToolMessage.content[{i}] is not a dict and could "
                            f"not be coerced to a string.:\n\n{x}"
                        )
                        raise ValueError(msg) from e
                else:
                    values["content"].append(x)

        tool_call_id = values["tool_call_id"]
        if isinstance(tool_call_id, (UUID, int, float)):
            values["tool_call_id"] = str(tool_call_id)
        return values

Domain

Subdomains

Frequently Asked Questions

What does coerce_args() do?
coerce_args() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/tool.py.
Where is coerce_args() defined?
coerce_args() is defined in libs/core/langchain_core/messages/tool.py at line 92.

Analyze Your Own Codebase

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

Try Supermodel Free