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.

Add fix for anthropic streaming </thinking tag (#2621)

Will Sargentdf6b04a6409f
diff --git a/letta/interfaces/anthropic_streaming_interface.py b/letta/interfaces/anthropic_streaming_interface.py
index 974673f84..179ff11d0 100644
--- a/letta/interfaces/anthropic_streaming_interface.py
+++ b/letta/interfaces/anthropic_streaming_interface.py
@@ -83,6 +83,9 @@ class AnthropicStreamingInterface:
self.inner_thoughts_complete = False
self.put_inner_thoughts_in_kwarg = put_inner_thoughts_in_kwarg
+ # Buffer to handle partial XML tags across chunks
+ self.partial_tag_buffer = ""
+
def get_tool_call_object(self) -> ToolCall:
"""Useful for agent loop"""
return ToolCall(id=self.tool_call_id, function=FunctionCall(arguments=self.accumulated_tool_call_args, name=self.tool_call_name))
@@ -154,8 +157,22 @@ class AnthropicStreamingInterface:
f"Streaming integrity failed - received BetaTextDelta object while not in TEXT EventMode: {delta}"
)
- # TODO: Strip out </thinking> more robustly, this is pretty hacky lol
- delta.text = delta.text.replace("</thinking>", "")
+ # Combine buffer with current text to handle tags split across chunks
+ combined_text = self.partial_tag_buffer + delta.text
+
+ # Remove all occurrences of </thinking> tag
+ cleaned_text = combined_text.replace("</thinking>", "")
+
+ # Extract just the new content (without the buffer part)
+ if len(self.partial_tag_buffer) <= len(cleaned_text):
+ delta.text = cleaned_text[len(self.partial_tag_buffer) :]
+ else:
+ # Edge case: the tag was removed and now the text is shorter than the buffer
+ delta.text = ""
+
+ # Store the last 10 characters (or all if less than 10) for the next chunk
+ # This is enough to catch "</thinking" which is 10 characters
+ self.partial_tag_buffer = combined_text[-10:] if len(combined_text) > 10 else combined_text
self.accumulated_inner_thoughts.append(delta.text)
reasoning_message = ReasoningMessage(
diff --git a/letta/schemas/llm_config.py b/letta/schemas/llm_config.py
index 4bc0e236a..dc0f4f58c 100644
--- a/letta/schemas/llm_config.py
+++ b/letta/schemas/llm_config.py
@@ -75,7 +75,8 @@ class LLMConfig(BaseModel):
description="The reasoning effort to use when generating text reasoning models",
)
max_reasoning_tokens: int = Field(
- 0, description="Configurable thinking budget for extended thinking. Used for enable_reasoner and also for Google Vertex models like Gemini 2.5 Flash. Minimum value is 1024 when used with enable_reasoner."
+ 0,
+ description="Configurable thinking budget for extended thinking. Used for enable_reasoner and also for Google Vertex models like Gemini 2.5 Flash. Minimum value is 1024 when used with enable_reasoner.",
)
# FIXME hack to silence pydantic protected namespace warning
diff --git a/tests/integration_test_sleeptime_agent.py b/tests/integration_test_sleeptime_agent.py
index 1d6da8582..205ef619c 100644
--- a/tests/integration_test_sleeptime_agent.py
+++ b/tests/integration_test_sleeptime_agent.py
@@ -155,7 +155,7 @@ async def test_sleeptime_group_chat(server, actor):
# 6. Verify run status after sleep
time.sleep(2)
-
+
for run_id in run_ids:
job = server.job_manager.get_job_by_id(job_id=run_id, actor=actor)
assert job.status == JobStatus.running or job.status == JobStatus.completed