The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
chore(langchain): fix types in `test_todo`, `test_tool_retry` (#34503)
Christophe Bornet5fbf270c9def
diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.pyindex 382ce12026..eb37700f4a 100644--- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py+++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py@@ -2,7 +2,7 @@from __future__ import annotations-from typing import TYPE_CHECKING, cast+from typing import TYPE_CHECKING, Any, castimport pytestfrom langchain_core.language_models.fake_chat_models import GenericFakeChatModel@@ -133,7 +133,9 @@ def test_appends_to_existing_system_prompt() -> None:(None, "## `write_todos`"),],)-def test_todo_middleware_on_model_call(original_prompt, expected_prompt_prefix) -> None:+def test_todo_middleware_on_model_call(+ original_prompt: str | None, expected_prompt_prefix: str+) -> None:"""Test that wrap_model_call handles system prompts correctly."""middleware = TodoListMiddleware()model = FakeToolCallingModel()@@ -154,15 +156,16 @@ def test_todo_middleware_on_model_call(original_prompt, expected_prompt_prefix)captured_request = None- def mock_handler(req: ModelRequest) -> AIMessage:+ def mock_handler(req: ModelRequest) -> ModelResponse:nonlocal captured_requestcaptured_request = req- return AIMessage(content="mock response")+ return ModelResponse(result=[AIMessage(content="mock response")])# Call wrap_model_call to trigger the middleware logicmiddleware.wrap_model_call(request, mock_handler)# Check that the modified request passed to handler has the expected promptassert captured_request is not None+ assert captured_request.system_prompt is not Noneassert captured_request.system_prompt.startswith(expected_prompt_prefix)# Original request should be unchangedassert request.system_prompt == original_prompt@@ -212,10 +215,10 @@ def test_todo_middleware_custom_system_prompt() -> None:captured_request = None- def mock_handler(req: ModelRequest) -> AIMessage:+ def mock_handler(req: ModelRequest) -> ModelResponse:nonlocal captured_requestcaptured_request = req- return AIMessage(content="mock response")+ return ModelResponse(result=[AIMessage(content="mock response")])# Call wrap_model_call to trigger the middleware logicmiddleware.wrap_model_call(request, mock_handler)@@ -273,10 +276,10 @@ def test_todo_middleware_custom_system_prompt_and_tool_description() -> None:captured_request = None- def mock_handler(req: ModelRequest) -> AIMessage:+ def mock_handler(req: ModelRequest) -> ModelResponse:nonlocal captured_requestcaptured_request = req- return AIMessage(content="mock response")+ return ModelResponse(result=[AIMessage(content="mock response")])# Call wrap_model_call to trigger the middleware logicmiddleware.wrap_model_call(request, mock_handler)@@ -326,7 +329,9 @@ def test_todo_middleware_custom_system_prompt_and_tool_description() -> None:),],)-def test_todo_middleware_write_todos_tool_execution(todos, expected_message) -> None:+def test_todo_middleware_write_todos_tool_execution(+ todos: list[dict[str, Any]], expected_message: str+) -> None:"""Test that the write_todos tool executes correctly."""tool_call = {"args": {"todos": todos},@@ -346,7 +351,9 @@ def test_todo_middleware_write_todos_tool_execution(todos, expected_message) ->[{"status": "pending"}],],)-def test_todo_middleware_write_todos_tool_validation_errors(invalid_todos) -> None:+def test_todo_middleware_write_todos_tool_validation_errors(+ invalid_todos: list[dict[str, Any]],+) -> None:"""Test that the write_todos tool rejects invalid input."""tool_call = {"args": {"todos": invalid_todos},@@ -784,8 +791,8 @@ async def test_handler_called_with_modified_request_async() -> None:"""Test async version - handler receives the modified request."""middleware = TodoListMiddleware()request = _make_request(system_prompt="Original")- handler_called = {"value": False}- received_prompt = {"value": None}+ handler_called: dict[str, bool] = {"value": False}+ received_prompt: dict[str, str | None] = {"value": None}async def mock_handler(req: ModelRequest) -> ModelResponse:handler_called["value"] = Truediff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_retry.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_retry.pyindex 1e45a8a42e..d430fc714a 100644--- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_retry.py+++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_retry.py@@ -2,6 +2,7 @@import timefrom collections.abc import Callable+from typing import Anyimport pytestfrom langchain_core.messages import HumanMessage, ToolCall, ToolMessage@@ -869,10 +870,14 @@ def test_tool_retry_multiple_middleware_composition() -> None:# Custom middleware that logs calls@wrap_tool_call- def logging_middleware(request: ToolCallRequest, handler: Callable) -> ToolMessage | Command:- call_log.append(f"before_{request.tool.name}")+ def logging_middleware(+ request: ToolCallRequest, handler: Callable[[ToolCallRequest], ToolMessage | Command[Any]]+ ) -> ToolMessage | Command[Any]:+ if request.tool:+ call_log.append(f"before_{request.tool.name}")response = handler(request)- call_log.append(f"after_{request.tool.name}")+ if request.tool:+ call_log.append(f"after_{request.tool.name}")return responsemodel = FakeToolCallingModel(@@ -909,7 +914,7 @@ def test_tool_retry_deprecated_raise_keyword() -> None:with pytest.warns(DeprecationWarning, match="on_failure='raise' is deprecated"):retry = ToolRetryMiddleware(max_retries=2,- on_failure="raise",+ on_failure="raise", # type: ignore[arg-type])# Should be converted to 'error'@@ -948,7 +953,7 @@ def test_tool_retry_deprecated_raise_behavior() -> None:max_retries=2,initial_delay=0.01,jitter=False,- on_failure="raise",+ on_failure="raise", # type: ignore[arg-type])agent = create_agent(