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%LangChainAgentsRetrieversChat ModelsAffected 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

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