Skip to main content

Langchain Integrations for Cloudflare's WorkersAI and Vectorize

Project description

langchain-cloudflare

This package contains the LangChain integration with CloudflareWorkersAI

Installation

pip install -U langchain-cloudflare

And you should configure credentials by setting the following environment variables:

  • CF_ACCOUNT_ID

AND

  • CF_API_TOKEN (if using a single token scoped for all services)

OR (if using separately scoped tokens)

  • CF_AI_API_TOKEN (CloudflareWorkersAI and CloudflareWorkersAIEmbeddings)
  • CF_AI_SEARCH_API_TOKEN (CloudflareAISearchRetriever)
  • CF_VECTORIZE_API_TOKEN (CloudflareVectorize)
  • CF_D1_API_TOKEN (CloudflareVectorize)
  • CF_D1_DATABASE_ID (CloudflareVectorize)

Chat Models

ChatCloudflareWorkersAI class exposes chat models from CloudflareWorkersAI.

from langchain_cloudflare.chat_models import ChatCloudflareWorkersAI

llm = ChatCloudflareWorkersAI()
llm.invoke("Sing a ballad of LangChain.")

REST endpoint format

By default, ChatCloudflareWorkersAI uses the native Workers AI run endpoint:

llm = ChatCloudflareWorkersAI(
    model="@cf/moonshotai/kimi-k2.6",
    endpoint_format="workers_ai",  # default
)

For REST calls that need Cloudflare's OpenAI-compatible chat completions API, set endpoint_format="openai_compatible":

llm = ChatCloudflareWorkersAI(
    model="@cf/moonshotai/kimi-k2.6",
    endpoint_format="openai_compatible",
)

When ai_gateway is configured, OpenAI-compatible mode routes through the Workers AI chat completions path on AI Gateway. This option is REST-only; Worker bindings use env.AI.run() and do not expose a chat completions route.

Embeddings

CloudflareWorkersAIEmbeddings class exposes embeddings from CloudflareWorkersAI.

from langchain_cloudflare.embeddings import CloudflareWorkersAIEmbeddings

embeddings = CloudflareWorkersAIEmbeddings(
    model_name="@cf/baai/bge-base-en-v1.5"
)
embeddings.embed_query("What is the meaning of life?")

VectorStores

CloudflareVectorize class exposes vectorstores from Cloudflare Vectorize.

from langchain_cloudflare.vectorstores import CloudflareVectorize

vst = CloudflareVectorize(
    embedding=embeddings
)
vst.create_index(index_name="my-cool-vectorstore")

Retrievers

CloudflareAISearchRetriever exposes Cloudflare AI Search (the managed retrieval / RAG service, fka AutoRAG) as a LangChain retriever.

Prerequisites

  • An AI Search instance with content. The retriever searches an existing instance, so create one and add your data first — via the dashboard, Wrangler, or the Python SDK.
  • Credentials, read from the environment:
    • CF_ACCOUNT_ID
    • CF_AI_SEARCH_API_TOKEN — an AI Search:Run token (falls back to CF_API_TOKEN)
    • CF_AI_SEARCH_INSTANCE_NAME — or pass instance_name=

Usage

from langchain_cloudflare import CloudflareAISearchRetriever

retriever = CloudflareAISearchRetriever(instance_name="my-instance")
docs = retriever.invoke("How do I configure Workers AI?")

Inside a Python Worker, pass the dedicated ai_search binding instead of REST credentials (async only):

retriever = CloudflareAISearchRetriever(binding=env.MY_SEARCH)
docs = await retriever.ainvoke("How do I configure Workers AI?")

The constructor exposes AI Search's retrieval options (hybrid search, metadata filters, reranking, query rewriting, …) as parameters, plus an ai_search_options parameter for passing any AI Search option that doesn't have its own parameter. As a standard BaseRetriever it plugs into RAG chains and becomes an agent tool via create_retriever_tool. For multi-tenant setups, give each tenant its own instance and point a retriever at that instance.

Release Notes

v0.1.1 (2025-04-08)

  • Added ChatCloudflareWorkersAI integration
  • Added CloudflareWorkersAIEmbeddings support
  • Added CloudflareVectorize integration

v0.1.3 (2025-04-10)

  • Added AI Gateway support for CloudflareWorkersAIEmbeddings
  • Added Async support for CloudflareWorkersAIEmbeddings

v0.1.4 (2025-04-14)

  • Added support for additional model parameters as explicit class attributes for ChatCloudflareWorkersAI

v0.1.6 (2025-05-01)

  • Added Standalone D1 Metadata Filtering Methods
  • Update Docs for more clarity around D1 Table/Vectorize Index Names

v0.1.8 (2025-05-11)

  • Added support for environmental variables (embeddings, vectorstores)

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_cloudflare-0.3.6.tar.gz (103.1 kB view details)

Uploaded Source

Built Distribution

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

langchain_cloudflare-0.3.6-py3-none-any.whl (54.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_cloudflare-0.3.6.tar.gz.

File metadata

  • Download URL: langchain_cloudflare-0.3.6.tar.gz
  • Upload date:
  • Size: 103.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_cloudflare-0.3.6.tar.gz
Algorithm Hash digest
SHA256 f53740aca25d9be2546047c1a2af1ac8bcfaecc445afa1f2ed7c848685a8d079
MD5 25cb9e8375f8502ac0856047b033a48a
BLAKE2b-256 457404617076e798bfc58b767236dc7862159abc41b160ab506f8d55f8075364

See more details on using hashes here.

File details

Details for the file langchain_cloudflare-0.3.6-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_cloudflare-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7c3cb2bbf2c89415aaa9e0b9920e25a82e499782c37c7c30bc89340486839ca8
MD5 e3e5668aeab5f78c9228773f731ec511
BLAKE2b-256 2b00d7f65c933b2f4fe94d330acc2a2740a537475f52e633ec0c1c5610d369eb

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