Home / Function/ _create_tool_message() — langchain Function Reference

_create_tool_message() — langchain Function Reference

Architecture documentation for the _create_tool_message() function in tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0ff9c5f6_f001_3ae0_9867_f275f470ab7a["_create_tool_message()"]
  ed473e28_b1e7_7216_4f15_0d043bb4cb27["tools.py"]
  0ff9c5f6_f001_3ae0_9867_f275f470ab7a -->|defined in| ed473e28_b1e7_7216_4f15_0d043bb4cb27
  986c0fc8_1f78_cfda_ea76_c1e777e94a27["format_to_tool_messages()"]
  986c0fc8_1f78_cfda_ea76_c1e777e94a27 -->|calls| 0ff9c5f6_f001_3ae0_9867_f275f470ab7a
  style 0ff9c5f6_f001_3ae0_9867_f275f470ab7a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/format_scratchpad/tools.py lines 18–48

def _create_tool_message(
    agent_action: ToolAgentAction,
    observation: Any,
) -> ToolMessage:
    """Convert agent action and observation into a tool message.

    Args:
        agent_action: the tool invocation request from the agent.
        observation: the result of the tool invocation.

    Returns:
        ToolMessage that corresponds to the original tool invocation.

    Raises:
        ValueError: if the observation cannot be converted to a string.
    """
    if not isinstance(observation, str):
        try:
            content = json.dumps(observation, ensure_ascii=False)
        except TypeError:
            content = str(observation)
        except Exception:
            _logger.exception("Unexpected error converting observation to string.")
            content = str(observation)
    else:
        content = observation
    return ToolMessage(
        tool_call_id=agent_action.tool_call_id,
        content=content,
        additional_kwargs={"name": agent_action.tool},
    )

Subdomains

Frequently Asked Questions

What does _create_tool_message() do?
_create_tool_message() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/format_scratchpad/tools.py.
Where is _create_tool_message() defined?
_create_tool_message() is defined in libs/langchain/langchain_classic/agents/format_scratchpad/tools.py at line 18.
What calls _create_tool_message()?
_create_tool_message() is called by 1 function(s): format_to_tool_messages.

Analyze Your Own Codebase

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

Try Supermodel Free