Home / Function/ test_tool_runtime_error_handling() — langchain Function Reference

test_tool_runtime_error_handling() — langchain Function Reference

Architecture documentation for the test_tool_runtime_error_handling() function in test_injected_runtime_create_agent.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  985f94bf_88c1_cfb1_8593_5474c42dbf1c["test_tool_runtime_error_handling()"]
  a4d83fe9_0a39_5761_b1a5_de54f81974c9["test_injected_runtime_create_agent.py"]
  985f94bf_88c1_cfb1_8593_5474c42dbf1c -->|defined in| a4d83fe9_0a39_5761_b1a5_de54f81974c9
  style 985f94bf_88c1_cfb1_8593_5474c42dbf1c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/test_injected_runtime_create_agent.py lines 414–461

def test_tool_runtime_error_handling() -> None:
    """Test error handling with ToolRuntime injection."""

    @tool
    def error_tool(x: int, runtime: ToolRuntime) -> str:
        """Tool that may error."""
        # Access runtime to ensure it's injected even during errors
        _ = runtime.tool_call_id
        if x == 0:
            msg = "Cannot process zero"
            raise ValueError(msg)
        return f"Processed: {x}"

    # create_agent uses default error handling which doesn't catch ValueError
    # So we need to handle this differently
    @tool
    def safe_tool(x: int, runtime: ToolRuntime) -> str:
        """Tool that handles errors safely."""
        try:
            if x == 0:
                return "Error: Cannot process zero"
        except Exception as e:
            return f"Error: {e}"
        return f"Processed: {x}"

    agent = create_agent(
        model=FakeToolCallingModel(
            tool_calls=[
                [{"args": {"x": 0}, "id": "error_call", "name": "safe_tool"}],
                [{"args": {"x": 5}, "id": "success_call", "name": "safe_tool"}],
                [],
            ]
        ),
        tools=[safe_tool],
        system_prompt="You are a helpful assistant.",
    )

    result = agent.invoke({"messages": [HumanMessage("Test error handling")]})

    # Both tool calls should complete
    tool_messages = [msg for msg in result["messages"] if isinstance(msg, ToolMessage)]
    assert len(tool_messages) == 2

    # First call returned error message
    assert "Error:" in tool_messages[0].content or "Cannot process zero" in tool_messages[0].content

    # Second call succeeded
    assert "Processed: 5" in tool_messages[1].content

Domain

Subdomains

Frequently Asked Questions

What does test_tool_runtime_error_handling() do?
test_tool_runtime_error_handling() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/test_injected_runtime_create_agent.py.
Where is test_tool_runtime_error_handling() defined?
test_tool_runtime_error_handling() is defined in libs/langchain_v1/tests/unit_tests/agents/test_injected_runtime_create_agent.py at line 414.

Analyze Your Own Codebase

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

Try Supermodel Free