Forward LangChain agent middleware from AgentSpecLoader to create_agent - #5
Open
spichen wants to merge 9 commits into
Open
Forward LangChain agent middleware from AgentSpecLoader to create_agent#5spichen wants to merge 9 commits into
spichen wants to merge 9 commits into
Conversation
spichen
force-pushed
the
feat/langgraph-extensibility-hooks
branch
3 times, most recently
from
April 21, 2026 07:03
4446f9b to
53a232d
Compare
spichen
force-pushed
the
feat/langgraph-extensibility-hooks
branch
5 times, most recently
from
May 13, 2026 07:38
9c472d3 to
67c9c35
Compare
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
force-pushed
the
feat/langgraph-extensibility-hooks
branch
from
May 29, 2026 04:59
cb177b7 to
2d8d697
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a
middlewarekeyword to the LangGraph adapter'sAgentSpecLoaderand its underlyingAgentSpecToLangGraphConverter. When non-empty, the list is passed verbatim as themiddleware=argument oflangchain_agents.create_agentinside_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
Noneor an empty list,create_agentis called with exactly the previous keyword set — nomiddleware=kwarg is passed — preserving byte-identical behavior for existing users.The motivating use case is downstream consumers that need to inject middleware such as
HumanInTheLoopMiddlewareor bespoke state / message-rewriting middleware into the ReAct agent without copy-pasting the entire_create_react_agent_with_given_infomethod. Middleware is a LangGraph-exposed concept, so the parameter is LangGraph-adapter specific — other adapters have their ownAgentSpecLoaderclasses and are unaffected.Tests in
tests/adapters/langgraph/test_middleware_parameter.pycover: 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/langgraphsuite passes underSKIP_LLM_TESTS=1(68 passed, 71 skipped, 0 failed).