Home / Function/ test_hook_with_class_inheritance() — langchain Function Reference

test_hook_with_class_inheritance() — langchain Function Reference

Architecture documentation for the test_hook_with_class_inheritance() function in test_framework.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  05e1929f_e974_6405_7ed0_3f3f64215f33["test_hook_with_class_inheritance()"]
  9e2d20bd_cff7_3577_a41c_be4a0e929bf8["TestAgentMiddlewareHooks"]
  05e1929f_e974_6405_7ed0_3f3f64215f33 -->|defined in| 9e2d20bd_cff7_3577_a41c_be4a0e929bf8
  dc05ca59_23b5_cbf7_ea94_ff774cf0fc5a["abefore_agent()"]
  05e1929f_e974_6405_7ed0_3f3f64215f33 -->|calls| dc05ca59_23b5_cbf7_ea94_ff774cf0fc5a
  7322ead4_4fab_6293_fe16_c328ed9af04d["aafter_agent()"]
  05e1929f_e974_6405_7ed0_3f3f64215f33 -->|calls| 7322ead4_4fab_6293_fe16_c328ed9af04d
  4014cea1_1c2d_cca6_4f48_176441158497["before_agent()"]
  05e1929f_e974_6405_7ed0_3f3f64215f33 -->|calls| 4014cea1_1c2d_cca6_4f48_176441158497
  f8c801ee_06dc_05e5_b5d9_6499452ee141["after_agent()"]
  05e1929f_e974_6405_7ed0_3f3f64215f33 -->|calls| f8c801ee_06dc_05e5_b5d9_6499452ee141
  style 05e1929f_e974_6405_7ed0_3f3f64215f33 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py lines 832–875

    async def test_hook_with_class_inheritance(self, *, is_async: bool, hook_type: str) -> None:
        """Test agent hooks using class inheritance in both sync and async modes."""
        execution_log: list[str] = []

        class AsyncCustomMiddleware(AgentMiddleware):
            async def abefore_agent(
                self, state: AgentState[Any], runtime: Runtime
            ) -> dict[str, Any] | None:
                if hook_type == "before":
                    execution_log.append("hook_called")
                return None

            async def aafter_agent(
                self, state: AgentState[Any], runtime: Runtime
            ) -> dict[str, Any] | None:
                if hook_type == "after":
                    execution_log.append("hook_called")
                return None

        class CustomMiddleware(AgentMiddleware):
            def before_agent(
                self, state: AgentState[Any], runtime: Runtime
            ) -> dict[str, Any] | None:
                if hook_type == "before":
                    execution_log.append("hook_called")
                return None

            def after_agent(
                self, state: AgentState[Any], runtime: Runtime
            ) -> dict[str, Any] | None:
                if hook_type == "after":
                    execution_log.append("hook_called")
                return None

        middleware = AsyncCustomMiddleware() if is_async else CustomMiddleware()
        model = GenericFakeChatModel(messages=iter([AIMessage(content="Response")]))
        agent = create_agent(model=model, tools=[], middleware=[middleware])

        if is_async:
            await agent.ainvoke({"messages": [HumanMessage("Test")]})
        else:
            agent.invoke({"messages": [HumanMessage("Test")]})

        assert "hook_called" in execution_log

Domain

Subdomains

Frequently Asked Questions

What does test_hook_with_class_inheritance() do?
test_hook_with_class_inheritance() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py.
Where is test_hook_with_class_inheritance() defined?
test_hook_with_class_inheritance() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py at line 832.
What does test_hook_with_class_inheritance() call?
test_hook_with_class_inheritance() calls 4 function(s): aafter_agent, abefore_agent, after_agent, before_agent.

Analyze Your Own Codebase

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

Try Supermodel Free