AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

SummarizationMiddleware Replaces History With A Placeholder When No HumanMessage Falls In The Trim Window

SummarizationMiddleware silently deletes conversation history and inserts the placeholder 'Previous conversation was too long to summarize.' when the only HumanMessage is outside the trimmed token window. The summary model is never called, and with a checkpointer the data loss is persisted.

highConfidence 90%Langchain

Origin Analysis

_trim_messages_for_summary calls trim_messages with strategy='last', max_tokens=trim_tokens_to_summarize, start_on='human'. In a long tool loop the only HumanMessage is outside the last-4000-token window, so trim returns an empty list. _create_summary treats an empty list as final and returns the placeholder, which before_model then uses as if it were a real summary.
1. Create a SummarizationMiddleware with trigger tokens and keep messages. 2. Build a message list with one HumanMessage followed by many AIMessage tool calls and ToolMessages, such that the HumanMessage is outside the trim window. 3. Call middleware.before_model. 4. Observe that the summary model is never invoked and the output contains the placeholder text instead of a summary.

Fixing Code Block

Edge Case Audit

This fallback may include non-HumanMessage messages in the summarization prompt, but that is safe because the span is only serialized to text for the model. In rare cases where the summary model still fails or returns the placeholder, the history would still be replaced; consider adding an explicit None/placeholder guard before removing summarized messages. Existing checkpoints corrupted by this bug will not be repaired automatically. To rollback, revert the method to the original start_on='human' only version.

Ecosystem Topology