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

Core: Message Chunk Addition Drops 'Name' And Fallback 'Id', Breaking Streaming Multi-Agent Metadata And Merge_message_runs

In langchain_core, concatenating message chunks with '+' and the merge_message_runs function silently discard the 'name' attribute and fail to fallback to the trailing chunk's 'id' when the leading chunk's id is None, causing loss of metadata during streaming and in multi-agent scenarios.

highConfidence 88%LangchainAffected V1.4.8

Origin Analysis

The __add__ method on BaseMessage (and inherited by chunk classes) constructs a new message instance without passing the 'name' field, and the 'id' field is set using only self.id instead of self.id or other.id, so when self.id is None the other.id is ignored. The merge_message_runs utility similarly drops 'name' and mishandles 'id' during merging of consecutive messages of the same type.
1. Run the following Python code: from langchain_core.messages import AIMessageChunk, HumanMessageChunk, merge_message_runs, HumanMessage; ai = AIMessageChunk(content='Hello', name='assistant') + AIMessageChunk(content=' world'); print(ai.name) # Actual: None; merged = merge_message_runs([HumanMessage('Hello', name='alice'), HumanMessage(' world', name='alice')]); print(merged[0].name) # Actual: None. 2. Observe that the printed name is None instead of 'assistant' and 'alice' respectively. 3. Additionally, test id fallback by creating two chunks where the first has id=None and the second has id='123', then add them; the result's id will be None instead of '123'.

Fixing Code Block

Edge Case Audit

This monkey-patch overrides core LangChain methods and may conflict with future library updates or other monkey patches. It assumes all message classes use Pydantic's model_copy for updates; if a subclass overrides __add__ without calling super(), the patch may not apply. Users should verify that merging messages with different non-None names still yields the leading message's name (reasonable expectation). For rollback, simply remove the monkey-patch code and restart the application. It is strongly recommended to upgrade to an official LangChain release that fixes this issue and remove this patch once available. Concurrency and multithreading are not directly impacted as these are immutable operations, but global patch application should be done at import time to avoid race conditions.

Ecosystem Topology