Home / Function/ wrap_tool_call() — langchain Function Reference

wrap_tool_call() — langchain Function Reference

Architecture documentation for the wrap_tool_call() function in tool_emulator.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  4044e8b7_30b9_dc69_5caa_63ae699529ab["wrap_tool_call()"]
  a4266996_914c_18fd_7063_10ef49e72ec1["LLMToolEmulator"]
  4044e8b7_30b9_dc69_5caa_63ae699529ab -->|defined in| a4266996_914c_18fd_7063_10ef49e72ec1
  style 4044e8b7_30b9_dc69_5caa_63ae699529ab fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/tool_emulator.py lines 109–157

    def wrap_tool_call(
        self,
        request: ToolCallRequest,
        handler: Callable[[ToolCallRequest], ToolMessage | Command[Any]],
    ) -> ToolMessage | Command[Any]:
        """Emulate tool execution using LLM if tool should be emulated.

        Args:
            request: Tool call request to potentially emulate.
            handler: Callback to execute the tool (can be called multiple times).

        Returns:
            ToolMessage with emulated response if tool should be emulated,
                otherwise calls handler for normal execution.
        """
        tool_name = request.tool_call["name"]

        # Check if this tool should be emulated
        should_emulate = self.emulate_all or tool_name in self.tools_to_emulate

        if not should_emulate:
            # Let it execute normally by calling the handler
            return handler(request)

        # Extract tool information for emulation
        tool_args = request.tool_call["args"]
        tool_description = request.tool.description if request.tool else "No description available"

        # Build prompt for emulator LLM
        prompt = (
            f"You are emulating a tool call for testing purposes.\n\n"
            f"Tool: {tool_name}\n"
            f"Description: {tool_description}\n"
            f"Arguments: {tool_args}\n\n"
            f"Generate a realistic response that this tool would return "
            f"given these arguments.\n"
            f"Return ONLY the tool's output, no explanation or preamble. "
            f"Introduce variation into your responses."
        )

        # Get emulated response from LLM
        response = self.model.invoke([HumanMessage(prompt)])

        # Short-circuit: return emulated result without executing real tool
        return ToolMessage(
            content=response.content,
            tool_call_id=request.tool_call["id"],
            name=tool_name,
        )

Domain

Subdomains

Frequently Asked Questions

What does wrap_tool_call() do?
wrap_tool_call() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/tool_emulator.py.
Where is wrap_tool_call() defined?
wrap_tool_call() is defined in libs/langchain_v1/langchain/agents/middleware/tool_emulator.py at line 109.

Analyze Your Own Codebase

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

Try Supermodel Free