Home / Function/ test_tool_runtime_with_custom_state() — langchain Function Reference

test_tool_runtime_with_custom_state() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/test_injected_runtime_create_agent.py lines 285–327

def test_tool_runtime_with_custom_state() -> None:
    """Test ToolRuntime works with custom state schemas."""

    class CustomState(AgentState[Any]):
        custom_field: str

    runtime_state = {}

    @tool
    def custom_state_tool(x: int, runtime: ToolRuntime) -> str:
        """Tool that accesses custom state."""
        runtime_state["custom_field"] = runtime.state.get("custom_field", "not found")
        return f"Custom: {x}"

    class CustomMiddleware(AgentMiddleware):
        state_schema = CustomState

    agent = create_agent(
        model=FakeToolCallingModel(
            tool_calls=[
                [{"args": {"x": 10}, "id": "custom_call", "name": "custom_state_tool"}],
                [],
            ]
        ),
        tools=[custom_state_tool],
        system_prompt="You are a helpful assistant.",
        middleware=[CustomMiddleware()],
    )

    result = agent.invoke(
        {
            "messages": [HumanMessage("Test custom state")],
            "custom_field": "custom_value",
        }
    )

    # Verify custom field was accessible
    assert runtime_state["custom_field"] == "custom_value"

    # Verify tool executed
    tool_message = result["messages"][2]
    assert isinstance(tool_message, ToolMessage)
    assert tool_message.content == "Custom: 10"

Domain

Subdomains

Frequently Asked Questions

What does test_tool_runtime_with_custom_state() do?
test_tool_runtime_with_custom_state() 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_with_custom_state() defined?
test_tool_runtime_with_custom_state() is defined in libs/langchain_v1/tests/unit_tests/agents/test_injected_runtime_create_agent.py at line 285.

Analyze Your Own Codebase

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

Try Supermodel Free