Skip to content

Generate typed bindings for a Python Worker with wynd types

wynd types reads your Wrangler config and writes env.py, a Python class that lists every binding with its type. Pass it to your entrypoints, and your editor and type checker know what self.env holds. It works with crosswind-workers only, because the types it writes come from workers.bindings.

  • env.py goes in your source directory, next to the entry module.
  • wynd dev, wynd deploy, wynd versions upload and wynd preview regenerate it whenever its contents would change.
  • wynd types --check fails when the file is out of date, for CI.
  • Commit env.py.
  • With a cloudflare.config.py, wynd types reads the config Wynd compiles from it. The types are the same.

Generate env.py

With this config:

{
  "name": "typed",
  "main": "src/entry.py",
  "compatibility_date": "2026-09-08",
  "compatibility_flags": ["python_workers"],
  "kv_namespaces": [{ "binding": "CACHE", "id": "<your KV namespace id>" }],
  "durable_objects": { "bindings": [{ "name": "COUNTER", "class_name": "Counter" }] },
  "migrations": [{ "tag": "v1", "new_sqlite_classes": ["Counter"] }],
  "ai": { "binding": "AI" },
  "vars": { "GREETING": "hi", "LIMITS": { "uploads": 10, "users": 3 } }
}
name = "typed"
main = "src/entry.py"
compatibility_date = "2026-09-08"
compatibility_flags = ["python_workers"]

[[kv_namespaces]]
binding = "CACHE"
id = "<your KV namespace id>"

[[durable_objects.bindings]]
name = "COUNTER"
class_name = "Counter"

[[migrations]]
tag = "v1"
new_sqlite_classes = ["Counter"]

[ai]
binding = "AI"

[vars]
GREETING = "hi"
LIMITS = { uploads = 10, users = 3 }
from wynd.config import Config, Worker, bindings, exports

config = Config(
    worker=Worker(
        name="typed",
        entrypoint="src/entry.py",
        compatibility_date="2026-09-08",
        env={
            "CACHE": bindings.KV(id="<your KV namespace id>"),
            "COUNTER": bindings.DurableObject("Counter"),
            "AI": bindings.AI(),
            "GREETING": bindings.Text("hi"),
            "LIMITS": bindings.Json({"uploads": 10, "users": 3}),
        },
        exports={"Counter": exports.DurableObject()},
    ),
)

and a .dev.vars file that sets API_TOKEN=..., run:

uvx wynd types
wynd: wrote src/env.py
src/env.py
# Generated by wynd types. Do not edit.
from __future__ import annotations

from typing import TYPE_CHECKING, Protocol

from workers.bindings import Binding, DurableObjectNamespace, KVNamespace

if TYPE_CHECKING:
    from entry import Counter


class Env(Protocol):
    CACHE: KVNamespace
    COUNTER: DurableObjectNamespace[Counter]
    AI: Binding
    GREETING: str
    LIMITS: dict[str, int]
    API_TOKEN: str

Use it in your entry module:

from env import Env
from workers import DurableObject, WorkerEntrypoint


class Counter(DurableObject[Env]): ...


class Default(WorkerEntrypoint[Env]): ...

from env import Env works at runtime too, because Wrangler uploads env.py with the rest of your source. The Durable Object import sits under TYPE_CHECKING, so env.py and your entry module don't import each other at runtime.

Types for each binding

Config entry Type
kv_namespaces KVNamespace
d1_databases D1Database
r2_buckets R2Bucket
queues.producers Queue
durable_objects.bindings for a class in this Worker DurableObjectNamespace[YourClass]
durable_objects.bindings with script_name DurableObjectNamespace
services, assets Fetcher
workflows Workflow
vectorize Vectorize
analytics_engine_datasets AnalyticsEngineDataset
images ImagesBinding
ratelimits RateLimit
ai AI
version_metadata JsDict
Any other binding, such as browser Binding
A text var, and each name in .dev.vars str
A JSON var The type of its value, such as int, list[str] or dict[str, int]

Attribute names are the binding names exactly as written in the config. Wynd leaves out a name that isn't a valid Python identifier, such as MY-KV, so read it with getattr(self.env, "MY-KV"). A list or object var whose items have different types gets Any for its items.

Wynd imports Durable Object classes from your entry module. A class defined in another module still works if the entry module imports it.

Environments and configs

Pass the same flags you use for dev and deploy. Env then lists the bindings of that environment:

uvx wynd types --env staging
uvx wynd types --config wrangler.staging.jsonc

There is one env.py per source directory. If environments have different bindings, generate it for the one you check types against.

Check it in CI

uvx wynd types --check

The command exits with code 0 when env.py matches the config. Otherwise it exits with code 1 and prints:

wynd: error: src/env.py is out of date. Run `wynd types` and commit it.

The generated file follows ruff's formatter and import sorting, so running ruff format over your project doesn't make the check fail.

Troubleshooting

wynd: error: src/env.py exists and wynd types didn't write it.

Wynd only overwrites an env.py whose first line is # Generated by wynd types., so it never replaces a module you wrote. Rename your env.py, then run uvx wynd types again.

wynd: error: wynd types needs crosswind-workers, because workers-runtime-sdk has no workers.bindings.

The project uses the stock SDK. Switch to crosswind-workers with Migrate from workers-runtime-sdk, or skip wynd types. Every other Wynd command works with the stock SDK.

A secret is missing from Env

Wynd reads secret names from .dev.vars next to the Wrangler config or cloudflare.config.py. Add the secret there with any local value, and run uvx wynd types. A bindings.Secret() in cloudflare.config.py doesn't add the name to Env by itself.