Weaviate v4 Client not working with LangChain WeaviateVectorStore & ExampleSelector?

Problem:

I recently upgraded to Weaviate v4 and tried to integrate it with LangChain’s WeaviateVectorStore. However, I encountered an error stating that my client must be an instance of WeaviateClient. Additionally, I am using SemanticSimilarityExampleSelector for few-shot learning, which now fails due to this issue.


My Code (With Hidden Configurations):

import weaviate
from weaviate.auth import AuthApiKey
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain_weaviate.vectorstores import WeaviateVectorStore
from langchain_community.vectorstores import Weaviate
from src.embedding import AliyunEmbedding

# **Connect to Weaviate v4 Client**
client = weaviate.connect_to_custom(
    http_host="my-server-ip",
    http_port=8080,
    http_secure=False,
    grpc_host="my-server-ip",
    grpc_port=50051,
    grpc_secure=False,
    auth_credentials=AuthApiKey("my-api-key")
)

# **Initialize Embedding Model**
embedding_model = AliyunEmbedding(dimensions=1024)

# **Initialize Weaviate VectorStore**
vectorstore = WeaviateVectorStore(
    client=client,
    index_name="CypherExamples",  # ✅ Stores Cypher query examples
    text_key="text",
    attributes=[],
    embedding=embedding_model
)

# **Instantiate ExampleSelector**
example_selector = SemanticSimilarityExampleSelector.from_examples(
    examples=[],  # ✅ Weaviate handles retrieval, so no examples are needed
    vectorstore=vectorstore,
    embeddings=embedding_model,  # ✅ Provide embeddings explicitly
    vectorstore_cls=WeaviateVectorStore,  # ✅ Ensure correct vectorstore class
    k=3  # ✅ Retrieve top 3 most relevant examples
)

---

###Error Message:
ValueError: client must be an instance of WeaviateClient

---

Any guidance would be greatly appreciated!

hi @Jixy !!

You need to pass the client, index_name and VectorStore class, so SemanticSimilarityExampleSelector instantiate it for you.

I took the opportunity and wrote a recipe on this:

example_selector = SemanticSimilarityExampleSelector.from_examples(
    # The list of examples available to select from.
    examples,
    # The embedding class used to produce embeddings which are used to measure semantic similarity.
    OpenAIEmbeddings(),
    # The VectorStore class that is used to store the embeddings and do a similarity search over.
    WeaviateVectorStore,
    client=client,
    index_name="MyExamples",
    # The number of examples to produce.
    k=1,
)
similar_prompt = FewShotPromptTemplate(
    # We provide an ExampleSelector instead of examples.
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="Input: {adjective}\nOutput:",
    input_variables=["adjective"],
)

This was crafted based doc in How to select examples by similarity | 🦜️🔗 LangChain

Let me know if that helps!

Thanks!

1 Like

thank you very much,it works and the problems have been solved

1 Like