LangChain Vector Database

Search an existing LangChain vectorstore or retriever from your Knowledge Base.

The example uses OpenAI-backed embeddings or models. Set your key before running it:

export OPENAI_API_KEY="your-api-key"

Prepare the sample input files in the directory where you will run the Python example:

mkdir -p data
curl --fail --location https://raw.githubusercontent.com/agno-agi/agno/8f36eaf2d18e91afa7b327eec66a3cd3685dcb87/cookbook/07_knowledge/testing_resources/state_of_the_union.txt --output data/state_of_the_union.txt

Setup

uv pip install -U langchain langchain-community langchain-openai langchain-chroma openai agno

Example

agent_with_knowledge.py
import pathlib

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.langchaindb import LangChainVectorDb
from langchain_text_splitters import CharacterTextSplitter
from langchain_chroma import Chroma
from langchain_community.document_loaders import TextLoader
from langchain_openai import OpenAIEmbeddings

chroma_db_dir = pathlib.Path("./chroma_db")

# Load, split, and embed the documents with LangChain
raw_documents = TextLoader("data/state_of_the_union.txt", encoding="utf-8").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
Chroma.from_documents(
    documents, OpenAIEmbeddings(), persist_directory=str(chroma_db_dir)
)

# Point Agno at the existing vectorstore
db = Chroma(
    embedding_function=OpenAIEmbeddings(), persist_directory=str(chroma_db_dir)
)
knowledge = Knowledge(
    vector_db=LangChainVectorDb(knowledge_retriever=db.as_retriever())
)

agent = Agent(knowledge=knowledge)
agent.print_response(
    "What did the president say about broadcasting and the State of the Union?",
    markdown=True,
)

LangChainVectorDb wraps a vectorstore you already manage with LangChain. Pass a retriever with knowledge_retriever, or pass the vectorstore itself with vectorstore and Agno creates a retriever on the first search.

LangChainVectorDb is search-only. insert() and upsert() raise NotImplementedError. Load documents through LangChain, then wire the retriever to Agno. Filter expressions are not supported. When Agno builds a retriever from vectorstore, it merges dictionary filters directly into retriever search kwargs and caches that retriever. Later search limits and filters do not rebuild it. Configure provider-specific metadata filtering on an explicit retriever (for example, Chroma's search_kwargs={"k": 5, "filter": {"department": "hr"}}) before passing it as knowledge_retriever. That retriever owns filtering; Agno does not enforce ordinary metadata dictionaries on each call.

LangChainVectorDb Params

ParameterTypeDefaultDescription
vectorstoreAnyNoneA LangChain vectorstore instance. Used to create a retriever on first search if knowledge_retriever is not set.
search_kwargsdictNoneSearch parameters passed to vectorstore.as_retriever(). Defaults to {"k": limit} at search time.
knowledge_retrieverAnyNoneA LangChain BaseRetriever instance. Takes precedence over vectorstore.
namestrNoneName of the vector database.
descriptionstrNoneDescription of the vector database.

Native Agno user_id scoping is not applied by this wrapper. Configure user-specific retrieval in the external retriever or server when needed.