Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: ensure messages are in chronological order when backfilling tool call id (#5520)

jnjpng5913730992e1
diff --git a/letta/services/message_manager.py b/letta/services/message_manager.py
index e02969695..f457e75e6 100644
--- a/letta/services/message_manager.py
+++ b/letta/services/message_manager.py
@@ -41,6 +41,25 @@ def backfill_missing_tool_call_ids(messages: list, agent_id: Optional[str] = Non
from letta.schemas.message import Message as PydanticMessage
+ # Check if messages are ordered chronologically (oldest first)
+ # If not, reverse the list to ensure proper chronological order
+ was_reversed = False
+ if len(messages) > 1:
+ first_msg = messages[0]
+ last_msg = messages[-1]
+
+ # Only check PydanticMessage objects that have created_at
+ if (
+ isinstance(first_msg, PydanticMessage)
+ and isinstance(last_msg, PydanticMessage)
+ and hasattr(first_msg, "created_at")
+ and hasattr(last_msg, "created_at")
+ ):
+ # If first message is newer than last message, list is reversed
+ if first_msg.created_at > last_msg.created_at:
+ was_reversed = True
+ messages.reverse()
+
updated_messages = []
last_tool_call_id = None
backfilled_count = 0
@@ -92,6 +111,9 @@ def backfill_missing_tool_call_ids(messages: list, agent_id: Optional[str] = Non
f"Backfilled {backfilled_count} missing tool_call_ids for historical messages (oct 1-6, 2025 bug) - {agent_info}, {actor_info}"
)
+ if was_reversed:
+ updated_messages.reverse()
+
return updated_messages