How to beginner · 3 min read

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 modal
  • Modal 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.

bash
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.

python
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=None for CPU-only functions.
  • Customize Docker images with modal.Image for specific dependencies.
  • Deploy async functions by defining async def and calling await func.remote(...) inside an async context.
python
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_stub hangs, ensure you are logged in with modal login.
  • For GPU errors, verify your Modal plan supports GPU usage.
  • Use modal logs to inspect runtime errors.

Key Takeaways

  • Use @app.function to define serverless functions in Modal.
  • Specify GPU and dependencies in the function decorator for AI workloads.
  • Deploy functions locally with modal.runner.deploy_stub for testing.
  • Authenticate with modal login before deploying.
  • Use modal logs to debug runtime issues.
Verified 2026-04
Verify ↗