Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions libs/deepagents/deepagents/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

from langchain.agents import AgentState, create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware, InterruptOnConfig, TodoListMiddleware
from langchain.agents.middleware.types import AgentMiddleware, InputAgentState, OutputAgentState, ResponseT
from langchain.agents.middleware.types import (
AgentMiddleware,
InputAgentState,
OutputAgentState,
ResponseT,
StateT_co,
)
from langchain.agents.structured_output import ResponseFormat
from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import AnthropicPromptCachingMiddleware
Expand Down Expand Up @@ -238,7 +244,7 @@ def create_deep_agent( # noqa: C901, PLR0912, PLR0915 # Complex graph assembly
tools: Sequence[BaseTool | Callable | dict[str, Any]] | None = None,
*,
system_prompt: str | SystemMessage | None = None,
middleware: Sequence[AgentMiddleware] = (),
middleware: Sequence[AgentMiddleware[StateT_co, ContextT]] = (),
subagents: Sequence[SubAgent | CompiledSubAgent | AsyncSubAgent] | None = None,
skills: list[str] | None = None,
memory: list[str] | None = None,
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Type-checking fixture for context-aware middleware on `create_deep_agent`.

This module is not executed; it is type-checked by `ty` from
`test_graph.py::TestMiddlewareTyping` to guard against regressions where the
`middleware` parameter pins `ContextT` to `None` (see issue #4051).
"""

from dataclasses import dataclass

from langchain.agents import AgentState
from langchain.agents.middleware import AgentMiddleware

from deepagents import create_deep_agent


@dataclass
class Context:
"""Runtime context schema used by the context-aware middleware."""

user_name: str


class ContextAwareMiddleware(AgentMiddleware[AgentState, Context]):
"""Middleware parameterized with a concrete context type."""


create_deep_agent(
context_schema=Context,
middleware=[ContextAwareMiddleware()],
)
30 changes: 30 additions & 0 deletions libs/deepagents/tests/unit_tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from __future__ import annotations

import logging
import shutil
import subprocess
import sys
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
from unittest.mock import MagicMock, patch

Expand Down Expand Up @@ -2081,3 +2085,29 @@ def test_get_default_model_emits_langchain_deprecation_warning(self) -> None:
msg = str(deprecations[0].message)
assert "deprecated" in msg
assert "https://docs.langchain.com/oss/python/deepagents/models" in msg


class TestMiddlewareTyping:
"""Type-level coverage for `create_deep_agent`'s `middleware` parameter."""

def test_context_aware_middleware_type_checks(self) -> None:
"""Context-aware middleware must type-check under `context_schema` (#4051).

Runs `ty` over a fixture that passes an
`AgentMiddleware[AgentState, Context]` alongside `context_schema=Context`.
Before the fix the `middleware` parameter pinned `ContextT=None`, so the
type checker rejected the call.
"""
if shutil.which("ty") is None:
pytest.skip("ty is not installed in this environment")

package_root = Path(__file__).parents[2]
fixture = Path(__file__).parent / "_typing_fixtures" / "context_aware_middleware.py"
result = subprocess.run( # noqa: S603 # fixed args, no user input
[sys.executable, "-m", "ty", "check", str(fixture)],
cwd=package_root,
capture_output=True,
text=True,
check=False,
)
assert result.returncode == 0, f"ty reported diagnostics:\n{result.stdout}\n{result.stderr}"
Loading