RunPod Python SDK
Official Python SDK and serverless worker framework for the RunPod cloud GPU platform. Provides tools for creating serverless endpoint workers, managing pods, and interacting with the RunPod API. Used both as a client library and as the runtime framework inside serverless containers.
Warnings
- gotcha The handler function must return a dict or a generator. Returning a plain string will cause the worker to fail silently or produce malformed output.
- gotcha event['input'] is the actual user payload. The top-level event dict contains metadata (id, etc.). Accessing event['prompt'] directly instead of event['input']['prompt'] is a common mistake.
- breaking Generator (streaming) handlers must yield dicts, not strings. The output format changed in 1.x to require structured yields.
- gotcha runpod.serverless.start() blocks forever waiting for jobs. It must be the last call in your script. Code after it will never execute.
- gotcha Setting runpod.api_key is required before any API management calls (get_pods, create_pod, etc.). Without it, calls raise an authentication error.
- gotcha The refresh_worker option in the handler config causes the worker container to restart after every job. This is useful for debugging but causes cold starts in production.
Install
-
pip install runpod
Imports
- runpod
import runpod
- runpod.serverless.start
import runpod runpod.serverless.start({"handler": my_handler})
Quickstart
import runpod
import os
# --- Serverless Handler Example ---
def handler(event):
"""Serverless handler function."""
prompt = event['input'].get('prompt', 'Hello!')
return {"output": f"Processed: {prompt}"}
runpod.serverless.start({"handler": handler})
# --- API Client Example (run separately) ---
# runpod.api_key = os.environ.get('RUNPOD_API_KEY', '')
# pods = runpod.get_pods()
# print(pods)