Skip to main content

langchain-couchbase

This package contains the official LangChain integration with Couchbase

The documentation and API Reference can be found on Github Pages.

Installation

pip install -U langchain-couchbase

Vector Store

CouchbaseQueryVectorStore

CouchbaseQueryVectorStore class enables the usage of Couchbase for Vector Search using the Query and Indexing Service. It supports two different types of vector indexes:

  • Hyperscale Vector Index - Optimized for pure vector searches on large datasets (billions of documents). Best for content discovery, recommendations, and applications requiring high accuracy with low memory footprint. Hyperscale Vector indexes compare vectors and scalar values simultaneously.

  • Composite Vector Index - Combines a Global Secondary Index (GSI) with a vector column. Ideal for searches combining vector similarity with scalar filters where scalars filter out large portions of the dataset. Composite Vector indexes apply scalar filters first, then perform vector searches on the filtered results.

For guidance on choosing the right index type, see Choose the Right Vector Index.

Note: CouchbaseQueryVectorStore requires Couchbase Server version 8.0 and above.

To use this in an application:

import getpass

# Constants for the connection
COUCHBASE_CONNECTION_STRING = getpass.getpass(
    "Enter the connection string for the Couchbase cluster: "
)
DB_USERNAME = getpass.getpass("Enter the username for the Couchbase cluster: ")
DB_PASSWORD = getpass.getpass("Enter the password for the Couchbase cluster: ")

# Create Couchbase connection object
from datetime import timedelta

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import ClusterOptions

auth = PasswordAuthenticator(DB_USERNAME, DB_PASSWORD)
options = ClusterOptions(auth)
cluster = Cluster(COUCHBASE_CONNECTION_STRING, options)

# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))

from langchain_couchbase import CouchbaseQueryVectorStore
from langchain_couchbase.vectorstores import DistanceStrategy

vector_store = CouchbaseQueryVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=my_embeddings,
    distance_metric=DistanceStrategy.DOT,
)

Note: The Hyperscale and Composite vector indexes must be created after adding documents to the vector store. This enables efficient vector searches.

See a usage example

CouchbaseSearchVectorStore

CouchbaseSearchVectorStore class enables the usage of Couchbase for Vector Search using Search Vector Indexes. Search Vector Indexes combine a Couchbase Search index with a vector column, allowing hybrid searches that combine vector searches with Full-Text Search (FTS) and geospatial searches.

Note: CouchbaseSearchVectorStore requires Couchbase Server version 7.6 and above.

from langchain_couchbase import CouchbaseSearchVectorStore

To use this in an application:

import getpass

# Constants for the connection
COUCHBASE_CONNECTION_STRING = getpass.getpass(
    "Enter the connection string for the Couchbase cluster: "
)
DB_USERNAME = getpass.getpass("Enter the username for the Couchbase cluster: ")
DB_PASSWORD = getpass.getpass("Enter the password for the Couchbase cluster: ")

# Create Couchbase connection object
from datetime import timedelta

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import ClusterOptions

auth = PasswordAuthenticator(DB_USERNAME, DB_PASSWORD)
options = ClusterOptions(auth)
cluster = Cluster(COUCHBASE_CONNECTION_STRING, options)

# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))

from langchain_couchbase import CouchbaseSearchVectorStore

vector_store = CouchbaseSearchVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=my_embeddings,
    index_name=SEARCH_INDEX_NAME,
)

See a usage example

LLM Caches

CouchbaseCache

Use Couchbase as a cache for prompts and responses.

See a usage example.

To import this cache:

from langchain_couchbase.cache import CouchbaseCache

To use this cache with your LLMs:

from langchain_core.globals import set_llm_cache

cluster = couchbase_cluster_connection_object

set_llm_cache(
    CouchbaseCache(
        cluster=cluster,
        bucket_name=BUCKET_NAME,
        scope_name=SCOPE_NAME,
        collection_name=COLLECTION_NAME,
    )
)

CouchbaseSemanticCache

Semantic caching allows users to retrieve cached prompts based on the semantic similarity between the user input and previously cached inputs. Under the hood it uses Couchbase as both a cache and a vectorstore. The CouchbaseSemanticCache needs a Search Index defined to work. Please look at the usage example on how to set up the index.

See a usage example.

To import this cache:

from langchain_couchbase.cache import CouchbaseSemanticCache

To use this cache with your LLMs:

from langchain_core.globals import set_llm_cache

# use any embedding provider...

from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()
cluster = couchbase_cluster_connection_object

set_llm_cache(
    CouchbaseSemanticCache(
        cluster=cluster,
        embedding=embeddings,
        bucket_name=BUCKET_NAME,
        scope_name=SCOPE_NAME,
        collection_name=COLLECTION_NAME,
        index_name=INDEX_NAME,
    )
)

Chat Message History

Use Couchbase as the storage for your chat messages.

See a usage example.

To use the chat message history in your applications:

from langchain_couchbase.chat_message_histories import CouchbaseChatMessageHistory

cluster = couchbase_cluster_connection_object

message_history = CouchbaseChatMessageHistory(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    session_id="test-session",
)

message_history.add_user_message("hi!")
Documentation

Generating Documentation Locally

To generate the documentation locally, follow these steps:

  1. Ensure you have the project installed in your environment:
pip install -e .  # Install in development mode
  1. Install the required documentation dependencies:
pip install sphinx sphinx-rtd-theme tomli
  1. Navigate to the docs directory:
cd docs
  1. Ensure the build directory exists:
mkdir -p source/build
  1. Build the HTML documentation:
make html
  1. The generated documentation will be available in the docs/build/html directory. You can open index.html in your browser to view it:
# On macOS
open build/html/index.html
# On Linux
xdg-open build/html/index.html
# On Windows
start build/html/index.html

Additional Documentation Commands

  • To clean the build directory before rebuilding:
make clean html
  • To check for broken links in the documentation:
make linkcheck
  • To generate a PDF version of the documentation (requires LaTeX):
make latexpdf
  • For help on available make commands:
make help

Troubleshooting

  • If you encounter errors about missing modules, ensure you have installed the project in your environment.
  • If Sphinx can't find your package modules, verify your conf.py has the correct path configuration.
  • For sphinx-specific errors, refer to the Sphinx documentation.
  • If you see an error about missing tomli module, make sure you've installed it with pip install tomli.

Usage Data

This product automatically collects usage and performance data (such as product name and version) and browser information (such as IP address) (collectively, "Usage Data"). Couchbase uses Usage Data, along with other data you may provide to Couchbase (such as your user name or email address), to develop and improve our products as well as inform our sales and marketing programs. We do not access or collect any data you store in Couchbase products. We use Usage Data to understand aggregate usage patterns and make our products more useful to you. For more information on how Couchbase collects, protects, and processes information, please refer to the Couchbase Privacy Policy viewable at https://www.couchbase.com/privacy-policy.

Contributing

We welcome contributions! Please see our Contributing Guide for details.

📢 Support Policy

We truly appreciate your interest in this project!
This project is community-maintained, which means it's not officially supported by our support team.

If you need help, have found a bug, or want to contribute improvements, the best place to do that is right here — by opening a GitHub issue.
Our support portal is unable to assist with requests related to this project, so we kindly ask that all inquiries stay within GitHub.

Your collaboration helps us all move forward together — thank you!

Metadata

Release files for langchain-couchbase 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-couchbase 1.2.0
File Size Uploaded Provenance Actions
langchain_couchbase-1.2.0.tar.gz 21.2 kB Without provenance Details for langchain_couchbase-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-couchbase 1.2.0
File Interpreter ABI Platform Actions
langchain_couchbase-1.2.0-py3-none-any.whl Python 3 none any Details for langchain_couchbase-1.2.0-py3-none-any.whl

Total release size: 47.6 kB

Release files / langchain_couchbase-1.2.0.tar.gz

Download URL langchain_couchbase-1.2.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
80b440720b09bd416024b2565bd210bce84a6c49b94f6767defbd49d09745958
BLAKE2b-256 checksum
How to use checksums
22f64b43e1f848e9226e45a4b5f73316911f8dde54da9726ba9cf4927b6a53e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / langchain_couchbase-1.2.0-py3-none-any.whl

Download URL langchain_couchbase-1.2.0-py3-none-any.whl
Size 26.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9945da304eb1f5c93129437489691bf292d21606cfffcc4cbcf521fe9458a3d
BLAKE2b-256 checksum
How to use checksums
25ef0b38f7662fd07ed62595b0f8a829a25ea9502a8f5122c45ace65aa942c11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Security for langchain-couchbase 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page