Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion 05-langchain/.dev.vars

This file was deleted.

13 changes: 8 additions & 5 deletions 05-langchain/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# LangChain Example

** Currently this example is broken. **

Warning: Python support in Workers is experimental and things will break. This
example is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.
This example shows how to use LangChain with Cloudflare Workers AI binding.

## How to Run

Expand All @@ -15,3 +11,10 @@ Now, if you run `uv run pywrangler dev` within this directory, it should use the
in `wrangler.jsonc` to run the example.

You can also run `uv run pywrangler deploy` to deploy the example.

Because this example uses a remote Workers AI binding, local development requires
`npx wrangler login`.

## Notes

- If you want the lower-level direct binding example, see [`09-workers-ai/`](../09-workers-ai).
7 changes: 4 additions & 3 deletions 05-langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ description = "Python langchain example"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"langchain_core",
"langchain_openai"
"langchain-cloudflare",
"langchain-core",
"workers-runtime-sdk>=1.6.8",
]

[dependency-groups]
dev = [
"workers-py",
"workers-runtime-sdk"
"workers-runtime-sdk",
]
17 changes: 11 additions & 6 deletions 05-langchain/src/worker.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from langchain_cloudflare import ChatCloudflareWorkersAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from workers import Response, WorkerEntrypoint


class Default(WorkerEntrypoint):
async def fetch(self, request):
prompt = PromptTemplate.from_template(
"Complete the following sentence: I am a {profession} and "
"In one sentence, describe a great day in the life of an {profession}."
)
llm = OpenAI(api_key=self.env.API_KEY)
chain = prompt | llm
llm = ChatCloudflareWorkersAI(
model_name="@cf/meta/llama-3.3-70b-instruct-fp8-fast",
binding=self.env.AI,
max_tokens=64,
)
chain = prompt | llm | StrOutputParser()

res = await chain.ainvoke({"profession": "electrician"})
return Response(res.split(".")[0].strip())
result = await chain.ainvoke({"profession": "electrician"})
return Response.json({"result": result})
13 changes: 5 additions & 8 deletions 05-langchain/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
"$schema": "node_modules/wrangler/config-schema.json",
"name": "hello-langchain",
"main": "src/worker.py",
"compatibility_date": "2025-11-02",
"compatibility_date": "2026-07-29",
"compatibility_flags": [
"python_workers"
],
"rules": [
{
"type": "Data",
"globs": ["python_modules/**/*.js"],
"fallthrough": true
}
],
"ai": {
"binding": "AI",
"remote": true
},
"observability": {
"enabled": true
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_04_query_d1(init_db, dev_server):
]


@pytest.mark.xfail(reason="Not working")
@pytest.mark.xfail(reason="Requires remote bindings")
def test_05_langchain(dev_server):
pass

Expand Down
Loading