🐍 | Python library for Runpod API and serverless worker SDK.
Actively maintained. Last release 10 days ago, commits in 20 of the last 26 weeks. A safe choice.
pip install runpodRunpod Python
Define GPU functions in Python. Run them in the cloud with one decorator.
Using an AI agent? Point it to https://docs.runpod.io/agent-setup.md to install the Runpod skills and MCP server.
Installation
pip install runpod # or: uv add runpod
rp login # authenticate once
Requires Python 3.10+. Installing the package also installs the rp CLI.
Example
import runpod
from runpod import App, Model, NetworkVolume, Secret
app = App("inference")
checkpoints = NetworkVolume("checkpoints", size=100)
llama = Model("meta-llama/Llama-3.1-8B-Instruct")
# an autoscaling job queue on cloud H100s: weights pre-cached,
# dependencies vendored at deploy time, scale-to-zero when idle
@app.queue(
gpu="H100",
workers=(0, 3),
dependencies=["vllm"],
model=llama,
env={"HF_TOKEN": Secret("hf-token")},
)
def chat(prompt: str):
import vllm
llm = vllm.LLM(model=str(llama.path)) # weights already on disk
result = llm.generate([prompt])[0]
return result.outputs[0].text # results must be JSON values
# one ephemeral pod per call: provisions, runs to completion, terminates
@app.task(gpu="H100", mounts={"/checkpoints": checkpoints})
def finetune(steps: int = 1000):
if steps < 1:
raise ValueError("steps must be a positive integer")
import torch
model = torch.nn.Linear(1, 1).cuda()
optimizer = torch.optim.SGD(model.parameters(), lr=0.05)
x = torch.randn(1024, 1, device="cuda")
y = 3 * x + 1
for _ in range(steps):
loss = ((model(x) - y) ** 2).mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.save(model.state_dict(), checkpoints.path / "model.pt")
return {"loss": loss.item()}
@runpod.local_entrypoint
def main():
print(chat.remote("why is the sky blue?")) # blocks for the result
print(finetune.remote(steps=500)) # pod terminates on return
