From 5dd8a9cb24b64e7bbaa3f7dfbaff8b32d333d215 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Sat, 7 Jun 2025 19:57:08 -0700 Subject: [PATCH] Only add cache control to last Claude text block if exists, non-empty Otherwise Claude API throws error --- src/khoj/processor/conversation/anthropic/utils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/khoj/processor/conversation/anthropic/utils.py b/src/khoj/processor/conversation/anthropic/utils.py index ed958e36..796a90da 100644 --- a/src/khoj/processor/conversation/anthropic/utils.py +++ b/src/khoj/processor/conversation/anthropic/utils.py @@ -309,8 +309,15 @@ def format_messages_for_anthropic(messages: list[ChatMessage], system_prompt: st # Caching it should improve research efficiency. cache_message = messages[-2] if isinstance(cache_message.content, list) and cache_message.content: - # Add cache control to the last content block - cache_message.content[-1]["cache_control"] = {"type": "ephemeral"} + # Add cache control to the last content block only if it's a text block with non-empty content + last_block = cache_message.content[-1] + if ( + isinstance(last_block, dict) + and last_block.get("type") == "text" + and last_block.get("text") + and last_block.get("text").strip() + ): + last_block["cache_control"] = {"type": "ephemeral"} formatted_messages: List[anthropic.types.MessageParam] = [ anthropic.types.MessageParam(role=message.role, content=message.content) for message in messages