Automation agent toolkit for the Ondemand platform
pip install ondemand-ai==1.12.7 Copy PIP instructions
Python SDK for building automations on the Ondemand platform.
Provides OndemandWorker (a Temporal worker with automatic log capture and graceful shutdown), ActivityReporter (real-time step progress via webhooks), structured logging, R2 artifact storage, and human-in-the-loop approval helpers.
# Full install with Temporal worker support pip install ondemand-ai[worker] # Shared utilities only (logging, R2 storage, artifacts, approvals) pip install ondemand-ai Requirements: Python 3.9+
A minimal automation with one workflow and one activity:
# workflows.py from datetime import timedelta from temporalio import workflow from temporalio.common import RetryPolicy with workflow.unsafe.imports_passed_through(): from activities import process_data, MyInput @workflow.defn class MyWorkflow: @workflow.run async def run(self, input: MyInput) -> dict: result = await workflow.execute_activity( process_data, args=[input], start_to_close_timeout=timedelta(minutes=30), retry_policy=RetryPolicy(maximum_attempts=3), ) return result # activities.py import logging from temporalio import activity from ondemand.worker.activity_reporter import report logger = logging.getLogger("my-automation") @activity.defn async def process_data(input) -> dict: report.step_started("extract", "Extrair Dados") # ... do work ... report.record("extract", "file1.pdf", "success", "Processed OK") report.step_completed("extract", "Extrair Dados") logger.info("Processing complete") # streamed to portal in real-time return {"count": 42} # main.py from ondemand.worker import OndemandWorker from workflows import MyWorkflow from activities import process_data worker = OndemandWorker(name="my-automation") worker.register_workflow(MyWorkflow) worker.register_activity(process_data) worker.run() Architecture Overview The SDK is designed for automations running on GKE Autopilot with KEDA. KEDA scales worker pods from 0 to N based on Temporal task queue depth, with a cooldownPeriod to prevent premature scale-down.
Connects to Temporal, registers workflows and activities, polls a task queue, and handles graceful shutdown on SIGTERM.
from ondemand.worker import OndemandWorker worker = OndemandWorker(name="my-worker") # Register workflows and activities worker.register_workflow(MyWorkflow) worker.register_activity(my_activity) # Start polling (blocking call, runs asyncio event loop) worker.run() Behavior:
Sends step progress updates directly to the portal via STEP_REPORT webhooks. Each call fires immediately -- no batching, no state accumulation. The portal writes to DB and broadcasts via SSE for real-time UI updates.
from ondemand.worker.activity_reporter import report # Report step lifecycle report.step_started("extract", "Extrair Dados", parent="process") report.step_completed("extract", "Extrair Dados", parent="process") report.step_failed("extract", "Extrair Dados", error="Timeout na API", parent="process") report.step_warning("extract", "Extrair Dados", parent="process") report.step_skipped("extract", "Extrair Dados", parent="process") # Attach individual item results to a step report.record( step_id="extract", record_id="invoice_001.pdf", status="success", message="Processado OK", metadata={"pages": 3, "total": 1500.00}, ) Step statuses: RUNNING, SUCCEEDED, FAILED, WARNING, SKIPPED
All methods are no-ops when ONDEMAND_WEBHOOK_URL is not set, making local development seamless.
Python logging handler that captures all log output for portal display and R2 upload. Automatically set up when OndemandWorker starts -- no manual configuration needed.
import logging logger = logging.getLogger("my-automation") @activity.defn async def my_activity(): logger.info("Processing...") # captured + streamed to portal + stored for R2 logger.warning("Watch out") # same return {"result": ...} Behavior:
Console log format: timestamp - module - LEVEL - message
Query-based step tree reporter that stores state inside the Temporal workflow for polling via the Temporal Query API. This is the legacy approach -- new automations should use ActivityReporter for real-time webhook-based updates instead.
Custom logger with a SUCCESS level (25, between INFO and WARNING) and helpers for structured output.
from ondemand.shared import get_logger logger = get_logger(__name__) logger.info("Processing started") logger.success("All files uploaded") # SUCCESS level, green in portal logger.section("Fase 2: Classificacao") # logs "#### Fase 2: Classificacao", cyan in portal logger.step("Extrair", "ABC Corp") # logs "[Extrair] ABC Corp" logger.divider() # logs "============..." logger.summary("Results", {"total": 42, "errors": 0}) with logger.timed("Uploading files"): upload() # logs "#### Uploading files" on entry # logs "SUCCESS - Uploading files completed in 3.2s" on exit # logs "ERROR - Uploading files FAILED after 3.2s" on exception ondemand.shared.r2_storage Upload and download files from Cloudflare R2 (S3-compatible). Uses boto3 under the hood.
from ondemand.shared import get_r2_client, download_input_files, upload_task_artifacts from pathlib import Path # Direct client usage r2 = get_r2_client() r2.upload_file(Path("output.xlsx"), "artifacts/run-123/output.xlsx") r2.download_file("inputs/uuid/data.csv", Path("./downloads/data.csv")) r2.copy_object("inputs/uuid/data.csv", "artifacts/run-123/inputs/data.csv") # Upload raw content with automatic key prefixing and portal notification # Key is built as: artifacts/{ONDEMAND_RUN_ID}/{folder}/{filename} r2.upload_content( content=report_bytes, filename="report.xlsx", folder="reports", notify=True, # POSTs ARTIFACTS_UPLOADED webhook for SSE broadcast ) # Download all file-type inputs from a workflow's input dict downloaded = download_input_files( inputs={"planilha": "inputs/uuid/data.xlsx", "empresa": "ABC"}, dest_dir=Path("./downloads"), ) # downloaded == {"planilha": Path("./downloads/data.xlsx")} # Upload a task's output directory uploaded = upload_task_artifacts( task_output_dir=Path("output/run-123/classify"), run_id="run-123", task_name="classify", exclude=["console.txt"], ) ondemand.shared.approval Pause a workflow and wait for human approval (HITL pattern).
from ondemand import request_approval approval_url, rejection_url = request_approval( message="3 divergencias encontradas. Revisar?", data={"total": 15000, "items": ["NF-001", "NF-002", "NF-003"]}, show_buttons=True, # show approve/reject buttons in portal UI timeout_days=7, # auto-reject after 7 days (default) ) # Send notification however you want (email, Slack, WhatsApp, etc.) send_email(to="reviewer@client.com", body=f"Aprovar: {approval_url}") Behavior:
Manage output directories and pass data between workflow steps.
from ondemand.shared import ( set_run_id, get_run_id, get_run_info, get_output_dir, get_base_output_dir, save_artifact, load_artifact, ) set_run_id("run-123") # Per-task output: output/run-123/extract/ output_dir = get_output_dir("extract") # Shared output: output/run-123/ base_dir = get_base_output_dir() # Save/load JSON artifacts save_artifact({"companies": [...]}, "companies.json") data = load_artifact("companies.json", task="extract") # Run context info = get_run_info() # RunInfo(run_id, process_code, organization_id, started_at) Environment Variables Set by the platform at runtime. For local development, set them manually or use a .env file.
ONDEMAND_RUN_ID and ONDEMAND_WEBHOOK_URL are auto-set by the activity interceptor at runtime -- never set these manually.
ondemand/ ├── __init__.py # Top-level: request_approval, ApprovalRequestError ├── worker/ │ ├── __init__.py # Exports: OndemandWorker, report, ActivityReporter, setup_logging │ ├── base.py # OndemandWorker + activity interceptor + graceful shutdown │ ├── activity_reporter.py # ActivityReporter — real-time step updates via webhook │ ├── logging.py # OndemandLogHandler + _StderrTee + setup_logging() │ └── reporter.py # WorkflowReporter (legacy — for Temporal query-based reporting) └── shared/ ├── __init__.py # Re-exports all shared utilities ├── approval.py # request_approval() for HITL workflows ├── artifacts.py # save_artifact, load_artifact, output dirs, RunInfo ├── logging.py # OndemandLogger with SUCCESS level └── r2_storage.py # R2StorageClient, upload_content, download_input_files Publishing # Bump version in pyproject.toml, then: python -m build python -m twine upload dist/* Requires a PyPI API token configured in ~/.pypirc or via TWINE_PASSWORD.
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.




