Skip to main content

Azure CosmosDB integrations for LangChain and LangGraph, including vector store, cache, chat history, and checkpoint.

Project description

langchain-azure-cosmosdb

Azure CosmosDB NoSQL integrations for LangChain and LangGraph.

Installation

pip install langchain-azure-cosmosdb

Integrations

Integration Sync Async Description
Vector Store AzureCosmosDBNoSqlVectorSearch AsyncAzureCosmosDBNoSqlVectorSearch Vector, full-text, hybrid, and weighted hybrid search
Semantic Cache AzureCosmosDBNoSqlSemanticCache AsyncAzureCosmosDBNoSqlSemanticCache LLM response caching backed by CosmosDB
Chat History CosmosDBChatMessageHistory AsyncCosmosDBChatMessageHistory Persistent chat message history
LangGraph Checkpointer CosmosDBSaverSync CosmosDBSaver LangGraph graph state persistence
LangGraph Cache CosmosDBCacheSync CosmosDBCache LangGraph node-level result caching
LangGraph Store CosmosDBStore AsyncCosmosDBStore LangGraph long-term memory with optional vector search

Usage

Vector Store

from azure.cosmos import CosmosClient, PartitionKey
from langchain_azure_cosmosdb import AzureCosmosDBNoSqlVectorSearch

cosmos_client = CosmosClient("<endpoint>", "<key>")

vectorstore = AzureCosmosDBNoSqlVectorSearch(
    cosmos_client=cosmos_client,
    embedding=embedding,
    vector_embedding_policy={
        "vectorEmbeddings": [{
            "path": "/embedding",
            "dataType": "float32",
            "distanceFunction": "cosine",
            "dimensions": 1536,
        }]
    },
    indexing_policy={
        "indexingMode": "consistent",
        "includedPaths": [{"path": "/*"}],
        "excludedPaths": [{"path": '/"_etag"/?'}],
        "vectorIndexes": [{"path": "/embedding", "type": "diskANN"}],
    },
    cosmos_container_properties={"partition_key": PartitionKey(path="/id")},
    cosmos_database_properties={"id": "my-database"},
    vector_search_fields={"text_field": "text", "embedding_field": "embedding"},
    database_name="my-database",
    container_name="my-container",
)

# Add documents
vectorstore.add_texts(["Azure CosmosDB is a multi-model database."])

# Search
results = vectorstore.similarity_search("What is CosmosDB?", k=3)

Semantic Cache

from azure.cosmos import CosmosClient, PartitionKey
from langchain_core.globals import set_llm_cache
from langchain_azure_cosmosdb import AzureCosmosDBNoSqlSemanticCache

cosmos_client = CosmosClient("<endpoint>", "<key>")

cache = AzureCosmosDBNoSqlSemanticCache(
    cosmos_client=cosmos_client,
    embedding=embedding,
    vector_embedding_policy=vector_embedding_policy,
    indexing_policy=indexing_policy,
    cosmos_container_properties={"partition_key": PartitionKey(path="/id")},
    cosmos_database_properties={"id": "cache-db"},
    vector_search_fields={"text_field": "text", "embedding_field": "embedding"},
    database_name="cache-db",
    container_name="cache-container",
)

set_llm_cache(cache)

# First call hits LLM, second call returns cached result
response = llm.invoke("What is CosmosDB?")

Chat Message History

from langchain_azure_cosmosdb import CosmosDBChatMessageHistory

history = CosmosDBChatMessageHistory(
    cosmos_endpoint="<endpoint>",
    credential="<key>",  # or a TokenCredential for AAD
    cosmos_database="chat-db",
    cosmos_container="chat-container",
    session_id="session-001",
    user_id="user-alice",
    ttl=3600,  # optional: messages expire after 1 hour
)
history.prepare_cosmos()

history.add_user_message("Hello!")
history.add_ai_message("Hi there!")
print(history.messages)

LangGraph Checkpointer

Sync

from langchain_azure_cosmosdb import CosmosDBSaverSync

# Sync — uses COSMOSDB_ENDPOINT / COSMOSDB_KEY env vars or explicit params
checkpointer = CosmosDBSaverSync(
    database_name="langgraph-db",
    container_name="checkpoints",
    endpoint="<endpoint>",
    key="<key>",
)

graph = workflow.compile(checkpointer=checkpointer)
result = graph.invoke(input, config={"configurable": {"thread_id": "1"}})

Async

from langchain_azure_cosmosdb import CosmosDBSaver

# Async — use as a context manager
async with CosmosDBSaver.from_conn_info(
    endpoint="<endpoint>",
    key="<key>",
    database_name="langgraph-db",
    container_name="checkpoints",
) as checkpointer:
    graph = workflow.compile(checkpointer=checkpointer)
    result = await graph.ainvoke(input, config={"configurable": {"thread_id": "1"}})

LangGraph Cache

Sync

from langchain_azure_cosmosdb import CosmosDBCacheSync

