From 5ee3cb9ae68ac8a58997b37cfd0989ebdc182df2 Mon Sep 17 00:00:00 2001 From: Anton Nakonechny Date: Sun, 21 Jun 2026 16:55:12 +0200 Subject: [PATCH] Fix response text extraction and update to latest Claude model - Add _extract_text() helper to safely extract text from Claude responses, handling edge cases where content may be empty or contain non-text blocks - Update ANTHROPIC_MODEL from 4-20250514 to latest stable 4.6 Co-Authored-By: Claude Haiku 4.5 --- backend/ai_generator.py | 19 ++++++++++++++++--- backend/config.py | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/backend/ai_generator.py b/backend/ai_generator.py index 0363ca90c..69f5403fd 100644 --- a/backend/ai_generator.py +++ b/backend/ai_generator.py @@ -82,9 +82,22 @@ def generate_response(self, query: str, # Handle tool execution if needed if response.stop_reason == "tool_use" and tool_manager: return self._handle_tool_execution(response, api_params, tool_manager) - + # Return direct response - return response.content[0].text + return self._extract_text(response) + + @staticmethod + def _extract_text(response) -> str: + """Safely extract text from a Claude response. + + The model can return an empty content list (e.g. an end_turn with no + text) or lead with a non-text block. Guard against both rather than + assuming content[0] is always a text block. + """ + for block in response.content: + if getattr(block, "type", None) == "text": + return block.text + return "" def _handle_tool_execution(self, initial_response, base_params: Dict[str, Any], tool_manager): """ @@ -132,4 +145,4 @@ def _handle_tool_execution(self, initial_response, base_params: Dict[str, Any], # Get final response final_response = self.client.messages.create(**final_params) - return final_response.content[0].text \ No newline at end of file + return self._extract_text(final_response) \ No newline at end of file diff --git a/backend/config.py b/backend/config.py index d9f6392ef..a3416c898 100644 --- a/backend/config.py +++ b/backend/config.py @@ -10,7 +10,7 @@ class Config: """Configuration settings for the RAG system""" # Anthropic API settings ANTHROPIC_API_KEY: str = os.getenv("ANTHROPIC_API_KEY", "") - ANTHROPIC_MODEL: str = "claude-sonnet-4-20250514" + ANTHROPIC_MODEL: str = "claude-sonnet-4-6" # Embedding model settings EMBEDDING_MODEL: str = "all-MiniLM-L6-v2"