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

Filter_messages Leaves OpenAI Function_call Blocks After Excluding Tool Calls

When filter_messages(..., exclude_tool_calls=[...]) is called on an AIMessage containing OpenAI Responses-style function_call content blocks, the excluded tool call is removed from AIMessage.tool_calls but the corresponding function_call block remains in AIMessage.content, creating an inconsistent state that may cause downstream model errors.

mediumConfidence 90%Langchain-CoreMemoryRetrieversVector StoresAffected V1.2.25

Origin Analysis

The content block filtering logic in filter_messages only removes Anthropic-style 'tool_use' blocks (identified by 'id'), but does not handle OpenAI Responses-style 'function_call' blocks (identified by 'call_id'). Therefore, excluded tool calls persist in the content list.
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage, filter_messages tool_calls = [ {'name': 'foo', 'id': '1', 'args': {}, 'type': 'tool_call'}, {'name': 'bar', 'id': '2', 'args': {}, 'type': 'tool_call'}, ] messages = [ HumanMessage('foo', name='blah', id='1'), AIMessage( [ {'text': 'bar-response', 'type': 'text'}, {'type': 'function_call', 'call_id': '1', 'name': 'foo', 'arguments': '{}'}, {'type': 'function_call', 'call_id': '2', 'name': 'bar', 'arguments': '{}'}, ], tool_calls=tool_calls, id='2', ), ToolMessage('baz', tool_call_id='1', id='3'), ToolMessage('qux', tool_call_id='2', id='4'), ] result = filter_messages(messages, exclude_tool_calls=['1']) print(result[1].tool_calls) print(result[1].content)

Fixing Code Block

from typing import Any, Sequence from langchain_core.messages import AIMessage def _filter_content_blocks_by_tool_calls( content: Any, *, include_ids: set[str] | None = None, exclude_ids: set[str] | None = None, ) -> Any: # Remove content blocks whose tool call id matches the exclusion/inclusion filters. if not isinstance(content, list): return content filtered = [] for block in content: if not isinstance(block, dict): filtered.append(block) continue block_type = block.get('type') if block_type == 'tool_use': tool_id = block.get('id') elif block_type == 'function_call': tool_id = block.get('call_id') else: filtered.append(block) continue if exclude_ids is not None and tool_id in exclude_ids: continue if include_ids is not None and tool_id not in include_ids: continue filtered.append(block) return filtered # Inside filter_messages, when processing AIMessage tool calls and the tool_calls list changes: # Assuming exclude_ids = set(exclude_tool_calls or []) and include_ids = set(include_tool_calls or []) # and message is a copy of the original AIMessage with updated tool_calls: # # message = message.model_copy(update={'tool_calls': filtered_tool_calls}) # message.content = _filter_content_blocks_by_tool_calls( # message.content, # include_ids=include_ids if include_tool_calls is not None else None, # exclude_ids=exclude_ids if exclude_tool_calls is not None else None, # ) # message.invalidate_llm_result()
The added helper recognizes both Anthropic-style 'tool_use' blocks and OpenAI Responses-style 'function_call' blocks. It uses the appropriate identifier field ('id' for tool_use, 'call_id' for function_call) to filter content blocks consistently with the message's tool_calls list. The snippet should replace the existing content filtering logic in filter_messages that only handled tool_use blocks.

Edge Case Audit

This fix may break workflows that relied on stale function_call blocks remaining in content after filtering. Rollback to langchain-core 1.2.25 if unexpected behavior occurs. The helper assumes content is a list of dicts; non-list content is passed through unchanged. When both include_tool_calls and exclude_tool_calls are provided, exclusion is applied first, followed by inclusion. Other provider-specific block types with different id fields are not handled and will remain as-is. Ensure message objects are copied with model_copy to avoid mutating the original in concurrent scenarios. Thoroughly test with mixed content blocks before deployment.

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