Skip to main content

gen-worker

Python SDK for writing endpoints that run on Cozy's worker pool. You write one decorated function or class; the SDK handles discovery, scheduling, model download + placement, cancellation, file I/O, streaming, and reporting back to the control plane.

Install

pip install gen-worker[torch]   # for PyTorch inference/training
pip install gen-worker          # plain Python (e.g. API-proxy endpoints)

Optional extras: [images] / [audio] / [video] for media I/O, [vision] for torchvision.

Hello world

pyproject.toml — the one config value:

[tool.gen_worker]
main = "myendpoint.main"

main.py:

import msgspec
from gen_worker import RequestContext, endpoint

class Input(msgspec.Struct):
    prompt: str

class Output(msgspec.Struct):
    text: str

@endpoint
def echo(ctx: RequestContext, payload: Input) -> Output:
    return Output(text=f"got: {payload.prompt}")

Run it locally, no orchestrator:

gen-worker run --payload '{"prompt": "hello"}'

cozyctl build / cozyctl deploy take it from here — the full path to a deployed, billed endpoint is tensorhub docs/writing-endpoints.md.

Adding a model

Hold state in a class: setup() runs once, every public method is one routable function. The worker downloads the binding, constructs the pipeline from the setup() annotation, and owns device placement + low-VRAM offload — endpoint code never touches .to("cuda") or offload config.

from diffusers import StableDiffusionXLPipeline
from gen_worker import HF, RequestContext, Resources, endpoint

@endpoint(
    model=HF("stabilityai/stable-diffusion-xl-base-1.0", dtype="bf16"),
    resources=Resources(gpu=True),
)
class Generate:
    def setup(self, pipeline: StableDiffusionXLPipeline) -> None:
        self.pipeline = pipeline

    def generate(self, ctx: RequestContext, payload: Input) -> Output:
        view = ctx.for_request(self.pipeline, seed=42)
        image = view(payload.prompt, generator=view.generator).images[0]
        return Output(text=ctx.save_image(image).ref)

Resources declares only what the endpoint CANNOT run without (gpu, gpu_count, libraries, vcpus) plus requires= — the function scope of the one requirement vocabulary, "sm89+, vram80g" style, minimum and recommended. A minimum gates a config WRITE, never execution: the worker runs on whatever machine it is handed, degraded and complaining. Handlers are exactly (self, ctx, payload); per-request state (sampler, seed, scheduler) lives in a ctx.for_request view over shared weights — never assigned onto the instance.

