Developer guides / 01
Designing a Python execution API for untrusted code
A practical architecture for accepting Python jobs asynchronously, tracking their lifecycle, and keeping the runtime outside your application server.
If your product accepts Python from customers, plugins, or AI agents, the first design decision is where that code runs. Starting a subprocess inside the web server is convenient, but it gives submitted code the same host and process boundary as the application that handles requests.
A safer design separates accepting and tracking a job from running the job. That separation gives your product a durable API while letting the execution runtime be designed, tested, and replaced on its own.
Make submission asynchronous
An execution request can take longer than an HTTP request should stay open. Accept the request, validate its limits, record it, and return an identifier. The client can then read the current state or request cancellation.
POST /v1/executionsAuthorization: Bearer $API_KEYIdempotency-Key: customer-job-42Content-Type: application/json
{ "language": "python", "code": "print(6 * 7)", "timeout_ms": 3000, "memory_mb": 128}The response should describe an accepted job, not imply that the program has already completed. Return a stable execution ID and a lifecycle state such as queued; expose a separate read operation for later results. An idempotency key lets a client retry after a network timeout without accidentally creating a second logical job.
The mPaka HTTP API reference documents this submit-and-poll shape, including request limits and error responses.
Persist lifecycle state
The job record should outlive the API process that accepted it. Persist the execution ID, owner, requested limits, current state, timestamps, cancellation request, and eventual result. A worker can claim queued work, while the API reads the same durable record for clients.
That arrangement makes restarts and retries understandable. If a worker disappears, the system can reconcile its lease and decide whether the job should be retried or marked failed. If a client asks whether a job is done, the answer comes from shared state rather than whichever process happens to remember it.
Define legal state transitions centrally. For example, queued may become running or cancelled; a terminal state should not silently become running again. See why execution state should survive process memory for more on this boundary.
Put a real boundary around the runtime
Moving work to a background process is useful, but a worker process on the same host is not automatically a sandbox. The component that runs user code needs a deliberate isolation boundary and limits for CPU, memory, duration, filesystem access, network access, and credentials.
Keep the application-facing contract small. The scheduler should hand a validated job to an executor through an explicit interface, then record the result it gets back. This makes it possible to replace a local development executor with a stronger runtime without changing the public API. It also makes the trust boundary easier to review: the API accepts work; the executor runs it.
An executor interface is not evidence that the chosen runtime is safe. Test the actual runtime configuration against the threats you care about, and bind the evidence to the version and configuration you intend to deploy. The executor port design and sandbox admission checklist describe these concerns in more detail.
Treat cancellation and results as part of the contract
Cancellation races with normal completion. A client can ask to cancel a job just as the program finishes, so the API should report the state that actually won rather than promise that every cancellation request stops execution.
Likewise, completed should have a precise meaning. It might mean the runtime returned, but it does not necessarily mean the user program succeeded. Include the exit code and bounded stdout and stderr, and keep timeout, cancellation, infrastructure failure, and program failure distinguishable. A client can then make a useful decision without interpreting an opaque string.
Be precise about what is running today
An accepted request is not proof that code executed, and a configured sandbox is not proof that the sandbox blocks the threats you care about. Documentation, status fields, and product copy should distinguish a non-executing test mode from a runtime that has passed its admission checks.
Today the mPaka private beta uses mock_no_code_execution: it records the workflow but does not run submitted Python. You can inspect the API contract and build an integration, but do not treat the mock as an execution sandbox. Start with the quickstart and read the control-plane overview before deciding whether the current maturity fits your use case.
A practical design checklist
- Accept jobs quickly and return a stable ID.
- Require idempotency for retries of the same submission.
- Keep lifecycle state durable and define valid transitions.
- Run submitted code outside the API and host application process.
- Enforce resource limits at the runtime boundary, not only in request validation.
- Make cancellation races, timeouts, failures, and successful results observable.
- State clearly whether the current executor really runs code.
- Test and record evidence for the exact runtime configuration you plan to admit.
These choices let developers integrate one stable execution API while the runtime is evaluated independently. They do not remove the need to prove that the selected runtime enforces the isolation your workload requires.