Skip to main content

How to do per-user retrieval


This guide assumes familiarity with the following:

When building a retrieval app, you often have to build it with multiple users in mind. This means that you may be storing data not just for one user, but for many different users, and they should not be able to see each otherโ€™s data. This means that you need to be able to configure your retrieval chain to only retrieve certain information. This generally involves two steps.

Step 1: Make sure the retriever you are using supports multiple users

At the moment, there is no unified flag or filter for this in LangChain. Rather, each vectorstore and retriever may have their own, and may be called different things (namespaces, multi-tenancy, etc). For vectorstores, this is generally exposed as a keyword argument that is passed in during similaritySearch. By reading the documentation or source code, figure out whether the retriever you are using supports multiple users, and, if so, how to use it.

Step 2: Add that parameter as a configurable field for the chain

The LangChain config object is passed through to every Runnable. Here you can add any fields youโ€™d like to the configurable object. Later, inside the chain we can extract these fields.

Step 3: Call the chain with that configurable field

Now, at runtime you can call this chain with configurable field.

Code Exampleโ€‹

Letโ€™s see a concrete example of what this looks like in code. We will use Pinecone for this example.


Install dependenciesโ€‹

yarn add @langchain/pinecone @langchain/openai @pinecone-database/pinecone @langchain/core

Set environment variablesโ€‹

Weโ€™ll use OpenAI and Pinecone in this example:



# Optional, use LangSmith for best-in-class observability
import { OpenAIEmbeddings } from "@langchain/openai";
import { PineconeStore } from "@langchain/pinecone";
import { Pinecone } from "@pinecone-database/pinecone";
import { Document } from "@langchain/core/documents";

const embeddings = new OpenAIEmbeddings();

const pinecone = new Pinecone();

const pineconeIndex = pinecone.Index(Deno.env.get("PINECONE_INDEX"));

const vectorStore = await PineconeStore.fromExistingIndex(
new OpenAIEmbeddings(),
{ pineconeIndex }

await vectorStore.addDocuments(
[new Document({ pageContent: "i worked at kensho" })],
{ namespace: "harrison" }

await vectorStore.addDocuments(
[new Document({ pageContent: "i worked at facebook" })],
{ namespace: "ankush" }
[ "77b8f174-9d89-4c6c-b2ab-607fe3913b2d" ]

The pinecone kwarg for namespace can be used to separate documents

// This will only get documents for Ankush
const ankushRetriever = vectorStore.asRetriever({
filter: {
namespace: "ankush",

await ankushRetriever.invoke("where did i work?");
[ Document { pageContent: "i worked at facebook", metadata: {} } ]
// This will only get documents for Harrison
const harrisonRetriever = vectorStore.asRetriever({
filter: {
namespace: "harrison",

await harrisonRetriever.invoke("where did i work?");
[ Document { pageContent: "i worked at kensho", metadata: {} } ]

We can now create the chain that we will use to perform question-answering.

import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
} from "@langchain/core/runnables";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";

const template = `Answer the question based only on the following context:
Question: {question}`;

const prompt = ChatPromptTemplate.fromTemplate(template);

const model = new ChatOpenAI({
model: "gpt-3.5-turbo-0125",
temperature: 0,

We can now create the chain using our configurable retriever. It is configurable because we can define any object which will be passed to the chain. From there, we extract the configurable object and pass it to the vectorstore.

import {
} from "@langchain/core/runnables";

const chain = RunnableSequence.from([
context: async (input, config) => {
if (!config || !("configurable" in config)) {
throw new Error("No config");
const { configurable } = config;
const documents = await vectorStore
.invoke(input.question, config);
return => doc.pageContent).join("\n\n");
new StringOutputParser(),

We can now invoke the chain with configurable options. search_kwargs is the id of the configurable field. The value is the search kwargs to use for Pinecone

await chain.invoke(
{ question: "where did the user work?" },
{ configurable: { filter: { namespace: "harrison" } } }
"The user worked at Kensho."
await chain.invoke(
{ question: "where did the user work?" },
{ configurable: { filter: { namespace: "ankush" } } }
"The user worked at Facebook."

For more vector store implementations that can support multiple users, please refer to specific pages, such as Milvus.

Next stepsโ€‹

Youโ€™ve now seen one approach for supporting retrieval with data from multiple users.

Next, check out some of the other how-to guides on RAG, such as returning sources.

Was this page helpful?

You can also leave detailed feedback on GitHub.