Modal classes allow you to maintain state and define multiple methods that share resources.
Referencing classes
FromName
References a class from a deployed App by its name.
cls, err := mc.Cls.FromName(ctx, "my-app", "MyClass", nil)
if err != nil {
// Handle error
}
Context for the operation
The name of the App containing the class
Optional parametersEnvironment name. Defaults to client’s environment.
Returns:
*Cls - The class definition
error - NotFoundError if the class doesn’t exist
Class methods
Instance
Creates an instance of the class with the provided parameters.
instance, err := cls.Instance(ctx, map[string]any{
"param1": "value1",
"param2": 42,
})
if err != nil {
// Handle error
}
Context for the operation
Parameters to pass to the class constructor. Use nil for classes without parameters.
Returns:
*ClsInstance - An instance of the class with bound parameters
error - Error if instantiation fails or parameter validation fails
WithOptions
Returns a new Cls with runtime options overridden.
cpu := 2.0
memory := 2048
modifiedCls := cls.WithOptions(&modal.ClsWithOptionsParams{
CPU: &cpu,
MemoryMiB: &memory,
GPU: ptr("T4"),
})
Runtime configuration optionsCPU request in fractional cores
Hard CPU limit in fractional cores
GPU configuration (e.g., “T4”, “A100:2”)
Maximum number of containers
Number of buffer containers
Time before scaling down idle containers
Returns:
*Cls - A new Cls with options applied
WithConcurrency
Returns a new Cls with concurrency settings configured.
target := 5
modifiedCls := cls.WithConcurrency(&modal.ClsWithConcurrencyParams{
MaxInputs: 10,
TargetInputs: &target,
})
params
*ClsWithConcurrencyParams
Concurrency configurationMaximum number of concurrent inputs per container
Target number of concurrent inputs for autoscaling
Returns:
*Cls - A new Cls with concurrency configured
WithBatching
Returns a new Cls with dynamic batching configured.
modifiedCls := cls.WithBatching(&modal.ClsWithBatchingParams{
MaxBatchSize: 100,
Wait: 500 * time.Millisecond,
})
Batching configurationMaximum time to wait for batch to fill
Returns:
*Cls - A new Cls with batching configured
ClsInstance methods
Method
Retrieves a method from the class instance.
method, err := instance.Method("process")
if err != nil {
// Handle error
}
// Call the method
result, err := method.Remote(ctx, []any{"input"}, nil)
Returns:
*Function - The method as a Function
error - NotFoundError if the method doesn’t exist
Example usage
package main
import (
"context"
"fmt"
"github.com/modal-labs/modal-client/go"
)
func main() {
ctx := context.Background()
mc, _ := modal.NewClient()
defer mc.Close()
// Reference a class
cls, err := mc.Cls.FromName(ctx, "my-app", "Model", nil)
if err != nil {
panic(err)
}
// Create an instance with parameters
instance, err := cls.Instance(ctx, map[string]any{
"model_name": "gpt-4",
"temperature": 0.7,
})
if err != nil {
panic(err)
}
// Call a method
generateMethod, err := instance.Method("generate")
if err != nil {
panic(err)
}
result, err := generateMethod.Remote(ctx, []any{"Hello, world!"}, nil)
if err != nil {
panic(err)
}
fmt.Printf("Generated: %v\n", result)
// Use WithOptions to override resources
cpu := 4.0
memory := 8192
highResourceCls := cls.WithOptions(&modal.ClsWithOptionsParams{
CPU: &cpu,
MemoryMiB: &memory,
})
instance2, _ := highResourceCls.Instance(ctx, nil)
method2, _ := instance2.Method("process")
result2, _ := method2.Remote(ctx, []any{"data"}, nil)
fmt.Printf("Result: %v\n", result2)
}