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

SummarizationMiddleware._Find_safe_cutoff_point Preserves Orphan ToolMessages Causing Provider 400

When the target cutoff lands on a ToolMessage, _find_safe_cutoff_point uses set intersection instead of full coverage to find a safe AIMessage, allowing orphan ToolMessages to survive into the preserved window. This leads to provider 400 errors on the next chat completion.

highConfidence 92%LangchainEmbeddingsChainsAgents

Origin Analysis

The method collects tool_call_ids from consecutive ToolMessages at the cutoff point, then walks backward looking for an AIMessage whose tool_calls intersect with that set. The intersection check passes if any id matches, even if other ids remain uncovered. In mixed cases where one ToolMessage's originating AIMessage was already summarized away but another AIMessage matches a different id, the function returns an index that still includes the orphan ToolMessage.
Run the provided Python example with SummarizationMiddleware configured with trigger=("messages",4), keep=("messages",3) on a history containing a mixed AI/Tool pair where one ToolMessage id is orphan. The middleware will produce a preserved slice starting at the AIMessage with the matched id, but also include the orphan ToolMessage, causing the next model call to fail with 400.

Fixing Code Block

Edge Case Audit

The fallback (returning run_end) may discard valid AI/Tool pairs that were immediately before the orphaned group, leading to loss of context and potentially degraded agent performance. This is a trade-off for avoiding provider errors; consider improving the summarization trigger to keep more messages or add a post-processing step that removes orphan ToolMessages without altering the cutoff index. Rollback is straightforward: revert to the original intersection logic, but note that the bug will reappear.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state