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

Merge_message_runs Raises For ChatMessage Roles And FunctionMessage Names That Differ

The merge_message_runs utility attempts to merge consecutive ChatMessage or FunctionMessage objects based solely on message class, leading to ValueError when their identifying fields (role or name) differ. This violates expected behavior and breaks common message processing pipelines.

highConfidence 92%LangChainAffected Vlangchain-Core 1.6.1

Origin Analysis

merge_message_runs checks only the message type (and special-cases ToolMessage.tool_call_id) but does not verify the identity fields ChatMessage.role and FunctionMessage.name before calling __add__, which rejects merging of chunks with different roles/names.
Run the following Python code: ```python from langchain_core.messages import ChatMessage, FunctionMessage, merge_message_runs chat_messages = [ ChatMessage(role="human", content="first"), ChatMessage(role="assistant", content="second"), ] print(merge_message_runs(chat_messages)) function_messages = [ FunctionMessage(name="search", content="first"), FunctionMessage(name="lookup", content="second"), ] print(merge_message_runs(function_messages)) ``` Both calls raise ValueError.

Fixing Code Block

Edge Case Audit

This change may alter the output structure for users who expected the previous (broken) behavior of raising an error; now it silently separates runs. Regression risk is low, but custom subclasses of BaseMessage with additional required identity fields may still be incorrectly merged. Rollback: if unexpected merging issues appear in other message types, revert this change and extend the field-checking logic to cover those types.

Ecosystem Topology