Bindings: HF(id, revision=, dtype=, subfolder=, files=, storage_dtype=), Hub(ref, release=, storage_dtype=), Civitai(id, version=), ModelScope(id, ...). The slot name comes from the models={} key or the setup() parameter — never a constructor argument. storage_dtype="fp8" keeps denoiser weights in fp8-E4M3 storage with per-layer upcast to the compute dtype (half the VRAM on any card); fp8-stored artifacts get the same treatment automatically. Quantization itself is ahead-of-time only — a conversion endpoint produces the artifact, never setup() (th#1803) — and the flavor axis is DELETED (§1.32(d), pgw#1148, finished by pgw#1319: the producer DECLARES a precision_class and nothing infers one from a label): selection within a tag group is tensor-layout-contract compatibility, declared per slot as Slot(layouts=…) (§1.33, pgw#1143). See docs/endpoint-authoring.md.

Curated checkpoint selection is a runtime payload argument: a handler declares model: SomeModelChoice (a ModelChoice enum of Model rows, each carrying a ModelRef binding + typed per-model defaults) and reads payload.model.defaults typed — one generate(model=) replaces N near-identical functions. model: SomeModelChoice | ModelRef opens BYOM. Streaming = an async-generator handler. Engine-hosted endpoints declare runtime="vllm" and get a booted, health-checked server subprocess injected into setup().

Slot(pipeline_cls, selected_by=, family=, default_checkpoint=) is the hub-resolved alternative to ModelChoice: the model SET lives in platform config, not code, and the COMPONENT TREE (pipeline.unet, pipeline.vae, ...) is derived from the pipeline class and published to the hub — parts are never declared as sibling slots. The per-model config SCHEMA derives from the handler's context annotation (ctx: RequestContext[SdxlDefaults], a gen_worker.families.GenerationDefaults vocabulary); the catalog owns the VALUES and ctx.defaults hands the resolved recipe to the handler typed. family="sdxl" explicitly keeps a non-root/defaultless model lane inside that architecture's binding gate; family="" explicitly marks a shared auxiliary as family-agnostic. Omitting it preserves the compatibility inference.

Full reference: docs/endpoint-authoring.md.

Public surface

  • The decorator + bindings: endpoint, Resources, Compile, HF, Hub, Civitai, ModelScope, ModelRef
  • Model selection: Model, ModelChoice, ModelDefaults, Slot, ResolvedSlot, gen_worker.families.GenerationDefaults
  • Compile envelope (the declared serving region — resolutions, text lengths, guidance, batch): Compile, CompileAxis, AxisClass, DynamicDim, pad_text_sequence; per-request views: ctx.for_request / gen_worker.view
  • Contexts: RequestContext (≤15 members), ConversionContext, DatasetContext, TrainingContext
  • Errors: ValidationError, RetryableError, CanceledError, FatalError
  • Streaming: BatchItemDelta, IncrementalTokenDelta, Done, Error
  • Value types: Asset, ImageAsset, AudioAsset, VideoAsset
  • I/O codecs: gen_worker.io

The conversion ETL (hub ingest, dtype cast / quant, clone, Tensorhub publish) is gen_worker.convert (see docs/convert.md).

Local development

gen-worker run --payload '{"prompt": "hello"}'  # one-shot in-process
gen-worker run --list                            # describe functions (JSON)
gen-worker serve                                 # warm local server
gen-worker invoke <fn> prompt=hello              # client for serve
gen-worker prefetch                              # weights only, no GPU

stdout for results, stderr for events; exit 0 / 1 / 2 / 3 / 130 for success / user-exception / usage / model-resolution / SIGINT. Details: docs/local-dev.md; host contract: docs/host-integration.md.

Running tests

uv run --extra dev pytest

Plain uv run pytest would fall through to a global launcher — always pass --extra dev. Never pip install gen-worker globally: a stale ~/.local install silently shadows the working tree (tests/conftest.py hard-fails if gen_worker resolves outside src/).

Documentation

Examples

  • examples/marco-polo/ — minimal inference endpoint (sync, async, streaming)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gen_worker-0.121.0.tar.gz (2.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gen_worker-0.121.0-py3-none-any.whl (2.2 MB view details)

Uploaded Python 3

File details

Details for the file gen_worker-0.121.0.tar.gz.

File metadata

  • Download URL: gen_worker-0.121.0.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gen_worker-0.121.0.tar.gz
Algorithm Hash digest
SHA256 44c83a999f1d7c692d77ae4f2d7eac27a803e0fc83bc4fa63168fabc304a7446
MD5 4cc00dcdb688c11e71af14e01c3de5c8
BLAKE2b-256 c9c5546f6a8b879cc81a21fbb524495ebff4fda64e327809b87be95fba3e575b

See more details on using hashes here.

Provenance

The following attestation bundles were made for gen_worker-0.121.0.tar.gz:

Publisher: publish.yaml on cozy-creator/python-gen-worker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gen_worker-0.121.0-py3-none-any.whl.

File metadata

  • Download URL: gen_worker-0.121.0-py3-none-any.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gen_worker-0.121.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9bbef78783a77a375f035e2ce821ac236c861d53ade214909e87cba074061db
MD5 165e3b39988dd0410d85f53396d80aae
BLAKE2b-256 5eb3b33fe4872e8050e635036d73ad27502899fbbe49cca46e648204a72046e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for gen_worker-0.121.0-py3-none-any.whl:

Publisher: publish.yaml on cozy-creator/python-gen-worker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.128.0

2 files

0.127.2

2 files

0.127.1

2 files

0.127.0

2 files

0.126.0

2 files

0.125.0

2 files

0.124.0

2 files

0.123.0

2 files

0.122.0

2 files

This release

0.121.0 This release

2 files

0.120.0

2 files

0.119.0

2 files

0.118.0

2 files

0.117.0

2 files

0.116.0

2 files

0.115.0

2 files

0.114.3

2 files

0.114.2

2 files

0.114.1

2 files

0.114.0

2 files

0.113.2

2 files

0.113.1

2 files

0.113.0

2 files

0.112.0

2 files

0.111.0

2 files

0.110.0

2 files

0.109.0

2 files

0.108.0

2 files

0.106.0

2 files

0.105.0

2 files

0.104.0

2 files

0.103.0

2 files

0.102.0

2 files

0.101.0

2 files

0.100.1

2 files

0.100.0

2 files

0.99.0

2 files

0.98.0

2 files

0.97.0

2 files

0.96.3

2 files

0.96.2

2 files

0.96.1

2 files

0.96.0

2 files

0.95.1

2 files

0.95.0

2 files

0.94.1

2 files

0.94.0

2 files

0.93.3

2 files

0.93.2

2 files

0.93.1

2 files

0.93.0

2 files

0.92.2

2 files

0.92.1

2 files

0.92.0

2 files

0.91.4

2 files

0.91.3

2 files

0.91.2

2 files

0.90.6

2 files

0.90.5

2 files

0.90.4

2 files

0.90.3

2 files

0.90.2

2 files

0.90.1

2 files

0.90.0

2 files

0.89.0

2 files

0.88.0

2 files

0.87.0

2 files

0.86.0

2 files

0.85.0

2 files

0.84.0

2 files

0.83.0

2 files

0.82.0

2 files

0.81.0

2 files

0.80.0

2 files

0.79.0

2 files

0.78.0

2 files

0.77.0

2 files

0.76.8

2 files

0.76.7

2 files

0.76.6

2 files

0.76.5

2 files

0.76.3

2 files

0.76.2

2 files

0.76.0

2 files

0.75.1

2 files

0.70.5

2 files

0.70.4

2 files

0.70.3

2 files

0.70.2

2 files

0.70.1

2 files

0.70.0

2 files

0.68.0

2 files

0.67.4

2 files

0.67.3

2 files

0.67.1

2 files

0.67.0

2 files

0.66.0

2 files

0.65.0

2 files

0.64.0

2 files

0.63.0

2 files

0.61.0

2 files

0.60.1

2 files

0.60.0

2 files

0.58.3

2 files

0.58.2

2 files

0.58.1

2 files

0.58.0

2 files

0.56.3

2 files

0.56.2

2 files

0.56.1

2 files

0.56.0

2 files

0.55.0

2 files

0.54.0

2 files

0.52.3

2 files

0.52.2

2 files

0.52.1

2 files

0.52.0

2 files

0.51.0

2 files

0.50.2

2 files

0.50.1

2 files

0.50.0

2 files

0.48.2

2 files

0.48.1

2 files

0.48.0

2 files

0.47.0

2 files

0.46.0

2 files

0.45.0

2 files

0.44.0

2 files

0.43.1

2 files

0.43.0

2 files

0.42.0

2 files

0.41.0

2 files

0.40.7

2 files

0.40.5

2 files

0.40.3

2 files

0.40.2

2 files

0.40.1

2 files

0.40.0

2 files

0.39.4

2 files

0.39.3

2 files

0.39.2

2 files

0.39.1

2 files

0.39.0

2 files

0.38.7

2 files

0.38.6

2 files

0.38.5

2 files

0.38.4

2 files

0.38.3

2 files

0.38.2

2 files

0.38.1

2 files

0.38.0

2 files

0.37.5

2 files

0.37.4

2 files

0.37.3

2 files

0.37.2

2 files

0.37.1

2 files

0.37.0

2 files

0.36.10

2 files

0.36.9

2 files

0.36.8

2 files

0.36.7

2 files

0.36.6

2 files

0.36.5

2 files

0.36.4

2 files

0.36.3

2 files

0.36.2

2 files

0.36.1

2 files

0.36.0

2 files

0.35.2

2 files

0.35.1

2 files

0.35.0

2 files

0.34.0

2 files

0.33.0

2 files

0.32.2

2 files

0.32.1

2 files

0.32.0

2 files

0.31.0

2 files

0.30.2

2 files

0.30.1

2 files

0.30.0

2 files

0.29.0

2 files

0.28.1

2 files

0.28.0

2 files

0.27.0

2 files

0.26.11

2 files

0.26.10

2 files

0.26.9

2 files

0.26.8

2 files

0.26.7

2 files

0.26.6

2 files

0.26.5

2 files

0.26.4

2 files

0.26.3

2 files

0.26.2

2 files

0.26.1

2 files

0.26.0

2 files

0.25.2

2 files

0.25.1

2 files

0.25.0

2 files

0.24.2

2 files

0.22.6

2 files

0.22.4

2 files

0.22.3

2 files

0.18.2

2 files

0.18.1

2 files

0.17.5

2 files

0.17.4

2 files

0.17.2

2 files

0.17.0

2 files

0.16.0

2 files

0.15.0

2 files

0.14.15

2 files

0.14.14

2 files

0.14.13

2 files

0.14.10

2 files

0.14.9

2 files

0.14.8

2 files

0.14.6

2 files

0.14.5

2 files

0.14.3

2 files

0.14.0

2 files

0.13.34

2 files

0.13.33

2 files

0.13.32

2 files

0.13.28

2 files

0.13.27

2 files

0.13.26

2 files

0.13.25

2 files

0.13.24

2 files

0.13.22

2 files

0.13.21

2 files

0.13.19

2 files

0.13.18

2 files

0.13.17

2 files

0.13.16

2 files

0.13.15

2 files

0.13.14

2 files

0.13.13

2 files

0.13.12

2 files

0.13.11

2 files

0.13.10

2 files

0.13.9

2 files

0.13.8

2 files

0.13.7

2 files

0.13.6

2 files

0.13.5

2 files

0.13.4

2 files

0.13.3

2 files

0.13.2

2 files

0.13.1

2 files

0.13.0

2 files

0.12.3

2 files

0.12.2

2 files

0.12.1

2 files

0.12.0

2 files

0.11.2

2 files

0.11.1

2 files

0.11.0

2 files

0.10.0

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.3

2 files

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

0.7.43

2 files

0.7.42

2 files

0.7.41

2 files

0.7.40

2 files

0.7.38

2 files

0.7.37

2 files

0.7.36

2 files

0.7.35

2 files

0.7.34

2 files

0.7.33

2 files

0.7.32

2 files

0.7.31

2 files

0.7.30

2 files

0.7.29

2 files

0.7.28

2 files

0.7.27

2 files

0.7.26

2 files

0.7.25

2 files

0.7.24

2 files

0.7.23

2 files

0.7.22

2 files

0.7.21

2 files

0.7.20

2 files

0.7.19

2 files

0.7.18

2 files

0.7.17

2 files

0.7.16

2 files

0.7.15

2 files

0.7.14

2 files

0.7.13

2 files

0.7.12

2 files

0.7.11

2 files

0.7.10

2 files

0.7.9

2 files

0.7.8

2 files

0.7.7

2 files

0.7.6

2 files

0.7.5

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.34

2 files

0.5.32

2 files

0.5.31

2 files

0.5.30

2 files

0.5.29

2 files

0.5.28

2 files

0.5.27

2 files

0.5.26

2 files

0.5.25

2 files

0.5.24

2 files

0.5.23

2 files

0.5.22

2 files

0.5.21

2 files

0.5.20

2 files

0.5.19

2 files

0.5.18

2 files

0.5.17

2 files

0.5.16

2 files

0.5.15

2 files

0.5.14

2 files

0.5.13

2 files

0.5.12

2 files

0.5.11

2 files

0.5.9

2 files

0.5.8

2 files

0.5.7

2 files

0.5.6

2 files

0.5.5

2 files

0.5.4

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.11

2 files

0.3.10

2 files

0.3.9

2 files

0.3.8

2 files

0.3.7

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page