Skip to content

feat(langchain): MiddlewareTransformer for agent streams#36898

Open
Nick Hollon (nick-hollon-lc) wants to merge 3 commits intonh/agent-streamerfrom
nh/middleware-transformer
Open

feat(langchain): MiddlewareTransformer for agent streams#36898
Nick Hollon (nick-hollon-lc) wants to merge 3 commits intonh/agent-streamerfrom
nh/middleware-transformer

Conversation

@nick-hollon-lc
Copy link
Copy Markdown

@nick-hollon-lc Nick Hollon (nick-hollon-lc) commented Apr 20, 2026

Summary

  • Adds native MiddlewareTransformer + typed MiddlewareEvent dataclass, pre-registered on AgentStreamer.builtin_factories so every create_agent run exposes run.middleware without opting in.
  • Consumes the graph's existing updates stream mode and filters by the "{name}.{phase}" node-naming convention create_agent already uses (before_agent / before_model / after_model / after_agent).
  • No changes required in middleware implementations — the node-name convention is the contract.
  • run.middleware is an in-process-only projection; it's not routed anywhere outside the caller's process.
  • Lives in langchain (not langgraph prebuilt) because middleware is a langchain concept — langgraph has no middleware primitive. Parallels how ToolCallTransformer lives next to ToolNode in langgraph prebuilt.

Targeted at nh/agent-streamer — builds on top of AgentStreamer.

Adds a native `MiddlewareTransformer` and typed `MiddlewareEvent`
dataclass, pre-registered on `AgentStreamer.builtin_factories` so
every `create_agent` run exposes `run.middleware` alongside
`run.tool_calls` without the caller opting in.

The transformer consumes the graph's existing `updates` stream mode,
filters by the `"{name}.{phase}"` node-naming convention
`create_agent` already uses, and re-emits typed events. Middleware
implementations do not need to change; the node-name convention is
the contract. `run.middleware` is an in-process-only projection.

Lives in langchain (not langgraph prebuilt) because middleware is a
langchain-specific concept — langgraph has no middleware primitive.
@github-actions github-actions Bot added feature For PRs that implement a new feature; NOT A FEATURE REQUEST internal langchain `langchain` package issues & PRs size: M 200-499 LOC labels Apr 20, 2026
# Conflicts:
#	libs/langchain_v1/langchain/agents/__init__.py
#	libs/langchain_v1/langchain/agents/_streaming.py
# Conflicts:
#	libs/langchain_v1/langchain/agents/factory.py
@github-actions github-actions Bot added anthropic `langchain-anthropic` package issues & PRs chroma `langchain-chroma` package issues & PRs core `langchain-core` package issues & PRs deepseek `langchain-deepseek` package issues & PRs dependencies Pull requests that update a dependency file (e.g. `pyproject.toml` or `uv.lock`) exa `langchain-exa` package issues & PRs fireworks `langchain-fireworks` package issues & PRs github_actions Pull requests that update GitHub Actions code groq `langchain-groq` package issues & PRs huggingface `langchain-huggingface` package issues & PRs integration PR made that is related to a provider partner package integration langchain-classic `langchain-classic` package issues & PRs mistralai `langchain-mistralai` package issues & PRs model-profiles `langchain-model-profiles` package issues & PRs nomic `langchain-nomic` package issues & PRs ollama `langchain-ollama` package issues & PRs openai `langchain-openai` package issues & PRs openrouter `langchain-openrouter` package issues & PRs and removed size: M 200-499 LOC labels Apr 21, 2026
@nick-hollon-lc Nick Hollon (nick-hollon-lc) marked this pull request as draft April 21, 2026 18:36
@github-actions github-actions Bot added size: M 200-499 LOC and removed size: XL 1000+ LOC labels Apr 21, 2026
@nick-hollon-lc Nick Hollon (nick-hollon-lc) removed integration PR made that is related to a provider partner package integration core `langchain-core` package issues & PRs standard-tests Related to the package `standard-tests` text-splitters Related to the package `text-splitters` dependencies Pull requests that update a dependency file (e.g. `pyproject.toml` or `uv.lock`) github_actions Pull requests that update GitHub Actions code langchain-classic `langchain-classic` package issues & PRs model-profiles `langchain-model-profiles` package issues & PRs anthropic `langchain-anthropic` package issues & PRs chroma `langchain-chroma` package issues & PRs deepseek `langchain-deepseek` package issues & PRs exa `langchain-exa` package issues & PRs fireworks `langchain-fireworks` package issues & PRs groq `langchain-groq` package issues & PRs huggingface `langchain-huggingface` package issues & PRs mistralai `langchain-mistralai` package issues & PRs nomic `langchain-nomic` package issues & PRs ollama `langchain-ollama` package issues & PRs openai `langchain-openai` package issues & PRs perplexity `langchain-perplexity` package issues & PRs qdrant `langchain-qdrant` package issues & PRs xai `langchain-xai` package issues & PRs openrouter `langchain-openrouter` package issues & PRs labels Apr 21, 2026
@nick-hollon-lc Nick Hollon (nick-hollon-lc) marked this pull request as ready for review April 21, 2026 19:32
Copy link
Copy Markdown
Collaborator

@ccurme ccurme (ccurme) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider if we need this.

@mdrxy Mason Daugherty (mdrxy) changed the title feat(langchain): MiddlewareTransformer for agent streams feat(langchain): MiddlewareTransformer for agent streams Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature For PRs that implement a new feature; NOT A FEATURE REQUEST internal langchain `langchain` package issues & PRs size: M 200-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants