Latest Results
fix(langchain): keep middleware-internal model calls off the `messages` stream (#41579)
Fixes #34139
Fixes #34382
Supersedes #34351 (@Manju080); also replaces #34501 (@devbyteai) and
#35339 (@Nitin75408), which cleared callbacks and broke tracing/other
handlers. Thanks @nick-youngblut and @Allaoua9 for the reports.
`SummarizationMiddleware`, `LLMToolEmulator` and
`LLMToolSelectorMiddleware` no longer leak their internal model output
into `stream_mode="messages"`. The calls are tagged `nostream` and
remain visible in traces and `astream_events`. To track their progress
or tokens, use `astream_events` or a callback like
`UsageMetadataCallbackHandler`.
---
## Problem
Three middleware make their own model calls behind the scenes:
- `SummarizationMiddleware` writes a conversation summary
- `LLMToolEmulator` invents a tool's output
- `LLMToolSelectorMiddleware` picks tools for the next turn
With `agent.stream(..., stream_mode="messages")`, those tokens show up
in the agent's stream, so a chat UI displays the summary, fake tool
output or selection JSON as if the agent said it. Both issues were
closed after #39252, but that only fixed `stream_events` v3;
`stream_mode="messages"` still leaks.
## Fix
- Tag each internal call `nostream`. LangGraph already drops tagged runs
from the `messages` stream (and v3). Matches
langchain-ai/langchainjs#11331.
- Calls stay in LangSmith traces and `astream_events`, and keep
`lc_source` / `lc_internal_call` metadata.
- Remove the now-unused `InternalCallTransformer` registrations on these
three middleware (the class itself stays).
```text
before: 'SUMMARY-TEXTFinal answer.'
after: 'Final answer.'
```
## Heads up
- Anything reading these tokens from the `messages` stream stops seeing
them. deepagents-code does (compaction spinner, `/cost`); paired
deepagents changes should ship before or with this. Latest Branches
0%
0%
mdrxy/langchain/explain-baseten-minimum 0%
漏 2026 CodSpeed Technology