Try Live
Add Docs
Rankings
Pricing
Docs
Install
Theme
Install
Docs
Pricing
More...
More...
Try Live
Rankings
Enterprise
Create API Key
Add Docs
Modal Client
https://github.com/modal-labs/modal-client
Admin
The Modal Python library provides convenient, on-demand access to serverless cloud compute from
...
Tokens:
15,455
Snippets:
138
Trust Score:
9.8
Update:
1 month ago
Context
Skills
Chat
Benchmark
75.7
Suggestions
Latest
Show doc for...
Code
Info
Show Results
Context Summary (auto-generated)
Raw
Copy
Link
# Modal SDK Modal provides client libraries for Python, JavaScript/TypeScript, and Go that enable on-demand access to serverless cloud compute. The SDKs allow you to run arbitrary code in secure sandboxes, call deployed Modal Functions, and interact with Modal resources like Volumes, Secrets, and Queues. Modal handles infrastructure provisioning, scaling, and resource management automatically. The primary use cases include running code execution environments (Sandboxes) for AI agents, deploying and calling serverless functions with GPU support, building custom container images, and managing persistent storage through Volumes. The Python SDK (v1.3.x) is the most feature-complete, supporting function definition and deployment, while the JavaScript (v0.7.x) and Go (v0.7.x) SDKs focus on client-side interactions with deployed Modal resources. ## Python SDK ### Installation and Setup Install and configure the Modal Python SDK for serverless compute access. ```bash # Install Modal pip install modal # Or with uv uv pip install modal # Authenticate with Modal (interactive setup) python3 -m modal setup # Or set environment variables for non-interactive auth export MODAL_TOKEN_ID=ak-your-token-id export MODAL_TOKEN_SECRET=as-your-token-secret ``` ### Creating a Modal App Define a Modal App as a deployment unit for functions and classes. ```python import modal app = modal.App(name="my-app") @app.function( image=modal.Image.debian_slim().pip_install("pandas"), secrets=[modal.Secret.from_name("my-secret")], gpu="T4", timeout=300, ) def process_data(data: list) -> dict: import pandas as pd df = pd.DataFrame(data) return {"mean": df.mean().to_dict(), "count": len(df)} # Deploy the app # modal deploy my_app.py # Run locally for testing # modal run my_app.py ``` ### Defining Functions with Decorators Create serverless functions with custom compute resources and scheduling. ```python import modal app = modal.App() # Basic function with GPU @app.function(gpu="A100", memory=32768) def gpu_inference(prompt: str) -> str: # Your ML inference code here return f"Processed: {prompt}" # Function with concurrent execution limits @app.function() @modal.concurrent(max_inputs=10) def batch_process(items: list) -> list: return [item.upper() for item in items] # Scheduled function (cron) @app.function(schedule=modal.Cron("0 * * * *")) # Every hour def hourly_task(): print("Running hourly task") # Periodic function @app.function(schedule=modal.Period(minutes=30)) def periodic_task(): print("Running every 30 minutes") ``` ### Building Custom Images Create container images with dependencies and system packages. ```python import modal # Start from Debian slim and add Python packages image = modal.Image.debian_slim(python_version="3.12").pip_install( "torch", "transformers", "pandas>=2.0", ) # From Docker registry with additional setup image = modal.Image.from_registry( "nvidia/cuda:12.1.0-base-ubuntu22.04", add_python="3.11" ).apt_install( "git", "curl" ).pip_install( "numpy", "scipy" ).run_commands( "curl -fsSL https://example.com/setup.sh | bash" ) # Using Dockerfile commands with secrets image = modal.Image.from_registry("alpine:3.21").dockerfile_commands([ "RUN apk add --no-cache curl=$CURL_VERSION", "ENV API_URL=https://api.example.com" ], secrets=[modal.Secret.from_dict({"CURL_VERSION": "8.12.1-r1"})]) app = modal.App() @app.function(image=image) def run_with_custom_image(): import torch return torch.cuda.is_available() ``` ### Creating and Using Sandboxes Run arbitrary code in isolated sandbox environments. ```python import modal app = modal.App.lookup("my-app", create_if_missing=True) # Create a basic sandbox sb = modal.Sandbox.create( app=app, image=modal.Image.debian_slim(), timeout=600, cpu=2.0, memory=4096, ) # Execute commands in the sandbox process = sb.exec(["python", "-c", "print('Hello from sandbox!')"]) print(process.stdout.read()) exit_code = process.wait() # Write to stdin and read output sb2 = modal.Sandbox.create(app=app, image=modal.Image.debian_slim()) sb2.stdin.write(b"Hello\n") sb2.stdin.close() output = sb2.stdout.read() sb2.terminate() # Sandbox with GPU and volumes volume = modal.Volume.from_name("my-volume", create_if_missing=True) sb = modal.Sandbox.create( app=app, image=modal.Image.from_registry("pytorch/pytorch:2.0.0-cuda11.7-cudnn8-runtime"), gpu="A10G", volumes={"/data": volume}, secrets=[modal.Secret.from_name("api-keys")], ) ``` ### Working with Volumes Use persistent storage across function invocations and sandboxes. ```python import modal # Create or get a volume volume = modal.Volume.from_name("my-data-volume", create_if_missing=True) app = modal.App() @app.function(volumes={"/data": volume}) def write_to_volume(filename: str, content: str): with open(f"/data/{filename}", "w") as f: f.write(content) # Commit changes to persist them volume.commit() @app.function(volumes={"/data": volume}) def read_from_volume(filename: str) -> str: volume.reload() # Get latest data with open(f"/data/{filename}", "r") as f: return f.read() # List files in volume for entry in volume.listdir("/"): print(f"{entry.path}: {entry.size} bytes") # Upload local files to volume with volume.batch_upload() as batch: batch.put_file("local_file.txt", "/remote/path/file.txt") batch.put_directory("./local_dir", "/remote/dir") ``` ### Using Secrets Securely pass credentials and API keys to functions. ```python import modal # Reference a secret stored in Modal dashboard secret = modal.Secret.from_name("my-api-secret") # Create a secret from environment variables secret = modal.Secret.from_dotenv() # Create an inline secret (for development only) secret = modal.Secret.from_dict({ "API_KEY": "your-api-key", "DATABASE_URL": "postgresql://..." }) app = modal.App() @app.function(secrets=[secret]) def use_secret(): import os api_key = os.environ["API_KEY"] return f"Using API key: {api_key[:4]}..." # Multiple secrets @app.function(secrets=[ modal.Secret.from_name("aws-credentials"), modal.Secret.from_name("openai-key"), ]) def multi_secret_function(): import os return { "aws": os.environ.get("AWS_ACCESS_KEY_ID"), "openai": os.environ.get("OPENAI_API_KEY")[:8] + "...", } ``` ### Calling Deployed Functions Invoke deployed Modal functions remotely. ```python import modal # Look up a deployed function function = modal.Function.from_name("my-app", "process_data") # Synchronous call result = function.remote([1, 2, 3, 4, 5]) print(result) # Spawn for async execution function_call = function.spawn([1, 2, 3]) # Do other work... result = function_call.get() # Get result when ready # Map over multiple inputs (parallel execution) inputs = [[1, 2], [3, 4], [5, 6]] for result in function.map(inputs): print(result) # Starmap for multiple arguments for result in function.starmap([(1, "a"), (2, "b"), (3, "c")]): print(result) ``` ### Defining Modal Classes Create stateful services with lifecycle methods. ```python import modal app = modal.App() @app.cls(gpu="A100", container_idle_timeout=300) class ModelService: @modal.enter() def load_model(self): # Runs once when container starts from transformers import AutoModelForCausalLM self.model = AutoModelForCausalLM.from_pretrained("gpt2") self.model.eval() @modal.method() def generate(self, prompt: str, max_length: int = 100) -> str: # Process requests inputs = self.tokenizer(prompt, return_tensors="pt") outputs = self.model.generate(**inputs, max_length=max_length) return self.tokenizer.decode(outputs[0]) @modal.exit() def cleanup(self): # Runs when container is shutting down del self.model # Call the class method service = modal.Cls.from_name("my-app", "ModelService") result = service().generate.remote("Hello, world") ``` ### Web Endpoints Expose functions as HTTP endpoints. ```python import modal app = modal.App() @app.function() @modal.web_endpoint(method="POST") def api_handler(request: dict) -> dict: return {"status": "success", "data": request} # FastAPI integration from fastapi import FastAPI, Request web_app = FastAPI() @web_app.get("/health") async def health(): return {"status": "healthy"} @web_app.post("/process") async def process(request: Request): data = await request.json() return {"processed": data} @app.function() @modal.asgi_app() def fastapi_app(): return web_app ``` ### Using Queues Communicate between functions using queues. ```python import modal queue = modal.Queue.from_name("my-queue", create_if_missing=True) app = modal.App() @app.function() def producer(): for i in range(10): queue.put({"item": i, "timestamp": time.time()}) queue.put(None) # Signal end @app.function() def consumer(): results = [] while True: item = queue.get() if item is None: break results.append(item) return results # Multiple partitions for parallel processing @app.function() def partitioned_producer(): for i in range(100): partition = i % 4 queue.put({"value": i}, partition=partition) ``` ## JavaScript/TypeScript SDK ### Installation and Setup Install the Modal JavaScript SDK for Node.js, Deno, or Bun projects. ```bash npm install modal # Configure authentication via environment variables export MODAL_TOKEN_ID=ak-your-token-id export MODAL_TOKEN_SECRET=as-your-token-secret # Or use ~/.modal.toml config file (same as Python SDK) ``` ### Creating a Modal Client Initialize the client for interacting with Modal resources. ```typescript import { ModalClient } from "modal"; // Default client (uses environment variables or config file) const modal = new ModalClient(); // Custom client with explicit credentials const customModal = new ModalClient({ tokenId: "ak-your-token-id", tokenSecret: "as-your-token-secret", }); ``` ### Calling Deployed Functions Invoke Modal functions deployed via Python. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); // Get a deployed function const echo = await modal.functions.fromName("my-app", "echo_string"); // Call with positional args const result = await echo.remote(["Hello world!"]); console.log(result); // Call with keyword arguments const resultWithKwargs = await echo.remote([], { message: "Hello", count: 5 }); console.log(resultWithKwargs); // Spawn for async execution const functionCall = await echo.spawn(["async message"]); // Do other work... const asyncResult = await functionCall.get(); console.log(asyncResult); ``` ### Creating Sandboxes Run code in isolated sandbox environments. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); const app = await modal.apps.fromName("my-app", { createIfMissing: true }); const image = modal.images.fromRegistry("python:3.13-slim"); // Create sandbox with a command const sb = await modal.sandboxes.create(app, image, { command: ["python", "-c", "print('Hello from sandbox!')"], }); console.log("Sandbox ID:", sb.sandboxId); // Read output const output = await sb.stdout.readText(); console.log("Output:", output); // Wait for completion and get exit code const exitCode = await sb.wait(); console.log("Exit code:", exitCode); // Always terminate when done await sb.terminate(); ``` ### Executing Commands in Sandboxes Run commands inside an existing sandbox. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); const app = await modal.apps.fromName("my-app", { createIfMissing: true }); const image = modal.images.fromRegistry("python:3.13-slim"); const sb = await modal.sandboxes.create(app, image); console.log("Started Sandbox:", sb.sandboxId); try { // Execute a command const process = await sb.exec( ["python", "-c", "import sys; print('stdout'); print('stderr', file=sys.stderr)"], { stdout: "pipe", stderr: "pipe" } ); // Read stdout and stderr concurrently const [stdout, stderr] = await Promise.all([ process.stdout.readText(), process.stderr.readText(), ]); console.log("stdout:", stdout); console.log("stderr:", stderr); console.log("Exit code:", await process.wait()); // Execute with secrets const secret = await modal.secrets.fromName("api-secret", { requiredKeys: ["API_KEY"], }); const withSecret = await sb.exec(["printenv", "API_KEY"], { stdout: "pipe", secrets: [secret], }); console.log("API_KEY:", await withSecret.stdout.readText()); } finally { await sb.terminate(); } ``` ### Building Custom Images Create images with dependencies for sandboxes. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); const app = await modal.apps.fromName("my-app", { createIfMissing: true }); // Build image with Dockerfile commands const secret = await modal.secrets.fromObject({ CURL_VERSION: "8.12.1-r1" }); const image = modal.images .fromRegistry("alpine:3.21") .dockerfileCommands(["RUN apk add --no-cache curl=$CURL_VERSION"], { secrets: [secret], }) .dockerfileCommands(["ENV API_URL=https://api.example.com"]); // Use the image in a sandbox const sb = await modal.sandboxes.create(app, image, { command: ["curl", "-s", "$API_URL/health"], }); console.log("Output:", await sb.stdout.readText()); await sb.terminate(); ``` ### Working with Volumes Mount persistent storage in sandboxes. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); const app = await modal.apps.fromName("my-app", { createIfMissing: true }); const image = modal.images.fromRegistry("alpine:3.21"); // Get or create a volume const volume = await modal.volumes.fromName("my-volume", { createIfMissing: true, }); // Write to volume const writer = await modal.sandboxes.create(app, image, { command: ["sh", "-c", "echo 'Hello from sandbox!' > /data/message.txt"], volumes: { "/data": volume }, }); await writer.wait(); console.log("Writer finished"); // Read from volume (read-only mount) const reader = await modal.sandboxes.create(app, image, { volumes: { "/data": volume.readOnly() }, }); const readProcess = await reader.exec(["cat", "/data/message.txt"]); console.log("Message:", await readProcess.stdout.readText()); await writer.terminate(); await reader.terminate(); ``` ### Calling Modal Classes Invoke methods on deployed Modal classes. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); // Get a deployed class const cls = await modal.cls.fromName("my-app", "ModelService"); // Create an instance const instance = await cls.instance(); // Get a method and call it const method = instance.method("generate"); // Call with positional args const result = await method.remote(["Hello, generate something"]); console.log(result); // Call with keyword arguments const resultWithKwargs = await method.remote([], { prompt: "Write a poem", maxLength: 200, }); console.log(resultWithKwargs); ``` ### Running Agents in Sandboxes Execute AI agents in sandbox environments. ```typescript import { ModalClient } from "modal"; const modal = new ModalClient(); const app = await modal.apps.fromName("agent-app", { createIfMissing: true }); // Build image with agent dependencies const image = modal.images .fromRegistry("alpine:3.21") .dockerfileCommands([ "RUN apk add --no-cache bash curl git", "RUN curl -fsSL https://example.com/agent-install.sh | bash", "ENV PATH=/root/.local/bin:$PATH", ]); const sb = await modal.sandboxes.create(app, image); try { // Clone a repository const git = await sb.exec(["git", "clone", "https://github.com/user/repo", "/repo"]); await git.wait(); // Run agent with PTY (required for interactive agents) const agent = await sb.exec( ["agent", "-p", "Analyze this codebase"], { pty: true, secrets: [ await modal.secrets.fromName("agent-api-key", { requiredKeys: ["API_KEY"], }), ], workdir: "/repo", } ); await agent.wait(); console.log("Agent output:", await agent.stdout.readText()); } finally { await sb.terminate(); } ``` ## Go SDK ### Installation and Setup Install the Modal Go SDK for golang projects. ```bash go get -u github.com/modal-labs/modal-client/go # Configure authentication via environment variables export MODAL_TOKEN_ID=ak-your-token-id export MODAL_TOKEN_SECRET=as-your-token-secret ``` ### Creating a Modal Client Initialize the Go client for Modal interactions. ```go package main import ( "context" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { // Default client (uses environment variables or config file) mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } // Custom client with explicit options mc, err = modal.NewClientWithOptions(&modal.ClientParams{ TokenID: "ak-your-token-id", TokenSecret: "as-your-token-secret", }) if err != nil { log.Fatalf("Failed to create client: %v", err) } } ``` ### Calling Deployed Functions Invoke Modal functions from Go. ```go package main import ( "context" "fmt" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } // Get a deployed function echo, err := mc.Functions.FromName(ctx, "my-app", "echo_string", nil) if err != nil { log.Fatalf("Failed to get Function: %v", err) } // Call with positional args result, err := echo.Remote(ctx, []any{"Hello world!"}, nil) if err != nil { log.Fatalf("Failed to call Function: %v", err) } fmt.Println("Result:", result) // Call with keyword arguments result, err = echo.Remote(ctx, nil, map[string]any{"message": "Hello", "count": 5}) if err != nil { log.Fatalf("Failed to call Function: %v", err) } fmt.Println("Result with kwargs:", result) } ``` ### Spawning Functions Asynchronously Execute functions asynchronously and retrieve results later. ```go package main import ( "context" "fmt" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } echo, err := mc.Functions.FromName(ctx, "my-app", "process_data", nil) if err != nil { log.Fatalf("Failed to get Function: %v", err) } // Spawn function call (non-blocking) fc, err := echo.Spawn(ctx, []any{[]int{1, 2, 3, 4, 5}}, nil) if err != nil { log.Fatalf("Failed to spawn Function: %v", err) } fmt.Println("Function call spawned:", fc.FunctionCallID) // Do other work here... // Get the result when ready result, err := fc.Get(ctx, nil) if err != nil { log.Fatalf("Failed to get Function result: %v", err) } fmt.Println("Result:", result) } ``` ### Creating Sandboxes Run code in isolated sandbox environments. ```go package main import ( "context" "fmt" "io" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } app, err := mc.Apps.FromName(ctx, "my-app", &modal.AppFromNameParams{CreateIfMissing: true}) if err != nil { log.Fatalf("Failed to get App: %v", err) } image := mc.Images.FromRegistry("alpine:3.21", nil) // Create sandbox with a command sb, err := mc.Sandboxes.Create(ctx, app, image, &modal.SandboxCreateParams{ Command: []string{"cat"}, }) if err != nil { log.Fatalf("Failed to create Sandbox: %v", err) } fmt.Println("Sandbox ID:", sb.SandboxID) defer sb.Terminate(context.Background(), nil) // Write to stdin _, err = sb.Stdin.Write([]byte("Hello from Go!")) if err != nil { log.Fatalf("Failed to write to stdin: %v", err) } sb.Stdin.Close() // Read from stdout output, err := io.ReadAll(sb.Stdout) if err != nil { log.Fatalf("Failed to read stdout: %v", err) } fmt.Println("Output:", string(output)) } ``` ### Executing Commands in Sandboxes Run commands inside an existing sandbox. ```go package main import ( "context" "fmt" "io" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } app, err := mc.Apps.FromName(ctx, "my-app", &modal.AppFromNameParams{CreateIfMissing: true}) if err != nil { log.Fatalf("Failed to get App: %v", err) } image := mc.Images.FromRegistry("python:3.13-slim", nil) sb, err := mc.Sandboxes.Create(ctx, app, image, nil) if err != nil { log.Fatalf("Failed to create Sandbox: %v", err) } defer sb.Terminate(context.Background(), nil) // Execute a Python command process, err := sb.Exec(ctx, []string{ "python", "-c", "print('Hello from Python!')", }, nil) if err != nil { log.Fatalf("Failed to exec: %v", err) } stdout, _ := io.ReadAll(process.Stdout) exitCode, _ := process.Wait(ctx) fmt.Printf("Output: %s\nExit code: %d\n", stdout, exitCode) // Execute with secrets secret, err := mc.Secrets.FromName(ctx, "api-secret", &modal.SecretFromNameParams{ RequiredKeys: []string{"API_KEY"}, }) if err != nil { log.Fatalf("Failed to get secret: %v", err) } withSecret, err := sb.Exec(ctx, []string{"printenv", "API_KEY"}, &modal.SandboxExecParams{ Secrets: []*modal.Secret{secret}, }) if err != nil { log.Fatalf("Failed to exec with secret: %v", err) } secretOutput, _ := io.ReadAll(withSecret.Stdout) fmt.Println("API_KEY:", string(secretOutput)) } ``` ### Building Custom Images Create images with dependencies. ```go package main import ( "context" "fmt" "io" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } app, err := mc.Apps.FromName(ctx, "my-app", &modal.AppFromNameParams{CreateIfMissing: true}) if err != nil { log.Fatalf("Failed to get App: %v", err) } // Create a secret for build-time variables secret, err := mc.Secrets.FromMap(ctx, map[string]string{ "CURL_VERSION": "8.12.1-r1", }, nil) if err != nil { log.Fatalf("Failed to create secret: %v", err) } // Build image with Dockerfile commands image := mc.Images.FromRegistry("alpine:3.21", nil). DockerfileCommands([]string{"RUN apk add --no-cache curl=$CURL_VERSION"}, &modal.ImageDockerfileCommandsParams{ Secrets: []*modal.Secret{secret}, }). DockerfileCommands([]string{"ENV API_URL=https://api.example.com"}, nil) sb, err := mc.Sandboxes.Create(ctx, app, image, &modal.SandboxCreateParams{ Command: []string{"curl", "-s", "https://httpbin.org/get"}, }) if err != nil { log.Fatalf("Failed to create sandbox: %v", err) } defer sb.Terminate(context.Background(), nil) output, _ := io.ReadAll(sb.Stdout) fmt.Println("Output:", string(output)) } ``` ### Working with Volumes Mount persistent storage in sandboxes. ```go package main import ( "context" "fmt" "io" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } app, err := mc.Apps.FromName(ctx, "my-app", &modal.AppFromNameParams{CreateIfMissing: true}) if err != nil { log.Fatalf("Failed to get App: %v", err) } image := mc.Images.FromRegistry("alpine:3.21", nil) // Get or create a volume volume, err := mc.Volumes.FromName(ctx, "my-volume", &modal.VolumeFromNameParams{ CreateIfMissing: true, }) if err != nil { log.Fatalf("Failed to get volume: %v", err) } // Writer sandbox writer, err := mc.Sandboxes.Create(ctx, app, image, &modal.SandboxCreateParams{ Command: []string{"sh", "-c", "echo 'Hello from Go!' > /data/message.txt"}, Volumes: map[string]*modal.Volume{"/data": volume}, }) if err != nil { log.Fatalf("Failed to create writer: %v", err) } defer writer.Terminate(context.Background(), nil) writer.Wait(ctx) fmt.Println("Writer finished") // Reader sandbox with read-only volume reader, err := mc.Sandboxes.Create(ctx, app, image, &modal.SandboxCreateParams{ Volumes: map[string]*modal.Volume{"/data": volume.ReadOnly()}, }) if err != nil { log.Fatalf("Failed to create reader: %v", err) } defer reader.Terminate(context.Background(), nil) process, _ := reader.Exec(ctx, []string{"cat", "/data/message.txt"}, nil) output, _ := io.ReadAll(process.Stdout) fmt.Println("Message:", string(output)) } ``` ### Calling Modal Classes Invoke methods on deployed Modal classes. ```go package main import ( "context" "fmt" "log" modal "github.com/modal-labs/modal-client/go" ) func main() { ctx := context.Background() mc, err := modal.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } // Get a deployed class cls, err := mc.Cls.FromName(ctx, "my-app", "ModelService", nil) if err != nil { log.Fatalf("Failed to get Cls: %v", err) } // Create an instance instance, err := cls.Instance(ctx, nil) if err != nil { log.Fatalf("Failed to create instance: %v", err) } // Get a method method, err := instance.Method("generate") if err != nil { log.Fatalf("Failed to get method: %v", err) } // Call with positional args result, err := method.Remote(ctx, []any{"Hello, generate something"}, nil) if err != nil { log.Fatalf("Failed to call method: %v", err) } fmt.Println("Result:", result) // Call with keyword arguments result, err = method.Remote(ctx, nil, map[string]any{ "prompt": "Write a poem", "maxLength": 200, }) if err != nil { log.Fatalf("Failed to call method: %v", err) } fmt.Println("Result with kwargs:", result) } ``` ## Summary The Modal SDKs enable developers to build serverless applications that leverage cloud compute resources without managing infrastructure. The Python SDK is the primary interface for defining and deploying Modal Functions and Classes, with full support for GPU compute, custom container images, persistent volumes, scheduled tasks, and web endpoints. Functions defined in Python can then be invoked from any of the three SDKs, enabling polyglot architectures where backend ML services in Python are called from JavaScript or Go applications. Sandboxes provide the foundation for running arbitrary code in isolated environments, making them ideal for AI agent execution, code interpretation, and dynamic workload processing. All three SDKs share a consistent API design centered around a client object that provides access to Apps, Functions, Sandboxes, Images, Volumes, Secrets, and other Modal resources. The typical integration pattern involves defining compute-intensive functions in Python, deploying them to Modal, and then invoking them from application code in any language. For stateful services, Modal Classes allow container reuse with lifecycle hooks for model loading and cleanup, reducing cold start overhead for inference workloads.