class Function(modal.object.Object)

Functions are the basic units of serverless execution on Modal.

Generally, you will not construct a Function directly. Instead, use the @app.function() decorator on the App object (formerly called “Stub”) for your application.

def __init__(self, *args, **kwargs):


def keep_warm(self, warm_pool_size: int) -> None:

Set the warm pool size for the function (including parametrized functions).

Please exercise care when using this advanced feature! Setting and forgetting a warm pool on functions can lead to increased costs.

# Usage on a regular function.
f = modal.Function.lookup("my-app", "function")

# Usage on a parametrized function.
Model = modal.Cls.lookup("my-app", "Model")


def from_name(
    cls: Type["_Function"],
    app_name: str,
    tag: Optional[str] = None,
    environment_name: Optional[str] = None,
) -> "_Function":

Retrieve a function with a given name and tag.

other_function = modal.Function.from_name("other-app", "function")


def lookup(
    app_name: str,
    tag: Optional[str] = None,
    client: Optional[_Client] = None,
    environment_name: Optional[str] = None,
) -> "_Function":

Lookup a function with a given name and tag.

other_function = modal.Function.lookup("other-app", "function")


def web_url(self) -> str:

URL of a Function running as a web endpoint.


def remote(self, *args, **kwargs) -> Any:

Calls the function remotely, executing it with the given arguments and returning the execution’s result.


def remote_gen(self, *args, **kwargs) -> AsyncGenerator[Any, None]:

Calls the generator remotely, executing it with the given arguments and returning the execution’s result.


def shell(self, *args, **kwargs) -> None:


def local(self, *args, **kwargs) -> Any:

Calls the function locally, executing it with the given arguments and returning the execution’s result.

The function will execute in the same environment as the caller, just like calling the underlying function directly in Python. In particular, only secrets available in the caller environment will be available through environment variables.


def spawn(self, *args, **kwargs) -> Optional["_FunctionCall"]:

Calls the function with the given arguments, without waiting for the results.

Returns a modal.functions.FunctionCall object, that can later be polled or waited for using .get(timeout=...). Conceptually similar to multiprocessing.pool.apply_async, or a Future/Promise in other contexts.

Note: .spawn() on a modal generator function does call and execute the generator, but does not currently return a function handle for polling the result.


def get_raw_f(self) -> Callable[..., Any]:

Return the inner Python object wrapped by this Modal Function.


def get_current_stats(self) -> FunctionStats:

Return a FunctionStats object describing the current function’s queue and runner counts.


def map(
    *input_iterators: typing.Iterable[Any],  # one input iterator per argument in the mapped-over function/generator
    kwargs={},  # any extra keyword arguments for the function
    order_outputs: bool = True,  # return outputs in order
    return_exceptions: bool = False,  # propagate exceptions (False) or aggregate them in the results list (True)
) -> AsyncOrSyncIteratable:

Parallel map over a set of inputs.

Takes one iterator argument per argument in the function being mapped over.


def my_func(a):
    return a ** 2

def main():
    assert list([1, 2, 3, 4])) == [1, 4, 9, 16]

If applied to a stub.function, map() returns one result per input and the output order is guaranteed to be the same as the input order. Set order_outputs=False to return results in the order that they are completed instead.

return_exceptions can be used to treat exceptions as successful results:

def my_func(a):
    if a == 2:
        raise Exception("ohno")
    return a ** 2

def main():
    # [0, 1, UserCodeException(Exception('ohno'))]
    print(list(, return_exceptions=True)))


def starmap(
    input_iterator: typing.Iterable[typing.Sequence[Any]],
    order_outputs: bool = True,
    return_exceptions: bool = False,
) -> AsyncOrSyncIteratable:

Like map, but spreads arguments over multiple function arguments.

Assumes every input is a sequence (e.g. a tuple).


def my_func(a, b):
    return a + b

def main():
    assert list(my_func.starmap([(1, 2), (3, 4)])) == [3, 7]


def for_each(self, *input_iterators, kwargs={}, ignore_exceptions: bool = False):

Execute function for all inputs, ignoring outputs.

Convenient alias for .map() in cases where the function just needs to be called. as the caller doesn’t have to consume the generator to process the inputs.


class FunctionCall(modal.object.Object)

A reference to an executed function call.

Constructed using .spawn(...) on a Modal function with the same arguments that a function normally takes. Acts as a reference to an ongoing function call that can be passed around and used to poll or fetch function results at some later time.

Conceptually similar to a Future/Promise/AsyncResult in other contexts and languages.

def __init__(self, *args, **kwargs):


def get(self, timeout: Optional[float] = None):

Get the result of the function call.

This function waits indefinitely by default. It takes an optional timeout argument that specifies the maximum number of seconds to wait, which can be set to 0 to poll for an output immediately.

The returned coroutine is not cancellation-safe.


def get_call_graph(self) -> List[InputInfo]:

Returns a structure representing the call graph from a given root call ID, along with the status of execution for each node.

See modal.call_graph reference page for documentation on the structure of the returned InputInfo items.


def cancel(self):

Cancels the function call, which will stop its execution and mark its inputs as TERMINATED.


def from_id(function_call_id: str, client: Optional[_Client] = None) -> "_FunctionCall":


class FunctionStats(object)

Simple data structure storing stats for a running function.

def __init__(self, backlog: int, num_total_runners: int) -> None


async def gather(*function_calls: _FunctionCall):

Wait until all Modal function calls have results before returning

Accepts a variable number of FunctionCall objects as returned by Function.spawn().

Returns a list of results from each function call, or raises an exception of the first failing function call.


function_call_1 = slow_func_1.spawn()
function_call_2 = slow_func_2.spawn()

result_1, result_2 = gather(function_call_1, function_call_2)