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.
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 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.