-
Notifications
You must be signed in to change notification settings - Fork 10
fix(store): honor limit parameter in Redis search operations (#30) #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Tests for AsyncRedisStore search limits.""" | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
import pytest_asyncio | ||
|
||
from langgraph.store.redis import AsyncRedisStore | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def async_store(redis_url) -> AsyncRedisStore: | ||
"""Fixture to create an AsyncRedisStore.""" | ||
async with AsyncRedisStore(redis_url) as store: | ||
await store.setup() # Initialize indices | ||
yield store | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_search_with_larger_limit(async_store: AsyncRedisStore) -> None: | ||
"""Test async search with limit > 10.""" | ||
# Create 15 test documents | ||
for i in range(15): | ||
await async_store.aput( | ||
("test_namespace",), f"key{i}", {"data": f"value{i}", "index": i} | ||
) | ||
|
||
# Search with a limit of 15 | ||
results = await async_store.asearch(("test_namespace",), limit=15) | ||
|
||
# Should return all 15 results | ||
assert len(results) == 15, f"Expected 15 results, got {len(results)}" | ||
|
||
# Verify we have all the items | ||
result_keys = {item.key for item in results} | ||
expected_keys = {f"key{i}" for i in range(15)} | ||
assert result_keys == expected_keys | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_vector_search_with_larger_limit(redis_url) -> None: | ||
"""Test async vector search with limit > 10.""" | ||
from tests.embed_test_utils import CharacterEmbeddings | ||
|
||
# Create vector store with embeddings | ||
embeddings = CharacterEmbeddings(dims=4) | ||
index_config = { | ||
"dims": embeddings.dims, | ||
"embed": embeddings, | ||
"distance_type": "cosine", | ||
"fields": ["text"], | ||
} | ||
|
||
async with AsyncRedisStore(redis_url, index=index_config) as store: | ||
await store.setup() | ||
|
||
# Create 15 test documents | ||
for i in range(15): | ||
# Create documents with slightly different texts | ||
await store.aput( | ||
("test_namespace",), f"key{i}", {"text": f"sample text {i}", "index": i} | ||
) | ||
|
||
# Search with a limit of 15 | ||
results = await store.asearch(("test_namespace",), query="sample", limit=15) | ||
|
||
# Should return all 15 results | ||
assert len(results) == 15, f"Expected 15 results, got {len(results)}" | ||
|
||
# Verify we have all the items | ||
result_keys = {item.key for item in results} | ||
expected_keys = {f"key{i}" for i in range(15)} | ||
assert result_keys == expected_keys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Tests for RedisStore search limits.""" | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from langgraph.store.redis import RedisStore | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def store(redis_url) -> RedisStore: | ||
"""Fixture to create a Redis store.""" | ||
with RedisStore.from_conn_string(redis_url) as store: | ||
store.setup() # Initialize indices | ||
yield store | ||
|
||
|
||
def test_search_with_larger_limit(store: RedisStore) -> None: | ||
"""Test search with limit > 10.""" | ||
# Create 15 test documents | ||
for i in range(15): | ||
store.put(("test_namespace",), f"key{i}", {"data": f"value{i}", "index": i}) | ||
|
||
# Search with a limit of 15 | ||
results = store.search(("test_namespace",), limit=15) | ||
|
||
# Should return all 15 results | ||
assert len(results) == 15, f"Expected 15 results, got {len(results)}" | ||
|
||
# Verify we have all the items | ||
result_keys = {item.key for item in results} | ||
expected_keys = {f"key{i}" for i in range(15)} | ||
assert result_keys == expected_keys | ||
|
||
|
||
def test_vector_search_with_larger_limit(redis_url) -> None: | ||
"""Test vector search with limit > 10.""" | ||
from tests.embed_test_utils import CharacterEmbeddings | ||
|
||
# Create vector store with embeddings | ||
embeddings = CharacterEmbeddings(dims=4) | ||
index_config = { | ||
"dims": embeddings.dims, | ||
"embed": embeddings, | ||
"distance_type": "cosine", | ||
"fields": ["text"], | ||
} | ||
|
||
with RedisStore.from_conn_string(redis_url, index=index_config) as store: | ||
store.setup() | ||
|
||
# Create 15 test documents | ||
for i in range(15): | ||
# Create documents with slightly different texts | ||
store.put( | ||
("test_namespace",), f"key{i}", {"text": f"sample text {i}", "index": i} | ||
) | ||
|
||
# Search with a limit of 15 | ||
results = store.search(("test_namespace",), query="sample", limit=15) | ||
|
||
# Should return all 15 results | ||
assert len(results) == 15, f"Expected 15 results, got {len(results)}" | ||
|
||
# Verify we have all the items | ||
result_keys = {item.key for item in results} | ||
expected_keys = {f"key{i}" for i in range(15)} | ||
assert result_keys == expected_keys |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.