mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 21:19:12 +00:00
Only add cache control to last Claude text block if exists, non-empty
Otherwise Claude API throws error
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user