Skip to content

Forward LangChain agent middleware from AgentSpecLoader to create_agent - #5

Open
spichen wants to merge 9 commits into
mainfrom
feat/langgraph-extensibility-hooks
Open

Forward LangChain agent middleware from AgentSpecLoader to create_agent#5
spichen wants to merge 9 commits into
mainfrom
feat/langgraph-extensibility-hooks

Conversation

@spichen

@spichen spichen commented Apr 21, 2026

Copy link
Copy Markdown
Owner

Add a middleware keyword to the LangGraph adapter's AgentSpecLoader and its underlying AgentSpecToLangGraphConverter. When non-empty, the list is passed verbatim as the middleware= argument of langchain_agents.create_agent inside _create_react_agent_with_given_info. The list is copied defensively on construction and again on forward, so caller-side mutations cannot leak into subsequent conversions.

When the parameter is None or an empty list, create_agent is called with exactly the previous keyword set — no middleware= kwarg is passed — preserving byte-identical behavior for existing users.

The motivating use case is downstream consumers that need to inject middleware such as HumanInTheLoopMiddleware or bespoke state / message-rewriting middleware into the ReAct agent without copy-pasting the entire _create_react_agent_with_given_info method. Middleware is a LangGraph-exposed concept, so the parameter is LangGraph-adapter specific — other adapters have their own AgentSpecLoader classes and are unaffected.

Tests in tests/adapters/langgraph/test_middleware_parameter.py cover: the default (no kwarg forwarded); the empty-list case (also not forwarded); non-empty list forwarded in order; the converter accepting middleware directly without going through the loader; and the list being copied so post-construction mutation of the caller's list has no effect on conversions.

Full tests/adapters/langgraph suite passes under SKIP_LLM_TESTS=1 (68 passed, 71 skipped, 0 failed).

@spichen
spichen force-pushed the feat/langgraph-extensibility-hooks branch 3 times, most recently from 4446f9b to 53a232d Compare April 21, 2026 07:03
@spichen
spichen force-pushed the feat/langgraph-extensibility-hooks branch 5 times, most recently from 9c472d3 to 67c9c35 Compare May 13, 2026 07:38
spichen added 4 commits May 29, 2026 08:57
Add a `middleware` keyword on the LangGraph `AgentSpecLoader` and its converter. When the list is non-empty it's passed as `middleware=` to `langchain_agents.create_agent` inside `_create_react_agent_with_given_info`. Both the loader and the converter copy the list on construction, so later mutations by the caller don't leak in.

If `middleware` is `None` or `[]`, `create_agent` is called exactly as before, no `middleware=` kwarg and no behavior change for existing users.

Why: downstream code that wants to use `HumanInTheLoopMiddleware` or its own state/message-rewriting middleware currently has to subclass the converter and copy-paste the entire `_create_react_agent_with_given_info` method just to thread the keyword through. Middleware is a LangGraph thing, so this parameter only shows up on the LangGraph adapter. Other adapters have their own loader classes and aren't affected.
Signed-off-by: Salah Pichen <nh.salah@gmail.com>
create_agent(**kwargs) hides the return type, so mypy on 3.10/3.11
was failing. The annotation fixes it.
@spichen
spichen force-pushed the feat/langgraph-extensibility-hooks branch from cb177b7 to 2d8d697 Compare May 29, 2026 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant