modal.Cls
class Cls(modal.object.Object)Cls adds method pooling and lifecycle hook behavior to modal.Function.
Generally, you will not construct a Cls directly.
Instead, use the @app.cls() decorator on the App object.
hydrate
hydrate(self, client=None)Synchronize the local object with its identity on the Modal server.
It is rarely necessary to call this method explicitly, as most operations will lazily hydrate when needed. The main use case is when you need to access object metadata, such as its ID.
Added in v0.72.39: This method replaces the deprecated .resolve() method.
from_name
from_name(cls, app_name, name, *, version=None, environment_name=None,
client=None)Reference a Cls from a deployed App by its name.
This is a lazy method that defers hydrating the local object with metadata from Modal servers until the first time it is actually used.
Parameters
Returns
A Cls reference that hydrates on first use.
Usage
Model = modal.Cls.from_name("other-app", "Model")The version parameter constructs a version-pinned Cls:
Modelv3 = modal.Cls.from_name("other-app", "Model", version=3)with_options
with_options(self, *, cpu=None, memory=None, gpu=None, env=None, secrets=None,
volumes={}, retries=None, max_containers=None, buffer_containers=None,
scaledown_window=None, timeout=None, region=None, cloud=None)Override the static Cls configuration with invocation-specific values.
This method will return a new variant of the Cls that will autoscale independently of the base configuration.
Note that options cannot be “unset” with this method (i.e., if a GPU is configured in the @app.cls() decorator, passing gpu=None here will not create a CPU-only instance).
Container arguments (volumes and secrets) from later calls replace earlier values; they are not merged.
Parameters
@app.function / @app.cls resource options). A100. Volume or CloudBucketMount). (Default is {})aws, gcp, oci, or auto). Returns
A new Cls with the merged options.
Usage
You can use this method after looking up the Cls from a deployed App or if you have a direct reference to a Cls from another Function or local entrypoint on its App:
Model = modal.Cls.from_name("my_app", "Model")
ModelUsingGPU = Model.with_options(gpu="A100")
ModelUsingGPU().generate.remote(input_prompt) # Run with an A100 GPUThe method can be called multiple times to “stack” updates:
Model.with_options(gpu="A100").with_options(scaledown_window=300) # Use an A100 with slow scaledownwith_concurrency
with_concurrency(self, *, max_inputs, target_inputs=None)Override the static Cls configuration with invocation-specific input concurrency settings.
Parameters
@app.cls / Function concurrency docs. Returns
A new Cls with the merged concurrency settings.
Usage
Model = modal.Cls.from_name("my_app", "Model")
ModelUsingGPU = Model.with_options(gpu="A100").with_concurrency(max_inputs=100)
ModelUsingGPU().generate.remote(42) # will run on an A100 GPU with input concurrency enabledwith_batching
with_batching(self, *, max_batch_size, wait_ms)Override the static Cls configuration with invocation-specific dynamic batching settings.
Parameters
Returns
A new Cls with the merged batching settings.
Usage
Model = modal.Cls.from_name("my_app", "Model")
ModelUsingGPU = Model.with_options(gpu="A100").with_batching(max_batch_size=100, wait_ms=1000)
ModelUsingGPU().generate.remote(42) # A100 with dynamic batching