Modal Client SDKs

repository·main·Indexed 19 days ago

https://github.com/modal-labs/modal-client

Official SDKs for Modal, a serverless platform, providing tools for Python (3.10–3.14), JavaScript/TypeScript (Node 22+), and Go. These libraries enable developers to deploy high-performance applications, invoke Modal Functions, manage Sandboxes, and interact with platform resources. Includes the modal CLI and specialized tools for image builder configuration and telemetry integration via gRPC interceptors or middleware.

Tokens
64.8K
Snippets
270
Records
347
Agent score
67%

What's inside modal-client

  1. Add telemetry and observability to the Modal Go SDK

    main
    The Modal Go SDK supports custom gRPC interceptors for telemetry, tracing, and observability. You can implement custom unary and stream interceptors to measure API call latency, trace requests, and integrate with external observability tools like OpenTelemetry or DataDog.
  2. Configure class parametrization and lifecycle hooks

    main

    When using Cls (serverless classes), you can manage how they are initialized and how they behave during their lifecycle:

    • parameter: Define class parameters similarly to a Python dataclass field.
    • enter: A decorator for a method that executes during container startup.
    • exit: A decorator for a method that executes during container shutdown.
    • method: A decorator used to expose a specific class method as an invokable function.
  3. Configure function semantics: Batching, Concurrency, and Retries

    main

    Enhance function execution behavior with these decorators and configurations:

    • batched: Enables dynamic input batching to optimize throughput.
    • concurrent: Enables input concurrency to process multiple inputs simultaneously.
    • Retries: A policy used to define how functions should retry upon input failures.
  4. Construct an application with the Modal Python SDK

    main

    To deploy code to Modal, you must first define an App. The App serves as the main unit of deployment. You register your code components (functions, classes, or servers) to an App using specific decorators:

    • App.function: Decorator to register a standalone function.
    • App.cls: Decorator to register a class.
    • App.server: Decorator to register a server.
  5. Core Modal JavaScript SDK Objects

    main

    The SDK provides access to several core Modal abstractions through the ModalClient:

    • App: The main unit of deployment for code on Modal.
    • Function: A serverless function backed by an autoscaling container pool.
    • Image: An immutable representation of a container filesystem.
    • Sandbox: A process-like interface for restricted execution.
    • Secret: A secure reference to environment variables.
    • Volume: A mutable distributed storage device.
  6. Integrate web applications with Modal

    main

    You can expose your functions as web endpoints using several decorators depending on your web framework or protocol:

    • fastapi_endpoint: Exposes a simple FastAPI-based endpoint.
    • asgi_app: For functions that construct an ASGI web application.
    • wsgi_app: For functions that construct a WSGI web application.
    • web_server: For functions that construct a standard HTTP web server.
  7. Manage persistent and in-memory data storage

    main

    Modal provides several primitives for data handling:

    Persistent Storage

    • Volume: Distributed storage optimized for high-performance parallel reads/writes.
    • CloudBucketMount: Storage backed by third-party cloud buckets (e.g., S3).

    In-memory Storage

    • Dict: A distributed key-value store.
    • Queue: A distributed FIFO (First-In-First-Out) queue.
  8. How to use the Modal JavaScript SDK

    main

    To interact with Modal, first instantiate a ModalClient. You then obtain instances of Modal objects (like App, Image, or Sandbox) through service methods on that client. Most methods accept an options object (often with an associated Params type) for configuration.

    import { ModalClient } from "modal";
    
    const modal = new ModalClient();
  9. How Modal Sandboxes work in Go

    main

    A Sandbox provides a process-like interface for restricted execution. You obtain a Sandbox by calling mc.Sandboxes.Create using an App and an Image.

    Key features:

    • Execution: Use sb.Exec to run commands. It returns a process object containing Stdout and Stderr.
    • Configuration: You can configure resources like CPU, MemoryMiB, Timeout, and Secrets using SandboxCreateParams.
    • Lifecycle: Always call sb.Terminate(ctx, nil) to end the sandbox session.
    // Basic Sandbox usage
    app, err := mc.Apps.FromName(ctx, "sandbox-app", &modal.AppFromNameParams{CreateIfMissing: true})
    image := mc.Images.FromRegistry("alpine:3.21", nil)
    
    sb, err := mc.Sandboxes.Create(ctx, app, image, nil)
    if err != nil {
    	log.Fatal(err)
    }
    defer sb.Terminate(ctx, nil)
    
    p, err := sb.Exec(ctx, []string{"echo", "Hello!"}, nil)
    // ... read output