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: prevent empty reasoning messages in streaming interfaces (#7207)

Kian Jones82e5d7080791
diff --git a/letta/interfaces/anthropic_parallel_tool_call_streaming_interface.py b/letta/interfaces/anthropic_parallel_tool_call_streaming_interface.py
index 58d84517e..ffe6ac639 100644
--- a/letta/interfaces/anthropic_parallel_tool_call_streaming_interface.py
+++ b/letta/interfaces/anthropic_parallel_tool_call_streaming_interface.py
@@ -88,6 +88,7 @@ class SimpleAnthropicStreamingInterface:
self.tool_call_name = None
self.accumulated_tool_call_args = ""
self.previous_parse = {}
+ self.thinking_signature = None
# usage trackers
self.input_tokens = 0
@@ -426,20 +427,23 @@ class SimpleAnthropicStreamingInterface:
f"Streaming integrity failed - received BetaThinkingBlock object while not in THINKING EventMode: {delta}"
)
- if prev_message_type and prev_message_type != "reasoning_message":
- message_index += 1
- reasoning_message = ReasoningMessage(
- id=self.letta_message_id,
- source="reasoner_model",
- reasoning=delta.thinking,
- date=datetime.now(timezone.utc).isoformat(),
- otid=Message.generate_otid_from_id(self.letta_message_id, message_index),
- run_id=self.run_id,
- step_id=self.step_id,
- )
- self.reasoning_messages.append(reasoning_message)
- prev_message_type = reasoning_message.message_type
- yield reasoning_message
+ # Only emit reasoning message if we have actual content
+ if delta.thinking and delta.thinking.strip():
+ if prev_message_type and prev_message_type != "reasoning_message":
+ message_index += 1
+ reasoning_message = ReasoningMessage(
+ id=self.letta_message_id,
+ source="reasoner_model",
+ reasoning=delta.thinking,
+ signature=self.thinking_signature,
+ date=datetime.now(timezone.utc).isoformat(),
+ otid=Message.generate_otid_from_id(self.letta_message_id, message_index),
+ run_id=self.run_id,
+ step_id=self.step_id,
+ )
+ self.reasoning_messages.append(reasoning_message)
+ prev_message_type = reasoning_message.message_type
+ yield reasoning_message
elif isinstance(delta, BetaSignatureDelta):
# Safety check
@@ -448,21 +452,15 @@ class SimpleAnthropicStreamingInterface:
f"Streaming integrity failed - received BetaSignatureDelta object while not in THINKING EventMode: {delta}"
)
- if prev_message_type and prev_message_type != "reasoning_message":
- message_index += 1
- reasoning_message = ReasoningMessage(
- id=self.letta_message_id,
- source="reasoner_model",
- reasoning="",
- date=datetime.now(timezone.utc).isoformat(),
- signature=delta.signature,
- otid=Message.generate_otid_from_id(self.letta_message_id, message_index),
- run_id=self.run_id,
- step_id=self.step_id,
- )
- self.reasoning_messages.append(reasoning_message)
- prev_message_type = reasoning_message.message_type
- yield reasoning_message
+ # Store signature but don't emit empty reasoning message
+ # Signature will be attached when actual thinking content arrives
+ self.thinking_signature = delta.signature
+
+ # Update the last reasoning message with the signature so it gets persisted
+ if self.reasoning_messages:
+ last_msg = self.reasoning_messages[-1]
+ if isinstance(last_msg, ReasoningMessage):
+ last_msg.signature = delta.signature
elif isinstance(event, BetaRawMessageStartEvent):
self.message_id = event.message.id
diff --git a/letta/interfaces/gemini_streaming_interface.py b/letta/interfaces/gemini_streaming_interface.py
index 15b84c507..91fbb5023 100644
--- a/letta/interfaces/gemini_streaming_interface.py
+++ b/letta/interfaces/gemini_streaming_interface.py
@@ -224,40 +224,32 @@ class SimpleGeminiStreamingInterface:
# NOTE: the thought_signature comes on the Part with the function_call
thought_signature = part.thought_signature
self.thinking_signature = base64.b64encode(thought_signature).decode("utf-8")
- if prev_message_type and prev_message_type != "reasoning_message":
- message_index += 1
- yield ReasoningMessage(
- id=self.letta_message_id,
- date=datetime.now(timezone.utc).isoformat(),
- otid=Message.generate_otid_from_id(self.letta_message_id, message_index),
- source="reasoner_model",
- reasoning="",
- signature=self.thinking_signature,
- )
- prev_message_type = "reasoning_message"
+ # Don't emit empty reasoning message - signature will be attached to actual reasoning content
# Thinking summary content part (bool means text is thought part)
if part.thought:
reasoning_summary = part.text
- if prev_message_type and prev_message_type != "reasoning_message":
- message_index += 1
- yield ReasoningMessage(
- id=self.letta_message_id,
- date=datetime.now(timezone.utc).isoformat(),
- otid=Message.generate_otid_from_id(self.letta_message_id, message_index),
- source="reasoner_model",
- reasoning=reasoning_summary,
- run_id=self.run_id,
- step_id=self.step_id,
- )
- prev_message_type = "reasoning_message"
- self.content_parts.append(
- ReasoningContent(
- is_native=True,
+ # Only emit reasoning message if we have actual content
+ if reasoning_summary and reasoning_summary.strip():
+ if prev_message_type and prev_message_type != "reasoning_message":
+ message_index += 1
+ yield ReasoningMessage(
+ id=self.letta_message_id,
+ date=datetime.now(timezone.utc).isoformat(),
+ otid=Message.generate_otid_from_id(self.letta_message_id, message_index),
+ source="reasoner_model",
reasoning=reasoning_summary,
- signature=self.thinking_signature,
+ run_id=self.run_id,
+ step_id=self.step_id,
+ )
+ prev_message_type = "reasoning_message"
+ self.content_parts.append(
+ ReasoningContent(
+ is_native=True,
+ reasoning=reasoning_summary,
+ signature=self.thinking_signature,
+ )
)
- )
# Plain text content part
elif part.text:
diff --git a/letta/schemas/message.py b/letta/schemas/message.py
index 904b64a05..d09360dc7 100644
--- a/letta/schemas/message.py
+++ b/letta/schemas/message.py
@@ -1639,13 +1639,13 @@ class Message(BaseMessage):
# TextContent, ImageContent, ToolCallContent, ToolReturnContent, ReasoningContent, RedactedReasoningContent, OmittedReasoningContent
if isinstance(content_part, ReasoningContent):
if current_model == self.model:
- content.append(
- {
- "type": "thinking",
- "thinking": content_part.reasoning,
- "signature": content_part.signature,
- }
- )
+ block = {
+ "type": "thinking",
+ "thinking": content_part.reasoning,
+ }
+ if content_part.signature:
+ block["signature"] = content_part.signature
+ content.append(block)
elif isinstance(content_part, RedactedReasoningContent):
if current_model == self.model:
content.append(
@@ -1671,13 +1671,13 @@ class Message(BaseMessage):
for content_part in self.content:
if isinstance(content_part, ReasoningContent):
if current_model == self.model:
- content.append(
- {
- "type": "thinking",
- "thinking": content_part.reasoning,
- "signature": content_part.signature,
- }
- )
+ block = {
+ "type": "thinking",
+ "thinking": content_part.reasoning,
+ }
+ if content_part.signature:
+ block["signature"] = content_part.signature
+ content.append(block)
if isinstance(content_part, RedactedReasoningContent):
if current_model == self.model:
content.append(