cache = CosmosDBCacheSync(
    database_name="langgraph-db",
    container_name="cache",
    endpoint="<endpoint>",
    key="<key>",
)

graph = workflow.compile(cache=cache)

Async

from langchain_azure_cosmosdb import CosmosDBCache

async with CosmosDBCache.from_conn_info(
    endpoint="<endpoint>",
    key="<key>",
    database_name="langgraph-db",
    container_name="cache",
) as cache:
    graph = workflow.compile(cache=cache)
    result = await graph.ainvoke(input, config={"configurable": {"thread_id": "1"}})

LangGraph Store (Long-Term Memory)

Sync

from langchain_azure_cosmosdb import CosmosDBStore

store = CosmosDBStore.from_endpoint(
    endpoint="<endpoint>",
    credential="<key>",
    database_name="langgraph-db",
    container_name="store",
    index={
        "dims": 1536,
        "embed": embedding,
        "fields": ["text"],
    },
)
store.setup()

# Store items under namespaces
store.put(("users", "alice", "preferences"), "coffee", {"text": "Dark roast"})
item = store.get(("users", "alice", "preferences"), "coffee")

# Semantic search
results = store.search(("users",), query="beverage preferences", limit=3)

Async

from langchain_azure_cosmosdb import AsyncCosmosDBStore

async with AsyncCosmosDBStore.from_endpoint(
    endpoint="<endpoint>",
    credential="<key>",
    database_name="langgraph-db",
    container_name="store",
    index={
        "dims": 1536,
        "embed": embedding,
        "fields": ["text"],
    },
) as store:
    await store.setup()

    await store.aput(("users", "alice", "preferences"), "coffee", {"text": "Dark roast"})
    item = await store.aget(("users", "alice", "preferences"), "coffee")

    results = await store.asearch(("users",), query="beverage preferences", limit=3)

Authentication

All integrations support both access key and Microsoft Entra ID (AAD / Managed Identity) authentication:

# Access key
from azure.cosmos import CosmosClient
client = CosmosClient("<endpoint>", "<key>")

# AAD / Managed Identity
from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential
client = CosmosClient("<endpoint>", credential=DefaultAzureCredential())

The LangGraph integrations that manage their own client — CosmosDBSaverSync / CosmosDBSaver, CosmosDBCacheSync / CosmosDBCache, and CosmosDBStore / AsyncCosmosDBStore — fall back to DefaultAzureCredential automatically when no key is provided. The semantic cache (AzureCosmosDBNoSqlSemanticCache) and vectorstore require you to pass a CosmosClient explicitly.

Samples

See the samples/cosmosdb-nosql/ directory for runnable end-to-end examples of every integration.

Changelog

1.0.0

Initial release of langchain-azure-cosmosdb — a standalone package consolidating all Azure CosmosDB NoSQL integrations for LangChain and LangGraph.

LangChain Integrations:

  • AzureCosmosDBNoSqlVectorSearch / AsyncAzureCosmosDBNoSqlVectorSearch — Vector, full-text, hybrid, and weighted hybrid search
  • AzureCosmosDBNoSqlSemanticCache / AsyncAzureCosmosDBNoSqlSemanticCache — LLM semantic response caching
  • CosmosDBChatMessageHistory / AsyncCosmosDBChatMessageHistory — Persistent chat message history with TTL support

LangGraph Integrations:

  • CosmosDBSaverSync / CosmosDBSaver — Graph state checkpointing
  • CosmosDBCacheSync / CosmosDBCache — Node-level result caching
  • CosmosDBStore / AsyncCosmosDBStore — Long-term memory store with optional vector search

Highlights:

  • Full sync and async support for all integrations
  • Microsoft Entra ID (AAD / Managed Identity) authentication across all integrations
  • User agent tracking for all CosmosDB client instances

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_azure_cosmosdb-1.0.0.tar.gz (58.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_azure_cosmosdb-1.0.0-py3-none-any.whl (69.3 kB view details)

Uploaded Python 3

File details

Details for the file langchain_azure_cosmosdb-1.0.0.tar.gz.

File metadata

  • Download URL: langchain_azure_cosmosdb-1.0.0.tar.gz
  • Upload date:
  • Size: 58.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for langchain_azure_cosmosdb-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4b0dbffcd867ddc791fc4ec4dbc8e626d9910f0e41d4103f844313895e1f775e
MD5 c3ad40e9cb310f3511db5ae4d8962ccb
BLAKE2b-256 bd02c26a02409459c31b2e05d46d46cb3fc4ed6eacbddb8968d3f0f266c38fe3

See more details on using hashes here.

File details

Details for the file langchain_azure_cosmosdb-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_azure_cosmosdb-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31ad363091d51e01b8b95c6eca1a81e4c20f98f7dde062a9925f69eb5d0ba598
MD5 aebd531adda99637ba157d1d4aeddeb6
BLAKE2b-256 90177c1416bc70a9c73584cbdc0a45203831a650c9ec5f5108462337f769f8ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page