Home / File/ test_middleware_type_errors.py — langchain Source File

test_middleware_type_errors.py — langchain Source File

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

File python LangChainCore ApiManagement 7 imports 4 functions 12 classes

Entity Profile

Dependency Diagram

graph LR
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22["test_middleware_type_errors.py"]
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> feec1ec4_6917_867b_d228_b134d0ff8099
  dd5e7909_a646_84f1_497b_cae69735550e["pydantic"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> dd5e7909_a646_84f1_497b_cae69735550e
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> f85fae70_1011_eaec_151c_4083140ae9e5
  d9a6942a_c37a_07f8_ed13_74d0fdc117be["langchain.agents"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> d9a6942a_c37a_07f8_ed13_74d0fdc117be
  a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> a681398d_ed44_c914_1a44_5d174223b069
  069947d2_727b_035a_0691_c12203e2f5a6["tests.unit_tests.agents.model"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> 069947d2_727b_035a_0691_c12203e2f5a6
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  47e0e3d7_cac1_0ec3_228a_0fd46437bc22 --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  style 47e0e3d7_cac1_0ec3_228a_0fd46437bc22 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Demonstrate type errors that mypy catches for ContextT and ResponseT mismatches.

This file contains intentional type errors to demonstrate that mypy catches them.
Run: uv run --group typing mypy <this file>

Expected errors:
1. TypedDict "UserContext" has no key "session_id" - accessing wrong context field
2. Argument incompatible with supertype - mismatched ModelRequest type
3. Cannot infer value of type parameter - middleware/context_schema mismatch
4. "AnalysisResult" has no attribute "summary" - accessing wrong response field
5. Handler returns wrong ResponseT type
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from pydantic import BaseModel
from typing_extensions import TypedDict

from langchain.agents import create_agent
from langchain.agents.middleware.types import (
    AgentMiddleware,
    AgentState,
    ContextT,
    ModelRequest,
    ModelResponse,
)
from tests.unit_tests.agents.model import FakeToolCallingModel

if TYPE_CHECKING:
    from collections.abc import Callable


# =============================================================================
# Context and Response schemas
# =============================================================================
class UserContext(TypedDict):
    user_id: str
    user_name: str


class SessionContext(TypedDict):
    session_id: str
    expires_at: int


class AnalysisResult(BaseModel):
    sentiment: str
    confidence: float


class SummaryResult(BaseModel):
    summary: str
    key_points: list[str]


# =============================================================================
# ERROR 1: Using wrong context fields
# =============================================================================
// ... (142 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • langchain.agents
  • langchain.agents.middleware.types
  • pydantic
  • tests.unit_tests.agents.model
  • typing
  • typing_extensions

Frequently Asked Questions

What does test_middleware_type_errors.py do?
test_middleware_type_errors.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_type_errors.py?
test_middleware_type_errors.py defines 4 function(s): collections, test_backwards_compat_with_context_schema, test_mismatched_context_schema, test_mismatched_response_format.
What does test_middleware_type_errors.py depend on?
test_middleware_type_errors.py imports 7 module(s): collections.abc, langchain.agents, langchain.agents.middleware.types, pydantic, tests.unit_tests.agents.model, typing, typing_extensions.
Where is test_middleware_type_errors.py in the architecture?
test_middleware_type_errors.py is located at libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_type_errors.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