Make a function reusable
Add typed inputs and outputs to a Python function, publish it once and call it from the CLI or Python.
A saved CLI login, a project and a running CPU worker.
A Program wraps a Python function with typed Pydantic input and output models. You publish the function once, then call it from the CLI or Python with new inputs whenever you need.
1. Write a typed Python function
Define a function that takes a Pydantic BaseModel and returns a BaseModel. Here is double from compute/platform/examples/programs/jobs.py:
Keep these two rules in mind:
- Return values vs. files: Return small structured data (like numbers, flags, or summaries) in the Pydantic model. Write large outputs (like trajectories or structures) to
results/as files. - Reject typos early: Setting
extra="forbid"andstrict=Trueon your model catches misspelled parameters before a worker starts running.
2. Publish and run from the CLI
Publish jobs:double as version 1 of example.double, then submit a JSON input:
The worker validates {"value": 21}, runs double, uploads results/value.txt, and returns the structured output:
3. Call the Program from Python
You can look up and submit any published Program from Python using your saved CLI login:
Note: Run this script from
compute/platform/examples/programs/(or with that folder onPYTHONPATH) soimport jobsresolves.Clientis a regular object, not awithcontext manager.
To see which Programs are already published in your project, run:
4. Publish new versions safely
Published Program versions are immutable:
- Same code, new inputs: Call
client.submit(program, Number(value=5))—no republish needed. - Changed code or dependencies: Publish a new version (
--version 2orexample.double@2). Existing workflows pinned toexample.double@1continue to work without breaking.
Complete source for this tutorial (including the multi-step functions used in Build a scientific pipeline):