Skip to content

PGVectorStore fails with GoogleGenerativeAIEmbeddings #247

@sergiocharpineljr

Description

@sergiocharpineljr

The code below fails with the following error:

RuntimeError: Task <Task pending name='Task-3' coro=<AsyncPGVectorStore.asimilarity_search() running at xxx/.venv/lib/python3.12/site-packages/langchain_postgres/v2/async_vectorstore.py:689> cb=[_chain_future.<locals>._call_set_state() at /usr/lib/python3.12/asyncio/futures.py:394]> got Future <Task pending name='Task-4' coro=<InterceptedUnaryUnaryCall._invoke() running at xxx/.venv/lib/python3.12/site-packages/grpc/aio/_interceptor.py:652> cb=[InterceptedCall._fire_or_add_pending_done_callbacks()]> attached to a different loop

The above exception was the direct cause of the following exception:
...

    raise GoogleGenerativeAIError(f"Error embedding content: {e}") from e
langchain_google_genai._common.GoogleGenerativeAIError: Error embedding content: Task <Task pending name='Task-3' coro=<AsyncPGVectorStore.asimilarity_search() running at xxx/.venv/lib/python3.12/site-packages/langchain_postgres/v2/async_vectorstore.py:689> cb=[_chain_future.<locals>._call_set_state() at /usr/lib/python3.12/asyncio/futures.py:394]> got Future <Task pending name='Task-4' coro=<InterceptedUnaryUnaryCall._invoke() running at xxx/.venv/lib/python3.12/site-packages/grpc/aio/_interceptor.py:652> cb=[InterceptedCall._fire_or_add_pending_done_callbacks()]> attached to a different loop

Code:

import os
from dotenv import load_dotenv
from langchain_postgres import PGEngine, PGVectorStore
from langchain_google_genai import GoogleGenerativeAIEmbeddings

load_dotenv()
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
POSTGRES_DB = os.getenv("POSTGRES_DB")
TABLE_NAME = os.getenv("TABLE_NAME", "vectorstore")
VECTOR_SIZE = 1024

CONNECTION_STRING = (
    f"postgresql+psycopg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}"
    f":{POSTGRES_PORT}/{POSTGRES_DB}"
)

pg_engine = PGEngine.from_connection_string(url=CONNECTION_STRING)

# Needed only for initialization
#pg_engine.init_vectorstore_table(
#    table_name=TABLE_NAME,
#    vector_size=VECTOR_SIZE,
#)
embeddings = GoogleGenerativeAIEmbeddings(model="models/gemini-embedding-001")
vector_store = PGVectorStore.create_sync(
    engine=pg_engine,
    table_name=TABLE_NAME,
    embedding_service=embeddings,
)

result = vector_store.similarity_search("My Search")
print(result)

Also tried with async versions of the methods but the error is the same.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions