From 5a14284d628080aa14f3e2c80604b7888b63695e Mon Sep 17 00:00:00 2001 From: Bob Roberts Date: Mon, 27 Oct 2025 20:11:54 -0500 Subject: [PATCH] Fix TextNode score handling - store in metadata instead MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TextNode doesn't support a direct score attribute in this version of llama-index. Instead, store the similarity score in the node's metadata during creation and retrieve it from metadata during response formatting. This ensures full compatibility with Pydantic validation and prevents attribute errors. 🤖 Generated with Claude Code Co-Authored-By: Claude --- app/core/rag_engine.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/core/rag_engine.py b/app/core/rag_engine.py index 7debeea..8bf9a2b 100644 --- a/app/core/rag_engine.py +++ b/app/core/rag_engine.py @@ -58,11 +58,15 @@ class SimpleVectorRetriever: # Convert distance back to similarity score (1 - distance) similarity = float(1 - distance) # Convert numpy float to Python float + # Store similarity score in metadata since TextNode doesn't have a score attribute + if metadata is None: + metadata = {} + metadata["similarity_score"] = similarity + node = TextNode( text=content, id_=doc_id, - metadata=metadata, - score=similarity # Pass score during initialization for Pydantic validation + metadata=metadata ) nodes.append(node) @@ -318,14 +322,18 @@ class RAGEngine: """Format query response for return.""" sources = [] for node in source_nodes: - # Convert score to Python float for JSON serialization - score = node.score if hasattr(node, "score") else None + # Retrieve score from metadata (stored during node creation) + metadata = node.metadata if hasattr(node, "metadata") else {} + score = metadata.get("similarity_score") if metadata else None if score is not None: score = float(score) + # Return metadata without the internal similarity_score + display_metadata = {k: v for k, v in metadata.items() if k != "similarity_score"} if metadata else {} + sources.append({ "text": node.text, - "metadata": node.metadata if hasattr(node, "metadata") else {}, + "metadata": display_metadata, "score": score })