Skip to content

The agent engineering platform.

refactor(core): select chunk_id via ranking and remove extra allocation (#34588)

Andre Roelofsc25f3847d0ca
diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.py
index b582b11329..499b26bba7 100644
--- a/libs/core/langchain_core/messages/ai.py
+++ b/libs/core/langchain_core/messages/ai.py
@@ -1,5 +1,6 @@
"""AI message."""
+import itertools
import json
import logging
import operator
@@ -651,29 +652,28 @@ def add_ai_message_chunks(
else:
usage_metadata = None
+ # Ranks are defined by the order of preference. Higher is better:
+ # 2. Provider-assigned IDs (non lc_* and non lc_run-*)
+ # 1. lc_run-* IDs
+ # 0. lc_* and other remaining IDs
+ best_rank = -1
chunk_id = None
- candidates = [left.id] + [o.id for o in others]
- # first pass: pick the first provider-assigned id (non-run-* and non-lc_*)
+ candidates = itertools.chain([left.id], (o.id for o in others))
+
for id_ in candidates:
- if (
- id_
- and not id_.startswith(LC_ID_PREFIX)
- and not id_.startswith(LC_AUTO_PREFIX)
- ):
+ if not id_:
+ continue
+
+ if not id_.startswith(LC_ID_PREFIX) and not id_.startswith(LC_AUTO_PREFIX):
chunk_id = id_
+ # Highest rank, return instantly
break
- else:
- # second pass: prefer lc_run-* IDs over lc_* IDs
- for id_ in candidates:
- if id_ and id_.startswith(LC_ID_PREFIX):
- chunk_id = id_
- break
- else:
- # third pass: take any remaining ID (auto-generated lc_* IDs)
- for id_ in candidates:
- if id_:
- chunk_id = id_
- break
+
+ rank = 1 if id_.startswith(LC_ID_PREFIX) else 0
+
+ if rank > best_rank:
+ best_rank = rank
+ chunk_id = id_
chunk_position: Literal["last"] | None = (
"last" if any(x.chunk_position == "last" for x in [left, *others]) else None