You can run a function on its own, without a routine. See Run a function directly.
Install and create a function
Install the stable helper package with Python 3.11–3.13:@script decorator. It can use def or async def. The first parameter is a Pydantic input model; ctx: Context is optional. Fields with defaults are optional inputs.
The return annotation must be a Pydantic output model, Output | None, None, or Dataset[Item]. Use Output | None if returning no value is intentional; returning None from a required Output fails validation. A dataset can also use Dataset[Item] | None to publish items pushed through ctx.dataset.push(...).
Keep the function in one file. Helper modules next to it are not importable. Model schemas support common scalar types, lists, and nested models, plus date, datetime, UUID, URL, and email formats. Regex patterns and recursive models are not supported.
ctx.log.info(...), ctx.log.warning(...), ctx.log.error(...), and print(...) write log output. The platform sets the time limit. Do not print secrets.
Return data, keep state, control a device
These parts of theContext are optional. Use only what your function needs.
Return a list
Return aDataset when the result is a list. Mark the field that identifies each item with key_field(). It must serialize to a string or an integer.
Call ctx.dataset.push(item) to add each item as you find it. If the run stops early, the items you already pushed are kept.
Fail on purpose or return a partial result
RaiseScriptError(code, message) for a failure you expect. Call ctx.partial("reason") when the result is incomplete but still useful; the run ends as partial and shows your reason.
Keep data between runs
ctx.state.previous holds the data saved by the last successful run of the same step, or None on the first run. Save new data with ctx.state.save(...). Only a successful run saves state. If you change the shape of the saved data, pass a version with ctx.state.save(data, schema=2) and read the old one from ctx.state.previous_schema. Nothing on disk survives a run.
Control the device
When the routine step has a device, callctx.device(). The device is connected once and reused for every call in the run. A run without a device raises an error that says so.
pip install "mobilerun-functions[device]==0.1.0".
Limits
Do not print secrets. Log output is stored as you write it.
Validate before uploading
Inspect the file locally:0 and prints JSON with ok: true, its entry point, and its input and output schemas. An invalid file exits with code 3 and prints the issues, including line numbers and repair hints where available.
Inspection imports your file, so top-level code runs during the check. Keep work inside the entry point. Inspection checks the function contract; it does not prove that its business logic succeeds.
ok: true, outcome: "succeeded", result: {"message": "Hello, Ada!"}, and the validated input.