Home / File/ test_middleware_backwards_compat.py — langchain Source File

test_middleware_backwards_compat.py — langchain Source File

Architecture documentation for test_middleware_backwards_compat.py, a python file in the langchain codebase. 9 imports, 0 dependents.

File python LangChainCore ApiManagement 9 imports 13 functions 7 classes

Entity Profile

Dependency Diagram

graph LR
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0["test_middleware_backwards_compat.py"]
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> feec1ec4_6917_867b_d228_b134d0ff8099
  f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> f69d6389_263d_68a4_7fbf_f14c0602a9ba
  00fc047e_6208_0289_ebf0_f5b91b72ee90["langchain_core.language_models.fake_chat_models"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> 00fc047e_6208_0289_ebf0_f5b91b72ee90
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> f85fae70_1011_eaec_151c_4083140ae9e5
  d9a6942a_c37a_07f8_ed13_74d0fdc117be["langchain.agents"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> d9a6942a_c37a_07f8_ed13_74d0fdc117be
  a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> a681398d_ed44_c914_1a44_5d174223b069
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  e07f6d54_afcc_052d_d33f_8ccdcc46f752["langgraph.runtime"]
  74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 --> e07f6d54_afcc_052d_d33f_8ccdcc46f752
  style 74d2d22f_07ae_c5bb_33c3_7da58e2da5a0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test backwards compatibility for middleware type parameters.

This file verifies that middlewares written BEFORE the ResponseT change still work.
All patterns that were valid before should remain valid.

Run type check: uv run --group typing mypy <this file>
Run tests: uv run --group test pytest <this file> -v
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

import pytest
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, HumanMessage
from typing_extensions import TypedDict

from langchain.agents import create_agent
from langchain.agents.middleware.types import (
    AgentMiddleware,
    AgentState,
    ContextT,
    ModelRequest,
    ModelResponse,
    before_model,
)

if TYPE_CHECKING:
    from collections.abc import Awaitable, Callable

    from langgraph.runtime import Runtime


# =============================================================================
# OLD PATTERN 1: Completely unparameterized AgentMiddleware
# This was the most common pattern for simple middlewares
# =============================================================================
class OldStyleMiddleware1(AgentMiddleware):
    """Middleware with no type parameters at all - most common old pattern."""

    def before_model(self, state: AgentState[Any], runtime: Runtime[None]) -> dict[str, Any] | None:
        # Simple middleware that just logs or does something
        return None

    def wrap_model_call(
        self,
        request: ModelRequest,  # No type param
        handler: Callable[[ModelRequest], ModelResponse],  # No type params
    ) -> ModelResponse:  # No type param
        return handler(request)


# =============================================================================
# OLD PATTERN 2: AgentMiddleware with only 2 type parameters (StateT, ContextT)
# This was the pattern before ResponseT was added
# =============================================================================
class OldStyleMiddleware2(AgentMiddleware[AgentState[Any], ContextT]):
    """Middleware with 2 type params - the old signature before ResponseT."""

// ... (216 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • langchain.agents
  • langchain.agents.middleware.types
  • langchain_core.language_models.fake_chat_models
  • langchain_core.messages
  • langgraph.runtime
  • pytest
  • typing
  • typing_extensions

Frequently Asked Questions

What does test_middleware_backwards_compat.py do?
test_middleware_backwards_compat.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What functions are defined in test_middleware_backwards_compat.py?
test_middleware_backwards_compat.py defines 13 function(s): collections, fake_model, old_style_decorator, test_model_request_backwards_compat, test_model_response_backwards_compat, test_multiple_old_style_middlewares, test_old_pattern_1_unparameterized, test_old_pattern_2_two_params, test_old_pattern_3_explicit_none, test_old_pattern_4_specific_context, and 3 more.
What does test_middleware_backwards_compat.py depend on?
test_middleware_backwards_compat.py imports 9 module(s): collections.abc, langchain.agents, langchain.agents.middleware.types, langchain_core.language_models.fake_chat_models, langchain_core.messages, langgraph.runtime, pytest, typing, and 1 more.
Where is test_middleware_backwards_compat.py in the architecture?
test_middleware_backwards_compat.py is located at libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_backwards_compat.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/langchain_v1/tests/unit_tests/agents/middleware_typing).

Analyze Your Own Codebase

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

Try Supermodel Free