From 688397bbd24d129ab0c8bf954e1ca4000a300b33 Mon Sep 17 00:00:00 2001 From: Kian Jones <11655409+kianjones9@users.noreply.github.com> Date: Wed, 5 Nov 2025 14:39:05 -0800 Subject: [PATCH] fix: filter out messages with empty content in anthropic client (#5987) * no bare raise in anthropic client. Also filter out messages with empty content * use client logic * replace empty/whitespace with period * cursor bugbot --- letta/llm_api/anthropic_client.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/letta/llm_api/anthropic_client.py b/letta/llm_api/anthropic_client.py index 7990f778..37542284 100644 --- a/letta/llm_api/anthropic_client.py +++ b/letta/llm_api/anthropic_client.py @@ -430,6 +430,30 @@ class AnthropicClient(LLMClientBase): # Handle XML-style thinking in string content thinking_enabled = True + # Replace empty content with placeholder (Anthropic requires non-empty content except for final assistant message) + if messages_for_counting: + for i, msg in enumerate(messages_for_counting): + content = msg.get("content") + is_final_assistant = i == len(messages_for_counting) - 1 and msg.get("role") == "assistant" + + # Check if content is empty and needs replacement + if content is None: + if not is_final_assistant: + msg["content"] = "." + elif isinstance(content, str) and not content.strip(): + if not is_final_assistant: + msg["content"] = "." + elif isinstance(content, list): + if len(content) == 0: + if not is_final_assistant: + msg["content"] = [{"type": "text", "text": "."}] + elif not is_final_assistant: + # Replace empty text blocks with placeholder (only if not final assistant message) + for block in content: + if isinstance(block, dict) and block.get("type") == "text": + if not block.get("text", "").strip(): + block["text"] = "." + try: count_params = { "model": model or "claude-3-7-sonnet-20250219", @@ -458,8 +482,8 @@ class AnthropicClient(LLMClientBase): result = await client.beta.messages.count_tokens(**count_params, betas=betas) else: result = await client.beta.messages.count_tokens(**count_params) - except: - raise + except Exception as e: + raise self.handle_llm_error(e) token_count = result.input_tokens if messages is None: