Skip to main content
from fal import (
    App,
    HealthCheck,
    FalServerlessKeyCredentials,
    ContainerImage,
    function,
    cached,
    endpoint,
    realtime,
    sync_dir,
)

Classes

App

class fal.App
Create a fal serverless application. Subclass this to define your application with custom setup, endpoints, and configuration. The App class handles model loading, request routing, and lifecycle management. Example:
>>> class TextToImage(fal.App, machine_type="GPU"):
...     requirements = ["diffusers", "torch"]
...     
...     def setup(self):
...         self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
...     
...     @fal.endpoint("/")
...     def generate(self, prompt: str) -> dict:
...         image = self.pipe(prompt).images[0]
...         return {"url": fal.toolkit.upload_image(image)}
Inherits from: BaseServable

Constructor Parameters

NameTypeDefaultDescription
_allow_initboolFalse-

Class Variables

NameTypeDefaultDescription
requirementsClassVar[list[str]][]List of pip packages to install in the environment. Supports standard pip syntax including version specifiers. Example: ["numpy==1.24.0", "torch\>=2.0.0"]
local_python_modulesClassVar[list[str]][]List of local Python module names to include in the deployment. Use for custom code not available on PyPI. Example: ["my_utils", "models"]
machine_typeClassVar[str | list[str]]'S'Compute instance type. Options: ‘S’ (small), ‘M’ (medium), ‘L’ (large), ‘GPU’ (T4), ‘A100’ (A100 40GB). Use a list for fallback. Example: "GPU" or ["A100", "GPU"]
num_gpusClassVar[int | None]NoneNumber of GPUs to allocate. Only applies to GPU machine types.
regionsClassVar[Optional[list[str]]]NoneAllowed regions for deployment. None means any region. Example: ["us-east", "eu-west"]
host_kwargsClassVar[dict[str, Any]]\{'_scheduler': 'nomad', '_scheduler_options': \{'storage_region': 'us-east'\}, 'resolver': 'uv', 'keep_alive': 60\}Advanced configuration dictionary passed to the host. For internal use. Prefer using class attributes instead.
app_nameClassVar[Optional[str]]NoneCustom name for the application. Defaults to class name.
app_authClassVar[Optional[AuthModeLiteral]]NoneAuthentication mode. Options: ‘private’ (API key required), ‘public’ (no auth), ‘shared’ (shareable link).
app_filesClassVar[list[str]][]List of files/directories to include in deployment. Example: ["./models", "./config.yaml"]
app_files_ignoreClassVar[list[str]]['\\.pyc$', '__pycache__/', '\\.git/', '\\.DS_Store$']Regex patterns to exclude from deployment. Default excludes .pyc, __pycache__, .git, .DS_Store.
app_files_context_dirClassVar[Optional[str]]NoneBase directory for resolving app_files paths. Defaults to the directory containing the app file.
request_timeoutClassVar[Optional[int]]NoneMaximum seconds for a single request. None for default.
startup_timeoutClassVar[Optional[int]]NoneMaximum seconds for app startup/setup. None for default.
min_concurrencyClassVar[Optional[int]]NoneMinimum warm instances to keep running. Set to 1+ to avoid cold starts. Default is 0 (scale to zero).
max_concurrencyClassVar[Optional[int]]NoneMaximum instances to scale up to.
concurrency_bufferClassVar[Optional[int]]NoneAdditional instances to keep warm above current load.
concurrency_buffer_percClassVar[Optional[int]]NonePercentage buffer of instances above current load.
scaling_delayClassVar[Optional[int]]NoneSeconds to wait before scaling down idle instances.
max_multiplexingClassVar[Optional[int]]NoneMaximum concurrent requests per instance.
kindClassVar[Optional[str]]NoneDeployment kind. For internal use.
imageClassVar[Optional[ContainerImage]]NoneCustom container image for the application. Use ContainerImage to specify a Dockerfile.
local_file_pathClassVar[Optional[str]]None-
isolate_channelasync_grpc.Channel | NoneNone-

Methods

collect_routes

def collect_routes(self) -> 'dict[RouteSignature, Callable[..., Any]]'
Returns: dict[RouteSignature, Callable[Ellipsis, Any]]

get_endpoints

def get_endpoints(cls) -> 'list[str]'
Returns: list[str]

get_health_check_config

def get_health_check_config(cls) -> 'Optional[ApplicationHealthCheckConfig]'
Returns: Optional[ApplicationHealthCheckConfig]

health

def health(self)

lifespan

def lifespan(self, app: 'fastapi.FastAPI')
ParameterTypeDefaultDescription
appFastAPI--

provide_hints

def provide_hints(self) -> 'list[str]'
Provide hints for routing the application.Returns: list[str]

