Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain VectorStore As Retriever

From Leeroopedia
Knowledge Sources
Domains Information_Retrieval, RAG
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for converting a vector store into a LangChain Retriever provided by langchain-core.

Description

The VectorStore.as_retriever() method creates a VectorStoreRetriever that wraps the vector store. The retriever delegates to similarity_search(), max_marginal_relevance_search(), or similarity_search_with_relevance_scores() based on the search_type parameter.

Usage

Call as_retriever() on any configured vector store to get a Runnable that can be composed in LCEL chains.

Code Reference

Source Location

  • Repository: langchain
  • File: libs/core/langchain_core/vectorstores/base.py
  • Lines: L905-961 (as_retriever), L964-1112 (VectorStoreRetriever)

Signature

def as_retriever(self, **kwargs: Any) -> VectorStoreRetriever:

class VectorStoreRetriever(BaseRetriever):
    vectorstore: VectorStore
    search_type: str = "similarity"
    search_kwargs: dict = Field(default_factory=dict)

Import

from langchain_core.vectorstores import VectorStoreRetriever
# Usually accessed via vectorstore.as_retriever()

I/O Contract

Inputs

Name Type Required Description
search_type str No (default: "similarity") Search method: "similarity", "mmr", or "similarity_score_threshold"
search_kwargs dict No Passed to underlying search (k, filter, fetch_k, lambda_mult, score_threshold)

Outputs

Name Type Description
return VectorStoreRetriever Retriever implementing BaseRetriever; composable in LCEL chains

Usage Examples

RAG Chain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

retriever = vectorstore.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 5, "fetch_k": 20},
)

prompt = ChatPromptTemplate.from_template(
    "Answer based on context:\n{context}\n\nQuestion: {question}"
)

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | ChatOpenAI(model="gpt-4o-mini")
    | StrOutputParser()
)

answer = chain.invoke("What is LangChain?")

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment