Skip to content

Commit 3011f52

Browse files
committed
Fix RedisVL API format change - handle both dict and object results
- RedisVL now returns dictionaries instead of objects with attributes - Handle both old format (result.vector_score) and new format (result['vector_score']) - This fixes AttributeError: 'dict' object has no attribute 'vector_score' - Another real integration issue caught by proper testing
1 parent 2f014d5 commit 3011f52

File tree

1 file changed

+30
-8
lines changed
  • python-recipes/context-engineering/reference-agent/redis_context_course

1 file changed

+30
-8
lines changed

python-recipes/context-engineering/reference-agent/redis_context_course/memory.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,37 @@ async def retrieve_memories(
145145
# Handle both old and new RedisVL API formats
146146
docs = results.docs if hasattr(results, 'docs') else results
147147
for result in docs:
148-
if result.vector_score >= similarity_threshold:
148+
# Handle both object and dictionary formats
149+
if isinstance(result, dict):
150+
# New API returns dictionaries
151+
vector_score = result.get('vector_score', 1.0)
152+
result_id = result.get('id')
153+
student_id = result.get('student_id')
154+
content = result.get('content')
155+
memory_type = result.get('memory_type')
156+
importance = result.get('importance', 0.5)
157+
created_at = result.get('created_at')
158+
metadata = result.get('metadata', '{}')
159+
else:
160+
# Old API returns objects with attributes
161+
vector_score = result.vector_score
162+
result_id = result.id
163+
student_id = result.student_id
164+
content = result.content
165+
memory_type = result.memory_type
166+
importance = result.importance
167+
created_at = result.created_at
168+
metadata = result.metadata
169+
170+
if vector_score >= similarity_threshold:
149171
memory = ConversationMemory(
150-
id=result.id,
151-
student_id=result.student_id,
152-
content=result.content,
153-
memory_type=result.memory_type,
154-
importance=float(result.importance),
155-
created_at=datetime.fromtimestamp(float(result.created_at)),
156-
metadata=json.loads(result.metadata) if result.metadata else {}
172+
id=result_id,
173+
student_id=student_id,
174+
content=content,
175+
memory_type=memory_type,
176+
importance=float(importance),
177+
created_at=datetime.fromtimestamp(float(created_at)),
178+
metadata=json.loads(metadata) if metadata else {}
157179
)
158180
memories.append(memory)
159181

0 commit comments

Comments
 (0)