How to deploy a function with Modal
Quick answer
Use the
modal Python package to deploy functions serverlessly by defining them with the @app.function decorator and deploying via modal.runner.deploy_stub. Specify GPU or custom images in the decorator for AI workloads.PREREQUISITES
Python 3.8+pip install modalModal account and CLI login (modal login)Optional: Docker installed for custom images
Setup
Install the modal package and log in to Modal CLI to authenticate your environment.
pip install modal
modal login output
Collecting modal Downloading modal-1.0.0-py3-none-any.whl (50 kB) Installing collected packages: modal Successfully installed modal-1.0.0 # After installation # Run 'modal login' and follow the browser prompt to authenticate
Step by step
Define a Python function with @app.function decorator, specifying GPU and dependencies if needed. Deploy and invoke the function locally.
import os
import modal
app = modal.App("my-modal-app")
@app.function(gpu="A10G", image=modal.Image.debian_slim().pip_install("torch"))
def run_inference(prompt: str) -> str:
import torch
# Dummy inference logic
return f"Received prompt: {prompt}"
if __name__ == "__main__":
with modal.runner.deploy_stub(app):
result = run_inference.remote("Hello from Modal!")
print(result) output
Received prompt: Hello from Modal!
Common variations
- Use
gpu=Nonefor CPU-only functions. - Customize Docker images with
modal.Imagefor specific dependencies. - Deploy async functions by defining
async defand callingawait func.remote(...)inside an async context.
import modal
app = modal.App("my-app")
@app.function()
def cpu_task(name: str) -> str:
return f"Hello, {name}!"
# Async example
@app.function()
async def async_task():
import asyncio
await asyncio.sleep(1)
return "Async done!" Troubleshooting
- If
modal.runner.deploy_stubhangs, ensure you are logged in withmodal login. - For GPU errors, verify your Modal plan supports GPU usage.
- Use
modal logsto inspect runtime errors.
Key Takeaways
- Use
@app.functionto define serverless functions in Modal. - Specify GPU and dependencies in the function decorator for AI workloads.
- Deploy functions locally with
modal.runner.deploy_stubfor testing. - Authenticate with
modal loginbefore deploying. - Use
modal logsto debug runtime issues.