langchain - ✅(Solved) Fix context t should probably default to `Any`, not `None` [2 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35181Fetched 2026-04-08 00:27:19
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
cross-referenced ×2referenced ×2commented ×1labeled ×1

Fix Action

Fixed

PR fix notes

PR #35221: fix(langchain): default context type to Any (#35181)

Description (problem / solution / changelog)

Summary: Default unparameterized middleware/ModelRequest ContextT to Any in langchain agents to reduce false-positive type errors when context_schema is provided. Update middleware typing tests to reflect the new default and keep explicit None context as a mismatch case.

Fixes #35181

Breaking changes: Type-checking only: unparameterized middleware now treats ContextT as Any instead of None.

Verification:

uv run --group typing mypy tests/unit_tests/agents/middleware_typing/test_middleware_typing.py
uv run --group typing mypy tests/unit_tests/agents/middleware_typing/test_middleware_type_errors.py
uv run --group test pytest tests/unit_tests/agents/middleware_typing/test_middleware_typing.py -v
uv run --group test pytest tests/unit_tests/agents/middleware_typing/test_middleware_backwards_compat.py -v

Screenshot: unit_tests: 68aacc91476658d0e57d4a61a5e8a934

make test6e1a2d8bbd6d0c128a62f3be4097bf2a

Changed files

  • libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py (modified, +1/-1)
  • libs/langchain_v1/langchain/agents/middleware/types.py (modified, +4/-3)
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_backwards_compat.py (modified, +2/-2)
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_type_errors.py (modified, +10/-9)
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_typing.py (modified, +20/-8)

PR #35222: Fix #35221: fix(langchain): default context type to Any (#35181)

Description (problem / solution / changelog)

Summary

This PR fixes #35221: fix(langchain): default context type to Any (#35181)

Analysis

The issue requires changing the default type for ContextT from None to Any in the middleware type definitions. This affects the AgentMiddleware class and ModelRequest type definitions. The change allows unparameterized middleware to work with context_schema without type errors, while explicit None context remains a mismatch case.

Changes Made

Changed the default type for ContextT from None to Any in the middleware type definitions. This allows unparameterized middleware (AgentMiddleware without type parameters) to work seamlessly with context_schema without false-positive type errors. The explicit None context case (AgentMiddleware[..., None, ...]) remains as a mismatch case that will produce type errors when used with context_schema. Updated the typing tests to reflect this new behavior.

Files Changed

  • libs/langchain_v1/langchain/agents/middleware/types.py
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_typing.py
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_type_errors.py

This PR was automatically generated. Please review the changes before merging.

Fixes #35221

Changed files

  • libs/langchain_v1/langchain/agents/middleware/types.py (modified, +119/-1943)
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_type_errors.py (modified, +21/-83)
  • libs/langchain_v1/tests/unit_tests/agents/middleware_typing/test_middleware_typing.py (modified, +36/-300)
RAW_BUFFERClick to expand / collapse

Privileged issue

  • I am a LangChain maintainer, or was asked directly by a LangChain maintainer to create an issue here.

Issue Content

Technically breaking but would help resolve a lot of false positives on the type checking side

For langchain create_agent and AgentMiddleware

extent analysis

Fix: AgentMiddleware Type Checking Fix

Step-by-Step Solution Plan

  1. Update AgentMiddleware to accept Agent type:

    • Change the AgentMiddleware constructor to accept an Agent type instead of any.
    • Update the create_agent function to return an Agent type.

// Before class AgentMiddleware { constructor(middleware: any) { // ... } }

// After class AgentMiddleware { constructor(middleware: Agent) { // ... } }

function create_agent(): Agent { // ... return agent; }


2. **Update `create_agent` to return an `Agent` type**:
   - Update the return type of `create_agent` to `Agent`.

   ```typescript
function create_agent(): Agent {
  // ...
  return agent;
}
  1. Update type checking to reflect the changes:
    • Run yarn type-check or tsc to update the type checking.

Verification

  • Run yarn type-check or tsc to verify that the type checking passes.
  • Check that the create_agent function returns an Agent type.

Extra Tips

  • Make sure to update the documentation to reflect the changes.
  • Consider adding type guards to ensure that the AgentMiddleware constructor only accepts Agent types.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

langchain - ✅(Solved) Fix context t should probably default to `Any`, not `None` [2 pull requests, 1 comments, 2 participants]