setup

def setup(self)
Setup the application before serving.

teardown

def teardown(self)
Teardown the application after serving.

HealthCheck

class fal.HealthCheck

Constructor Parameters

NameTypeDefaultDescription
start_period_secondsOptional[int]NoneMinimum time the runner has been running before considering the runner unhealthy when health check fails. To prevent the health check from failing too early, this will be replaced by startup_timeout of the application if it’s less than it. Defaults to 30.
timeout_secondsOptional[int]NoneTimeout in seconds for the health check request. Defaults to 5 seconds.
failure_thresholdOptional[int]NoneNumber of consecutive failures before considering the runner as unhealthy. Defaults to 3.
call_regularlyOptional[bool]NonePerform health check every 15s. If false, only do it when the x-fal-runner-health-check header is present. Defaults to True.

Class Variables

NameTypeDefaultDescription
start_period_secondsOptional[int]None-
timeout_secondsOptional[int]None-
failure_thresholdOptional[int]None-
call_regularlyOptional[bool]None-

FalServerlessKeyCredentials

class fal.FalServerlessKeyCredentials
Inherits from: Credentials

Constructor Parameters

NameTypeDefaultDescription
key_idstr--
key_secretstr--

Class Variables

NameTypeDefaultDescription
key_idstr--
key_secretstr--

Methods

to_grpc

def to_grpc(self) -> 'grpc.ChannelCredentials'
Returns: ChannelCredentials

to_headers

def to_headers(self) -> 'dict[str, str]'
Returns: dict[str, str]

ContainerImage

class fal.ContainerImage
ContainerImage represents a Docker image that can be built from a Dockerfile.

Constructor Parameters

NameTypeDefaultDescription
dockerfile_strstr--
build_argsdict[str, str]\<factory\>-
registriesdict[str, dict[str, str]]\<factory\>-
builderOptional[Literal[depot, service, worker]]None-
compressionstr'gzip'-
force_compressionboolFalse-
secretsdict[str, str]\<factory\>-

Class Variables

NameTypeDefaultDescription
dockerfile_strstr--
build_argsdict[str, str]--
registriesdict[str, dict[str, str]]--
builderOptional[Literal[depot, service, worker]]None-
compressionstr'gzip'-
force_compressionboolFalse-
secretsdict[str, str]--

Methods

from_dockerfile

def from_dockerfile(cls, path: str, **kwargs) -> 'ContainerImage'
ParameterTypeDefaultDescription
pathstr--
kwargs---
Returns: ContainerImage

from_dockerfile_str

def from_dockerfile_str(cls, text: str, **kwargs) -> 'ContainerImage'
ParameterTypeDefaultDescription
textstr--
kwargs---
Returns: ContainerImage

to_dict

def to_dict(self) -> dict
Returns: dict

Functions

function

def function(kind: 'str' = 'virtualenv', *, host: 'Host | None' = None, local_python_modules: 'list[str] | None' = None, **config: 'Any')
ParameterTypeDefaultDescription
kindstr'virtualenv'-
hostfal.api.api.Host | NoneNone-
local_python_moduleslist[str] | NoneNone-
configAny--

cached

def cached(func: 'Callable[ArgsT, ReturnT]') -> 'Callable[ArgsT, ReturnT]'
Cache the result of the given function in-memory.
ParameterTypeDefaultDescription
funcCallable[ArgsT, ReturnT]--
Returns: Callable[ArgsT, ReturnT]

endpoint

def endpoint(path: 'str', *, is_websocket: 'bool' = False, health_check: 'HealthCheck | None' = None) -> 'Callable[[EndpointT], EndpointT]'
Designate the decorated function as an application endpoint.
ParameterTypeDefaultDescription
pathstr--
is_websocketboolFalse-
health_checkfal.sdk.HealthCheck | NoneNone-
Returns: Callable[EndpointT, EndpointT]

realtime

def realtime(path: 'str', *, buffering: 'int | None' = None, session_timeout: 'float | None' = None, input_modal: 'Any | None' = <object object at 0x102ffa3a0>, max_batch_size: 'int' = 1) -> 'Callable[[EndpointT], EndpointT]'
Designate the decorated function as a realtime application endpoint.
ParameterTypeDefaultDescription
pathstr--
bufferingint | NoneNone-
session_timeoutfloat | NoneNone-
input_modalAny | None\<object object at 0x102ffa3a0\>-
max_batch_sizeint1-
Returns: Callable[EndpointT, EndpointT]

sync_dir

def sync_dir(local_dir: 'str | Path', remote_dir: 'str', force_upload=False) -> 'str'
ParameterTypeDefaultDescription
local_dirstr | pathlib.Path--
remote_dirstr--
force_upload-False-
Returns: str