[Python] LangChain FAISS similarity_search returns empty list despite populated index

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to use LangChain with FAISS to build a simple document retriever. I’ve indexed several documents, but when I call similarity_search, I always get an empty list.

    from langchain.embeddings.openai import OpenAIEmbeddings
    from langchain.vectorstores import FAISS
    from langchain.schema import Document

    docs = [
    Document(page_content="Paris is the capital of France."),
    Document(page_content="Berlin is the capital of Germany."),
    Document(page_content="Tokyo is the capital of Japan.")
    ]

    embeddings = OpenAIEmbeddings(openai_api_key="<API_KEY>")
    vectorstore = FAISS.from_documents(docs, embeddings)

    query = "What is the capital of Germany?"
    results = vectorstore.similarity_search(query, k=2)

    print(results) # Expected: at least one Document about Berlin, but this prints []


    Environment:

    • langchain==0.0.297
    • faiss-cpu==1.7.4
    • openai==0.27.4
    • Python 3.9.16

    Expected behavior: similarity_search should return the Document about Berlin, or at least a non-empty list.

    What I’ve tried:

    • Verified that docs contains the correct content.
    • Checked that vectorstore.index has vectors (no errors when building).

    Actual behavior: An empty list is returned every time, even for very simple queries.

    Continue reading...

Compartilhe esta Página