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: summarization drops reasoning content mid turn [LET-7039] (#8769)

cthomasc3276dda178f
diff --git a/letta/services/summarizer/summarizer.py b/letta/services/summarizer/summarizer.py
index e3d4cec52..d8566a77c 100644
--- a/letta/services/summarizer/summarizer.py
+++ b/letta/services/summarizer/summarizer.py
@@ -263,6 +263,17 @@ class Summarizer:
while target_trim_index < len(all_in_context_messages) and all_in_context_messages[target_trim_index].role != MessageRole.user:
target_trim_index += 1
+ # If the first retained message is an approval request, also keep the assistant message before it
+ # (they're part of the same LLM response - assistant has reasoning/tool_calls, approval has approval-required subset)
+ if target_trim_index < len(all_in_context_messages):
+ first_retained = all_in_context_messages[target_trim_index]
+ if first_retained.role == MessageRole.approval and target_trim_index > 1:
+ # Check if the message before it is an assistant from the same step
+ prev_message = all_in_context_messages[target_trim_index - 1]
+ if prev_message.role == MessageRole.assistant and prev_message.step_id == first_retained.step_id:
+ # Back up to include the assistant message with reasoning
+ target_trim_index -= 1
+
evicted_messages = all_in_context_messages[1:target_trim_index] # everything except sys msg
updated_in_context_messages = all_in_context_messages[target_trim_index:] # may be empty
diff --git a/letta/services/summarizer/summarizer_all.py b/letta/services/summarizer/summarizer_all.py
index b8d1fa2a8..7918f89f5 100644
--- a/letta/services/summarizer/summarizer_all.py
+++ b/letta/services/summarizer/summarizer_all.py
@@ -33,8 +33,22 @@ async def summarize_all(
)
if in_context_messages[-1].role == MessageRole.approval:
# cannot evict a pending approval request (will cause client-side errors)
- messages_to_summarize = in_context_messages[1:-1]
+ # Also protect the assistant message before it if they share the same step_id
+ # (both are part of the same LLM response - assistant has thinking/tool_calls, approval has approval-required subset)
protected_messages = [in_context_messages[-1]]
+
+ # Check if the message before approval is also from the same step (has reasoning/tool_calls)
+ if len(in_context_messages) >= 2:
+ potential_assistant = in_context_messages[-2]
+ approval_request = in_context_messages[-1]
+ if potential_assistant.role == MessageRole.assistant and potential_assistant.step_id == approval_request.step_id:
+ # They're part of the same LLM response - protect both
+ protected_messages = [potential_assistant, approval_request]
+ messages_to_summarize = in_context_messages[1:-2]
+ else:
+ messages_to_summarize = in_context_messages[1:-1]
+ else:
+ messages_to_summarize = in_context_messages[1:-1]
else:
messages_to_summarize = in_context_messages[1:]
protected_messages = []