Skip to content

feat(cloudflare/workers): Python Workers (main: worker.py) - #861

Merged
sam-goodwin merged 6 commits into
mainfrom
claude/python-cloudflare-workers-7ccf74
Jul 17, 2026
Merged

sam-goodwin merged 6 commits into
mainfrom
claude/python-cloudflare-workers-7ccf74

Conversation

@sam-goodwin

@sam-goodwin sam-goodwin commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Deploy Cloudflare Python Workers by pointing main at a .py file, and embed cloudflare-tools as a git submodule workspace (same monorepo-in-monorepo setup as distilled).

Docs: new Python Workers guide (website/src/content/docs/cloudflare/compute/python-workers.mdx) + @section Python Workers on the Worker JSDoc.

Usage

A Python Worker is a regular Cloudflare.Worker whose main is a .py file. It is always an external (async-style) Worker — no inline Effect implementation — and bindings are declared with env:

// alchemy.run.ts
const cache = yield* Cloudflare.KV.Namespace("Cache");

const worker = yield* Cloudflare.Worker("Worker", {
  main: "./src/worker.py",
  env: { CACHE: cache, GREETING: "Hello from Python!" },
});
# src/worker.py — sibling .py files upload too and import by relative path
from workers import Response, WorkerEntrypoint

class Default(WorkerEntrypoint):
    async def fetch(self, request):
        cached = await self.env.CACHE.get("greeting")
        return Response(cached or self.env.GREETING)

Dependencies come from a pyproject.toml next to the entry, vendored with uv (>= 0.8.10) on deploy:

# src/pyproject.toml
[project]
name = "my-worker"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = ["fastapi", "httpx", "humanize"]

Python version follows the compatibility date/flags (3.13 for dates >= 2025-09-29 — the default; no_python_workers_20250116 pins 3.12), selecting the matching uv interpreter (cpython-3.1x-emscripten-wasm32-musl) and Pyodide wheel index. alchemy dev serves the same module graph from local workerd (built-in Pyodide), rebuilding on .py/pyproject.toml changes.

How it works

  • A .py entry skips rolldown entirely: the entry plus every sibling .py file upload as text/x-python modules (main_module = the entry), and python_workers is appended to the compatibility flags. An inline Effect implementation is a deploy-time error.
  • Vendoring mirrors pywrangler: uv pip compile against the Pyodide wheel index → pylock.toml → uv pip install --no-build into an emscripten cross-venv → site-packages uploads as python_modules/<relpath> (application/octet-stream; the workers-runtime-sdk JS shims under python_modules/workers/ as ESM). Cached under .alchemy/python/ on the pyproject hash; the managed workers-runtime-sdk package is always included.
  • A prebuilt python_modules/ directory (e.g. from pywrangler sync) is uploaded as-is without invoking uv — the escape hatch for externally-managed deps.
  • Local dev maps .py → PythonModule / vendored files → Data for workerd, with a filesystem watcher replacing the rolldown watch stream.

Verified live (test/Cloudflare/Workers/PythonWorker.test.ts, PythonWorkerLocal.test.ts): multi-module deploy with env bindings, uv-vendored humanize importable at runtime, and local workerd serving — plus a clean run of the existing Worker/PrebuiltWorker suites.

cloudflare-tools submodule

  • cloudflare-tools/packages/* (+ vendor/*, tools/*) join the bun workspace; the catalog pins for @distilled.cloud/cloudflare-runtime, -rolldown-plugin, -vite-plugin become workspace:*, and their named catalogs (effect, workers) merge into the root manifest.
  • Root and packages/alchemy tsconfigs reference the submodule's composite leaf configs; the prepare hook builds distilled's cloudflare lib then the cloudflare-tools dists on every install (the runtime has no bun export condition, so its tsdown dist/ must exist for type-checking).
  • Includes alchemy-run/cloudflare-tools#71 (typed RemoteWorker dts), which this monorepo's tsc -b surfaced.

🤖 Generated with Claude Code

sam-goodwin and others added 3 commits July 17, 2026 13:34
Monorepo-in-monorepo, same as distilled: cloudflare-tools/packages/* join
the bun workspace (catalog pins for @distilled.cloud/cloudflare-runtime,
-rolldown-plugin, -vite-plugin become workspace:*), its packages are
referenced from the root and packages/alchemy tsconfigs, and the root
build drives their build scripts.

Co-Authored-By: Claude Fable 5 <[email protected]>
A .py entry deploys a Python Worker: no bundling — the entry plus
sibling .py files upload as text/x-python modules, python_workers is
appended to the compatibility flags, and dependencies are vendored with
uv (pyproject.toml -> pylock.toml -> emscripten cross-venv ->
python_modules/) against the Pyodide wheel index, mirroring pywrangler.
A prebuilt python_modules/ directory (e.g. from pywrangler sync) is
uploaded as-is without invoking uv. Local dev serves the same module
graph through workerd's built-in Pyodide (PythonModule module type) with
a filesystem watcher in place of the rolldown watch stream.

Co-Authored-By: Claude Fable 5 <[email protected]>
CI runs `bun tsc -b` right after `bun install`; alchemy's typecheck
resolves @distilled.cloud/cloudflare-runtime types from its tsdown-built
dist/, so the prepare hook builds the submodule packages after every
install.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alchemy-version-bot

alchemy-version-bot Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Install the packages built from this commit:

alchemy

bun add alchemy@https://pkg.ing/alchemy/dfaff80

@alchemy.run/better-auth

bun add @alchemy.run/better-auth@https://pkg.ing/@alchemy.run/better-auth/dfaff80

@alchemy.run/pr-package

bun add @alchemy.run/pr-package@https://pkg.ing/@alchemy.run/pr-package/dfaff80

sam-goodwin and others added 3 commits July 17, 2026 13:58
The runtime's tsdown dts generation resolves @distilled.cloud/cloudflare
types from distilled's lib/; on a cold install (CI) that lib doesn't
exist yet and the annotated Credentials requirement degraded to any.

Co-Authored-By: Claude Fable 5 <[email protected]>
…Python Workers

Encodes the supported-package surface in one live fixture: fastapi
served via the runtime asgi module, pydantic request validation, numpy
(compiled emscripten-wasm32 wheel — proves vendored binary wheels match
the deployed Pyodide ABI), httpx async outbound HTTP, and env bindings
threaded through request.scope. Adds the ASGI/FastAPI pattern to the
Python Workers guide.

Co-Authored-By: Claude Fable 5 <[email protected]>
@sam-goodwin
sam-goodwin merged commit 75952e4 into main Jul 17, 2026
8 checks passed
@sam-goodwin
sam-goodwin deleted the claude/python-cloudflare-workers-7ccf74 branch July 17, 2026 21:43
sam-goodwin added a commit that referenced this pull request Oct 9, 2026
…ith exports

Point distilled at main for exports on dispatch uploads (#860) and the
dispatch_namespace field on namespace listings (#861). Dispatch user
Workers now upload exports instead of migrations, which removes the
migration-tag bookkeeping and its error-message recovery. Namespace
listings are scoped to the Worker's own context, so an account Worker and
a dispatch Worker with the same script name no longer see each other's
classes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant