mirror of
https://github.com/langchain-ai/langchain-postgres.git
synced 2026-07-16 01:33:18 -04:00
Metadata field not properly deserialized when using async_mode=True with PGVector #46
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @shamspias on GitHub (Oct 8, 2024).
When using the
PGVectorclass withasync_mode=True, themetadatafield of theDocumentobjects returned from query methods (e.g.,asimilarity_search_with_score_by_vector) is not deserialized into a Pythondict. Instead, it remains as aFragmentobject or another non-dict type. This causes aValidationErrorwhen theDocumentclass expectsmetadatato be a dictionary.To Reproduce
Steps to reproduce the behavior:
PGVectorwithasync_mode=Trueanduse_jsonb=True.asimilarity_searchorasimilarity_search_with_score_by_vector.Documentobjects havemetadatafields that are not dictionaries.Expected behavior
The
metadatafield of the returnedDocumentobjects should be properly deserialized into Python dictionaries, matching the behavior whenasync_mode=False.Actual behavior
When
async_mode=True, themetadatafield is aFragmentobject (fromasyncpg), leading to errors when the code expects adict.Error message
Environment:
langchain_postgresversion: 0.0.12pgvectorextensionasyncpgAdditional context
This issue arises because
asyncpgreturns JSONB fields asRecordorFragmentobjects, which are not automatically deserialized into Python dictionaries by SQLAlchemy when using asynchronous sessions.Code to Reproduce
Ensure that the required connection details like
connection_string,collection_name, andembedding_modelare securely provided when testing the code.Proposed Solution
Modify the
_results_to_docs_and_scoresmethod in thePGVectorclass to ensure that themetadatafield is correctly converted into a dictionary before creating theDocumentobjects.Related Issues:
#118
@simadimonyan commented on GitHub (Nov 11, 2024):
Did you fix it? I have related issue: https://github.com/langchain-ai/langchain/issues/28029
@shamspias commented on GitHub (Nov 28, 2024):
already fixed in
https://github.com/langchain-ai/langchain-postgres/pull/125
@eyurtsev commented on GitHub (Feb 7, 2025):
I can see other users are encountering this issue, but we don't have a way to reproduce currently.
One thing I'm noticing is that the this is using asyncpg as the driver, but the package is only meant to work with psycopg3 right now.
A unit test that shows how to reproduce is required