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, CloudflareWorkersAIEmbeddings, CloudflareBrowserRunLoader, CloudflareBrowserRunTool)CF_AI_SEARCH_API_TOKEN(CloudflareAISearchRetriever)CF_VECTORIZE_API_TOKEN(CloudflareVectorize)CF_D1_API_TOKEN(CloudflareVectorize)CF_D1_DATABASE_ID(CloudflareVectorize)
Browser Run requires the Browser Rendering – Edit permission on your API token. See Browser Run setup.
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_IDCF_AI_SEARCH_API_TOKEN— anAI Search:Runtoken (falls back toCF_API_TOKEN)CF_AI_SEARCH_INSTANCE_NAME— or passinstance_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.
Browser Run: REST vs. Worker Binding Parity
Browser Run has two distinct APIs: Quick Actions (single request/response calls --
markdown extraction, screenshots, structured extraction, etc.) and full browser
sessions (stateful, multi-step control via CDP/Puppeteer/Playwright/Stagehand -- click,
type, navigate across pages). This library only implements Quick Actions; full sessions
are JS/npm-only (@cloudflare/puppeteer, Playwright) with no Python equivalent, so
they're not reachable from a Python Worker at all, REST or binding.
Every Quick Action is reachable through this library, split across
CloudflareBrowserRunLoader (document ingestion) and CloudflareBrowserRunTool (agent
actions) — see their sections below for details. crawl and browser="kitesurf" are
REST-only; every other Quick Action works over both REST and the binding parameter,
verified live against the real API and against a real Python Worker:
| Mode | Class | REST | Binding (quickAction()) |
|---|---|---|---|
markdown |
Loader, Tool | ✅ | ✅ |
content |
Loader | ✅ | ✅ |
scrape |
Loader | ✅ | ✅ |
crawl |
Loader | ✅ | ❌ async job with polling, no quickAction() equivalent |
json |
Tool | ✅ | ✅ |
links |
Tool | ✅ | ✅ |
screenshot |
Tool | ✅ | ✅ |
pdf |
Tool | ✅ | ✅ |
snapshot |
Tool | ✅ | ✅ |
accessibility_tree |
Tool | ✅ | ✅ |
browser="kitesurf" |
Loader, Tool | ✅ | ❌ URL query param, no binding equivalent |
The binding path is async-only on both classes (aload()/ainvoke(), not
load()/invoke()) — calling the sync methods with binding set raises
NotImplementedError.
Browser Run (Document Loader)
CloudflareBrowserRunLoader loads web pages as LangChain Document objects using
Cloudflare Browser Run (formerly
Browser Rendering). It renders JavaScript-heavy pages on Cloudflare's global network and
returns clean content via a REST API or, inside a Python Worker, the browser binding.
from langchain_cloudflare import CloudflareBrowserRunLoader
# Single page -> markdown
loader = CloudflareBrowserRunLoader(
urls=["https://developers.cloudflare.com/workers-ai/"],
mode="markdown",
)
docs = loader.load()
# Multi-page crawl -> knowledge base (REST-only; async job with polling)
loader = CloudflareBrowserRunLoader(
urls=["https://developers.cloudflare.com/cloudflare-one/"],
mode="crawl",
crawl_limit=50,
crawl_depth=2,
crawl_options={"source": "sitemaps"}, # any other /crawl body option
)
docs = loader.load()
# Scrape specific elements with CSS selectors
loader = CloudflareBrowserRunLoader(
urls=["https://example.com/pricing"],
mode="scrape",
elements=[{"selector": "h1"}, {"selector": ".plan-card"}],
)
docs = loader.load() # one Document per matched selector group
# Async support
docs = await loader.aload()
Supported modes:
| Mode | Endpoint | Description |
|---|---|---|
markdown |
/markdown |
Clean markdown from any page |
crawl |
/crawl |
Multi-page crawl with async polling (REST-only) |
scrape |
/scrape |
CSS selector-based element extraction |
content |
/content |
Raw rendered HTML |
Inside a Python Worker, pass the browser binding instead of REST credentials
(async only — use aload()/alazy_load(), not load()/lazy_load()):
loader = CloudflareBrowserRunLoader(
urls=["https://example.com"], mode="markdown", binding=env.BROWSER
)
docs = await loader.aload()
Pass browser="kitesurf" to use Cloudflare's stateless, agent-optimized browser
runtime instead of full Chromium (REST-only — not reachable via the quickAction()
binding, since it's a URL query parameter with no equivalent in the binding's params
object):
loader = CloudflareBrowserRunLoader(
urls=["https://example.com"], mode="markdown", browser="kitesurf"
)
Browser Run (Agent Tool)
CloudflareBrowserRunTool gives LangGraph
agents the ability to interact with the live web.
from langchain_cloudflare import CloudflareBrowserRunTool
# Read any page as markdown
tool = CloudflareBrowserRunTool(mode="markdown")
content = tool.invoke({"url": "https://example.com"})
# AI-powered structured data extraction
tool = CloudflareBrowserRunTool(
mode="json",
json_prompt="Extract the company name, pricing plans, and key features.",
)
data = tool.invoke({"url": "https://www.cloudflare.com/plans/"})
# Combined-format snapshot (markdown + screenshot in one call)
tool = CloudflareBrowserRunTool(
mode="snapshot", snapshot_formats=["markdown", "screenshot"]
)
snapshot = tool.invoke({"url": "https://example.com"})
# Accessibility tree (roles, names, states, hierarchy)
tool = CloudflareBrowserRunTool(mode="accessibility_tree")
tree = tool.invoke({"url": "https://example.com"})
# Use multiple tools in a LangGraph agent
from langgraph.prebuilt import ToolNode
tools = [
CloudflareBrowserRunTool(mode="markdown"),
CloudflareBrowserRunTool(mode="json", json_prompt="Extract key facts."),
CloudflareBrowserRunTool(mode="links"),
]
tool_node = ToolNode(
tools
) # each tool auto-named: cloudflare_browser_run_markdown, etc.
Supported modes:
| Mode | Endpoint | Description |
|---|---|---|
markdown |
/markdown |
Read any webpage as markdown |
json |
/json |
AI-powered structured data extraction |
links |
/links |
Discover all links on a page |
screenshot |
/screenshot |
Capture screenshot (base64 PNG) |
pdf |
/pdf |
Generate PDF (base64) |
snapshot |
/snapshot |
Multiple page formats in one call |
accessibility_tree |
/accessibilityTree |
Accessibility tree as JSON |
Inside a Python Worker, pass the browser binding instead of REST credentials
(async only — use ainvoke(), not invoke()). This calls Browser Run's
quickAction() RPC method instead of the REST API:
tool = CloudflareBrowserRunTool(mode="markdown", binding=env.BROWSER)
result = await tool.ainvoke({"url": "https://example.com"})
The browser binding requires a compatibility_date of 2026-03-24 or later
and, in local development, "remote": true (quickAction() isn't supported in
local simulation):
// wrangler.jsonc
{
"compatibility_date": "2026-03-24",
"browser": { "binding": "BROWSER", "remote": true }
}
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)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_cloudflare-0.3.9.tar.gz.
File metadata
- Download URL: langchain_cloudflare-0.3.9.tar.gz
- Upload date:
- Size: 288.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf812066cc944861e10ec9b8935f3c86c85659fc945c4a53b72485af0e333118
|
|
| MD5 |
a5ef13e963dd00fcb880cd4e161d9682
|
|
| BLAKE2b-256 |
b9f5e13697ccc1ccba18eb4c8ec92af12468551bc272fdc121fb0e6cc1c76790
|
File details
Details for the file langchain_cloudflare-0.3.9-py3-none-any.whl.
File metadata
- Download URL: langchain_cloudflare-0.3.9-py3-none-any.whl
- Upload date:
- Size: 70.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9da96559a1518325ff6164ac512bfdce71b75dcd3422a560a7041c4bfd7a006d
|
|
| MD5 |
d350e73a6e767dd183e966e0aa3487cc
|
|
| BLAKE2b-256 |
3058096067178647173ea1ffce4d0641b7e760f95fe2d6a52f6aa9c8b71a5021
|