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: preserve Gemini thought_signature on function calls without reasoning (LET-8166) (#10237)

Camerone83f196eea1c
diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.py
index 1086d7c9c..9475e1f5e 100644
--- a/letta/agents/letta_agent.py
+++ b/letta/agents/letta_agent.py
@@ -380,8 +380,15 @@ class LettaAgent(BaseAgent):
)
] # reasoning placed into content for legacy reasons
else:
- self.logger.info("No reasoning content found.")
- reasoning = None
+ # Preserve thought_signature even when there's no reasoning text.
+ # Gemini requires the signature on all function call parts in history;
+ # dropping it causes 400 INVALID_ARGUMENT on the next request.
+ sig = response.choices[0].message.reasoning_content_signature
+ if sig:
+ reasoning = [TextContent(text="", signature=sig)]
+ else:
+ self.logger.info("No reasoning content found.")
+ reasoning = None
persisted_messages, should_continue, stop_reason = await self._handle_ai_response(
tool_call,
@@ -721,8 +728,15 @@ class LettaAgent(BaseAgent):
elif response.choices[0].message.omitted_reasoning_content:
reasoning = [OmittedReasoningContent()]
else:
- self.logger.info("No reasoning content found.")
- reasoning = None
+ # Preserve thought_signature even when there's no reasoning text.
+ # Gemini requires the signature on all function call parts in history;
+ # dropping it causes 400 INVALID_ARGUMENT on the next request.
+ sig = response.choices[0].message.reasoning_content_signature
+ if sig:
+ reasoning = [TextContent(text="", signature=sig)]
+ else:
+ self.logger.info("No reasoning content found.")
+ reasoning = None
persisted_messages, should_continue, stop_reason = await self._handle_ai_response(
tool_call,
diff --git a/letta/server/rest_api/utils.py b/letta/server/rest_api/utils.py
index fd4ebf35c..b9712f7ef 100644
--- a/letta/server/rest_api/utils.py
+++ b/letta/server/rest_api/utils.py
@@ -424,7 +424,7 @@ def create_letta_messages_from_llm_response(
content = []
if reasoning_content:
for content_part in reasoning_content:
- if isinstance(content_part, TextContent) and content_part.text == "":
+ if isinstance(content_part, TextContent) and content_part.text == "" and content_part.signature is None:
continue
content.append(content_part)
@@ -444,7 +444,7 @@ def create_letta_messages_from_llm_response(
content = []
if reasoning_content:
for content_part in reasoning_content:
- if isinstance(content_part, TextContent) and content_part.text == "":
+ if isinstance(content_part, TextContent) and content_part.text == "" and content_part.signature is None:
continue
content.append(content_part)
@@ -572,7 +572,7 @@ def create_parallel_tool_messages_from_llm_response(
] = []
if reasoning_content:
for content_part in reasoning_content:
- if isinstance(content_part, TextContent) and content_part.text == "":
+ if isinstance(content_part, TextContent) and content_part.text == "" and content_part.signature is None:
continue
content.append(content_part)
diff --git a/tests/test_message_serialization.py b/tests/test_message_serialization.py
index 23992f396..0d4706939 100644
--- a/tests/test_message_serialization.py
+++ b/tests/test_message_serialization.py
@@ -80,3 +80,49 @@ def test_to_google_dict_falls_back_for_malformed_tool_call_arguments():
function_calls = [item for item in serialized["parts"] if item.get("functionCall")]
assert len(function_calls) == 1
assert function_calls[0]["functionCall"]["args"] == {"_malformed_tool_arguments": malformed_args}
+
+
+def test_to_google_dict_preserves_thought_signature_on_empty_content():
+ """When Gemini returns a function call without reasoning text, the
+ thought_signature must still appear on the serialized functionCall part.
+ Regression test for LET-8166 / GitHub #3221."""
+ sig = "EoQHsomebase64signaturedata=="
+ msg = Message(
+ role=MessageRole.assistant,
+ content=[TextContent(text="", signature=sig)],
+ tool_calls=[
+ ChatCompletionMessageToolCall(
+ id="call_test_thought_sig",
+ type="function",
+ function=Function(name="archival_memory_search", arguments='{"query": "test"}'),
+ )
+ ],
+ )
+
+ serialized = msg.to_google_dict(current_model="google/gemini-3-flash")
+
+ function_calls = [p for p in serialized["parts"] if "functionCall" in p]
+ assert len(function_calls) == 1
+ assert function_calls[0].get("thought_signature") == sig
+
+
+def test_to_google_dict_no_signature_when_absent():
+ """Without a signature, functionCall parts should not include
+ thought_signature (no sentinel, no empty string)."""
+ msg = Message(
+ role=MessageRole.assistant,
+ content=[],
+ tool_calls=[
+ ChatCompletionMessageToolCall(
+ id="call_test_no_sig",
+ type="function",
+ function=Function(name="send_message", arguments='{"message": "hi"}'),
+ )
+ ],
+ )
+
+ serialized = msg.to_google_dict(current_model="google/gemini-3-flash")
+
+ function_calls = [p for p in serialized["parts"] if "functionCall" in p]
+ assert len(function_calls) == 1
+ assert "thought_signature" not in function_calls[0]