@@ -921,17 +921,23 @@ async def search_memories(
921
921
return_fields = return_fields ,
922
922
filter_expression = redis_filter ,
923
923
distance_threshold = float (distance_threshold ),
924
- k = limit + offset ,
924
+ k = limit ,
925
925
)
926
926
else :
927
927
vq = VectorQuery (
928
928
vector = embedding_vector ,
929
929
vector_field_name = "vector" ,
930
930
return_fields = return_fields ,
931
931
filter_expression = redis_filter ,
932
- k = limit + offset ,
932
+ k = limit ,
933
933
)
934
934
935
+ # Apply RedisVL paging instead of manual slicing
936
+ from contextlib import suppress
937
+
938
+ with suppress (Exception ):
939
+ vq .paging (offset , limit )
940
+
935
941
# Execute via AsyncSearchIndex if available
936
942
if hasattr (index , "asearch" ):
937
943
raw = await index .asearch (vq )
@@ -942,9 +948,7 @@ async def search_memories(
942
948
docs = getattr (raw , "docs" , raw ) or []
943
949
944
950
memory_results : list [MemoryRecordResult ] = []
945
- for i , doc in enumerate (docs ):
946
- if i < offset :
947
- continue
951
+ for doc in docs :
948
952
fields = (
949
953
getattr (doc , "fields" , None )
950
954
or getattr (doc , "__dict__" , {})
@@ -1029,14 +1033,11 @@ async def search_memories(
1029
1033
except Exception :
1030
1034
pass
1031
1035
1032
- next_offset = (
1033
- offset + limit
1034
- if (len (docs ) if docs else 0 ) > offset + limit
1035
- else None
1036
- )
1036
+ total_docs = len (docs ) if docs else 0
1037
+ next_offset = offset + limit if total_docs == limit else None
1037
1038
return MemoryRecordResults (
1038
1039
memories = memory_results [:limit ],
1039
- total = ( len ( docs ) if docs else 0 ) ,
1040
+ total = offset + total_docs ,
1040
1041
next_offset = next_offset ,
1041
1042
)
1042
1043
except Exception as e :
0 commit comments