---
title: "LangChain"
description: "Use SurrealDB native vector indexes as a drop-in vector store for LangChain."
canonical_url: "https://surrealdb.com/docs/integrate/integrations/ai-frameworks/langchain"
section: "AI frameworks"
surrealdb_version: "4.x"
last_modified: "2026-11-18T02:40:10Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# LangChain

Use SurrealDB native vector indexes as a drop-in vector store for LangChain.

[LangChain](https://python.langchain.com/docs/introduction/) is a framework for developing applications powered by large language models (LLMs). SurrealDB suits [LangChain](https://python.langchain.com/docs/introduction/) projects mainly because of its [multi-model capabilities](/docs/learn/data-models.md): a single database manages all of the application's data.

It handles structured and unstructured data, with vector search, [graph traversal](/docs/learn/data-models/graph/overview.md), [relational queries](/docs/learn/data-models.md), [full-text search](/docs/learn/data-models/full-text-search/overview.md), [document storage](/docs/learn/data-models/document/overview.md), and [time-series data](/docs/learn/data-models/time-series/overview.md) all within one ACID-compliant engine.

LangChain applications often work with several types of data for tasks such as context retrieval. Keeping all of them in one database simplifies the architecture, reduces latency, and keeps the data consistent.

This guide shows how to use SurrealDB as a vector store for LangChain.

> [!NOTE]
> This page uses the Python package, `langchain-surrealdb`. For TypeScript and JavaScript, the `@surrealdb/langgraph` package stores LangGraph.js checkpoints, long-term memory and node results in SurrealDB. See [LangGraph](/docs/integrate/integrations/ai-frameworks/langgraph.md).

<video style="width: 100%;" height="300" controls>
    <source src={VidMp4} type="video/mp4" />
    <source src={VidWebm} type="video/webm" />
</video>

## Setup

The examples on this page connect to a SurrealDB server at `ws://localhost:8000` and sign in as `root` with the password `secret`. To run one locally, [install SurrealDB](/docs/running/installation.md) and start it in memory with `surreal db start -u root -p secret`, or [run it with Docker](/docs/running/docker.md).

Then install the packages:

```bash
pip install -U langchain-surrealdb langchain_ollama surrealdb
```

* `surrealdb` → [SurrealDB Python SDK](/docs/reference/python.md)
* `langchain-surrealdb` → houses `SurrealDBVectorStore`
* `langchain_ollama`, `langchain-openai` (or HF, Cohere, etc.) → embeddings

## Create a vector store

`SurrealDBVectorStore` takes a LangChain embeddings model and a SurrealDB connection:

```python
from langchain_surrealdb.vectorstores import SurrealDBVectorStore
from langchain_ollama import OllamaEmbeddings
from surrealdb import Surreal

conn = Surreal("ws://localhost:8000/rpc")
conn.signin({"username": "root", "password": "secret"})
conn.use("langchain", "demo")
vector_store = SurrealDBVectorStore(OllamaEmbeddings(model="llama3.2"), conn)
```

Any LangChain embeddings class works in place of `OllamaEmbeddings`, such as `OpenAIEmbeddings` from `langchain-openai`. Internally, the vector store:

1. Creates the table **`documents`** if it does not exist.
2. Adds an **HNSW** index with the dimension of the embeddings model, using cosine distance and `F32` vectors.
3. Inserts each text with the embedding that the model generated for it.

## Add and search documents

The vector store implements the standard LangChain vector store methods:

| Method | Use |
| --- | --- |
| `add_documents(documents, ids)` | Embed and store LangChain `Document` objects. |
| `similarity_search(query, k, custom_filter)` | Return the `k` documents closest to the query text. `custom_filter` matches on document metadata. |
| `similarity_search_with_score(query, k, custom_filter)` | The same, with the distance score of each document. |
| `as_retriever(search_type, search_kwargs)` | Wrap the vector store as a retriever for use in a chain, for example with `search_type="mmr"`. |

The LangChain example in the tutorial [Build an AI agent with Python](/docs/learn/tutorials/tutorials/build-an-ai-agent.md#getting-started-with-langchain) uses each of these on two sample documents and shows the output.

## Next steps

The following resources cover more advanced use of SurrealDB with LangChain.

Several example implementations are available:

* A [basic example](https://github.com/surrealdb/langchain-surrealdb/tree/main/examples/basic) with a ready-to-use Dockerfile - perfect for your first steps
* A more advanced [graph example](https://github.com/surrealdb/langchain-surrealdb/tree/main/examples/graph) showcasing graph capabilities
* An interactive [Jupyter notebook](https://python.langchain.com/docs/integrations/vectorstores/surrealdb/) for hands-on learning

### Further reading and resources

For a deeper understanding of the technology stack:

* Explore the [SurrealDB vector-search reference](/docs/learn/data-models/vector-search/overview.md) for detailed technical information
* Check out the [LangChain API docs for `SurrealDBStore`](https://api.python.langchain.com/en/latest/vectorstores/langchain_community.vectorstores.surrealdb.SurrealDBStore.html) for the full API documentation
* Browse [Awesome SurrealDB](https://github.com/surrealdb/awesome-surreal) for a curated collection of resources, tools, and applications
That’s it - you now have a fully-featured LangChain vector store powered by SurrealDB, no boilerplate required. 🚀
