Home / Class/ TestChainModelCallHandlers Class — langchain Architecture

TestChainModelCallHandlers Class — langchain Architecture

Architecture documentation for the TestChainModelCallHandlers class in test_composition.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5e72c447_c04a_d317_225b_afa4d498b59e["TestChainModelCallHandlers"]
  e8d75ab8_cc79_e047_2101_0ea8e47809e6["_ComposedExtendedModelResponse"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|extends| e8d75ab8_cc79_e047_2101_0ea8e47809e6
  529538f3_9470_dbd3_2a47_9e95e11a4afe["test_composition.py"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|defined in| 529538f3_9470_dbd3_2a47_9e95e11a4afe
  d7da206d_6194_e663_cffc_1a6149fe3047["test_empty_handlers_returns_none()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| d7da206d_6194_e663_cffc_1a6149fe3047
  30869bdc_14b4_affc_afd0_7aa8d1b64dc7["test_single_handler_returns_unchanged()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| 30869bdc_14b4_affc_afd0_7aa8d1b64dc7
  d48098ee_abf9_680f_f095_b00ba3ff95fb["test_two_handlers_basic_composition()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| d48098ee_abf9_680f_f095_b00ba3ff95fb
  9571a66f_3cfc_fb64_879e_3904d5ef8907["test_two_handlers_with_commands()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| 9571a66f_3cfc_fb64_879e_3904d5ef8907
  e7ef9e7c_7cac_65af_270c_7a6b7507aa10["test_three_handlers_composition()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| e7ef9e7c_7cac_65af_270c_7a6b7507aa10
  5b594f23_684f_e87d_2e3a_0e9fb2102257["test_inner_handler_retry()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| 5b594f23_684f_e87d_2e3a_0e9fb2102257
  fb071b18_f4bf_e841_c09c_bea353d5e793["test_error_to_success_conversion()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| fb071b18_f4bf_e841_c09c_bea353d5e793
  630dd632_c630_42c1_703e_e565a3711b57["test_request_modification()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| 630dd632_c630_42c1_703e_e565a3711b57
  e66fea12_e10b_1f15_92c0_d237f0b1cff1["test_composition_preserves_state_and_runtime()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| e66fea12_e10b_1f15_92c0_d237f0b1cff1
  d5463e49_cba1_1c30_cf32_ff569b0404f6["test_multiple_yields_in_retry_loop()"]
  5e72c447_c04a_d317_225b_afa4d498b59e -->|method| d5463e49_cba1_1c30_cf32_ff569b0404f6

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_composition.py lines 40–349

class TestChainModelCallHandlers:
    """Test the `_chain_model_call_handlers` composition function."""

    def test_empty_handlers_returns_none(self) -> None:
        """Test that empty handlers list returns None."""
        result = _chain_model_call_handlers([])
        assert result is None

    def test_single_handler_returns_unchanged(self) -> None:
        """Test that single handler is wrapped to normalize output."""

        def handler(
            request: ModelRequest, base_handler: Callable[[ModelRequest], ModelResponse]
        ) -> ModelResponse:
            return base_handler(request)

        result = _chain_model_call_handlers([handler])
        # Result is wrapped to normalize, so it won't be identical
        assert result is not None
        assert callable(result)

    def test_two_handlers_basic_composition(self) -> None:
        """Test basic composition of two handlers."""
        execution_order = []

        def outer(
            request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
        ) -> ModelResponse:
            execution_order.append("outer-before")
            result = handler(request)
            execution_order.append("outer-after")
            return result

        def inner(
            request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
        ) -> ModelResponse:
            execution_order.append("inner-before")
            result = handler(request)
            execution_order.append("inner-after")
            return result

        composed = _chain_model_call_handlers([outer, inner])
        assert composed is not None

        result = composed(create_test_request(), create_mock_base_handler())

        assert execution_order == [
            "outer-before",
            "inner-before",
            "inner-after",
            "outer-after",
        ]
        # Outermost result is always _ComposedExtendedModelResponse
        assert isinstance(result, _ComposedExtendedModelResponse)
        assert result.model_response.result[0].content == "test"

    def test_two_handlers_with_commands(self) -> None:
        """Test that commands from inner and outer are collected correctly."""

        def outer(
            request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
        ) -> ExtendedModelResponse:
            response = handler(request)
            return ExtendedModelResponse(
                model_response=response,
                command=Command(update={"outer_key": "outer_val"}),
            )

        def inner(
            request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
        ) -> ExtendedModelResponse:
            response = handler(request)
            return ExtendedModelResponse(
                model_response=response,
                command=Command(update={"inner_key": "inner_val"}),
            )

        composed = _chain_model_call_handlers([outer, inner])
        assert composed is not None

        result = composed(create_test_request(), create_mock_base_handler())

Frequently Asked Questions

What is the TestChainModelCallHandlers class?
TestChainModelCallHandlers is a class in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_composition.py.
Where is TestChainModelCallHandlers defined?
TestChainModelCallHandlers is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_composition.py at line 40.
What does TestChainModelCallHandlers extend?
TestChainModelCallHandlers extends _ComposedExtendedModelResponse.

Analyze Your Own Codebase

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

Try Supermodel Free