Compare commits

...
Author SHA1 Message Date
Sera 2725d056f0 fix(langgraph): give message objects with an empty id a new id
ensure_message_ids treats an empty id as missing for dicts, strings and
tuples, but kept id="" on message objects. Two such messages in one
write collapsed into one in _messages_delta_reducer.

Fixes LG-951
2026-11-23 15:34:05 -05:00
2 changed files with 27 additions and 1 deletions

No files matched your search

+1 -1
View File
@@ -621,5 +621,5 @@ def _message_with_id(item: Any, *, strict: bool = False) -> Any:
def _stamp_message_id(item: Any) -> None:
if isinstance(item, BaseMessage) and item.id is None:
if isinstance(item, BaseMessage) and not item.id:
item.id = str(uuid4())
@@ -173,6 +173,7 @@ def test_delta_channel_message_in_an_overwrite_gets_stable_id(container: type) -
MESSAGE_SHAPES = [
pytest.param(lambda: [HumanMessage("hello")], id="message"),
pytest.param(lambda: HumanMessage("hello"), id="bare message"),
pytest.param(lambda: [HumanMessage("hello", id="")], id="message, empty id"),
pytest.param(lambda: [{"role": "user", "content": "hello"}], id="dict"),
pytest.param(lambda: {"type": "human", "content": "hello"}, id="bare dict"),
pytest.param(
@@ -223,6 +224,31 @@ def test_node_message_of_any_shape_gets_stable_id(write: Any) -> None:
assert first == second, "its id must be the same on every read"
@pytest.mark.parametrize(
"write",
[
pytest.param(
lambda: [HumanMessage("a", id=""), HumanMessage("b", id="")],
id="messages",
),
pytest.param(
lambda: Overwrite([HumanMessage("a", id=""), HumanMessage("b", id="")]),
id="Overwrite",
),
],
)
def test_messages_with_empty_ids_in_one_write_get_distinct_ids(write: Any) -> None:
graph = _build_graph(InMemorySaver(), reducer=_messages_delta_reducer, write=write)
config = {"configurable": {"thread_id": "t"}}
graph.invoke({"messages": []}, config)
messages = graph.get_state(config).values["messages"]
assert [m.content for m in messages] == ["a", "b"]
assert all(m.id for m in messages), "each message should get a UUID"
assert messages[0].id != messages[1].id
def test_a_list_written_twice_adds_its_messages_twice() -> None:
turn = [("user", "hello")]
graph = _build_graph(InMemorySaver(), reducer=_messages_delta_reducer)