Skip to content

Commit dc8d6cf

Browse files
committed
fix langchain example
1 parent 155eed7 commit dc8d6cf

6 files changed

Lines changed: 29 additions & 24 deletions

File tree

05-langchain/.dev.vars

Lines changed: 0 additions & 1 deletion
This file was deleted.

05-langchain/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# LangChain Example
22

3-
** Currently this example is broken. **
4-
5-
Warning: Python support in Workers is experimental and things will break. This
6-
example is meant for reference only right now; you should be prepared to update
7-
your code between now and official release time as APIs may change.
3+
This example shows how to use LangChain with Cloudflare Workers AI binding.
84

95
## How to Run
106

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

1713
You can also run `uv run pywrangler deploy` to deploy the example.
14+
15+
Because this example uses a remote Workers AI binding, local development requires
16+
`npx wrangler login`.
17+
18+
## Notes
19+
20+
- If you want the lower-level direct binding example, see [`09-workers-ai/`](../09-workers-ai).

05-langchain/pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ description = "Python langchain example"
55
readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
8-
"langchain_core",
9-
"langchain_openai"
8+
"langchain-cloudflare",
9+
"langchain-core",
10+
"workers-runtime-sdk>=1.6.8",
1011
]
1112

1213
[dependency-groups]
1314
dev = [
1415
"workers-py",
15-
"workers-runtime-sdk"
16+
"workers-runtime-sdk",
1617
]

05-langchain/src/worker.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
from langchain_cloudflare import ChatCloudflareWorkersAI
2+
from langchain_core.output_parsers import StrOutputParser
13
from langchain_core.prompts import PromptTemplate
2-
from langchain_openai import OpenAI
34
from workers import Response, WorkerEntrypoint
45

56

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

14-
res = await chain.ainvoke({"profession": "electrician"})
15-
return Response(res.split(".")[0].strip())
19+
result = await chain.ainvoke({"profession": "electrician"})
20+
return Response.json({"result": result})

05-langchain/wrangler.jsonc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
"$schema": "node_modules/wrangler/config-schema.json",
33
"name": "hello-langchain",
44
"main": "src/worker.py",
5-
"compatibility_date": "2025-11-02",
5+
"compatibility_date": "2026-07-29",
66
"compatibility_flags": [
77
"python_workers"
88
],
9-
"rules": [
10-
{
11-
"type": "Data",
12-
"globs": ["python_modules/**/*.js"],
13-
"fallthrough": true
14-
}
15-
],
9+
"ai": {
10+
"binding": "AI",
11+
"remote": true
12+
},
1613
"observability": {
1714
"enabled": true
1815
}

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_04_query_d1(init_db, dev_server):
7373
]
7474

7575

76-
@pytest.mark.xfail(reason="Not working")
76+
@pytest.mark.xfail(reason="Requires remote bindings")
7777
def test_05_langchain(dev_server):
7878
pass
7979

0 commit comments

Comments
 (0)