Skip to content

prefect.flows

Module containing the base workflow class and decorator - for most use cases, using the @flow decorator is preferred.

Flow

A Prefect workflow definition.

Note

We recommend using the @flow decorator for most use-cases.

Wraps a function with an entrypoint to the Prefect engine. To preserve the input and output types, we use the generic type variables P and R for "Parameters" and "Returns" respectively.

Parameters:

Name Description Default
fn

The function defining the workflow.

Callable[[~P], ~R]
required
name

An optional name for the flow; if not provided, the name will be inferred from the given function.

str
None
version

An optional version string for the flow; if not provided, we will attempt to create a version string as a hash of the file containing the wrapped function; if the file cannot be located, the version will be null.

str
None
task_runner

An optional task runner to use for task execution within the flow; if not provided, a ConcurrentTaskRunner will be used.

Union[Type[prefect.task_runners.BaseTaskRunner], prefect.task_runners.BaseTaskRunner]
<class 'prefect.task_runners.ConcurrentTaskRunner'>
description

An optional string description for the flow; if not provided, the description will be pulled from the docstring for the decorated function.

str
None
timeout_seconds

An optional number of seconds indicating a maximum runtime for the flow. If the flow exceeds this runtime, it will be marked as failed. Flow execution may continue until the next task is called.

Union[int, float]
None
validate_parameters

By default, parameters passed to flows are validated by Pydantic. This will check that input values conform to the annotated types on the function. Where possible, values will be coerced into the correct type; for example, if a parameter is defined as x: int and "5" is passed, it will be resolved to 5. If set to False, no validation will be performed on flow parameters.

bool
True
Source code in prefect/flows.py
class Flow(Generic[P, R]):
    """
    A Prefect workflow definition.

    !!! note
        We recommend using the [`@flow` decorator][prefect.flows.flow] for most use-cases.

    Wraps a function with an entrypoint to the Prefect engine. To preserve the input
    and output types, we use the generic type variables `P` and `R` for "Parameters" and
    "Returns" respectively.

    Args:
        fn: The function defining the workflow.
        name: An optional name for the flow; if not provided, the name will be inferred
            from the given function.
        version: An optional version string for the flow; if not provided, we will
            attempt to create a version string as a hash of the file containing the
            wrapped function; if the file cannot be located, the version will be null.
        task_runner: An optional task runner to use for task execution within the flow;
            if not provided, a `ConcurrentTaskRunner` will be used.
        description: An optional string description for the flow; if not provided, the
            description will be pulled from the docstring for the decorated function.
        timeout_seconds: An optional number of seconds indicating a maximum runtime for
            the flow. If the flow exceeds this runtime, it will be marked as failed.
            Flow execution may continue until the next task is called.
        validate_parameters: By default, parameters passed to flows are validated by
            Pydantic. This will check that input values conform to the annotated types
            on the function. Where possible, values will be coerced into the correct
            type; for example, if a parameter is defined as `x: int` and "5" is passed,
            it will be resolved to `5`. If set to `False`, no validation will be
            performed on flow parameters.
    """

    # NOTE: These parameters (types, defaults, and docstrings) should be duplicated
    #       exactly in the @flow decorator
    def __init__(
        self,
        fn: Callable[P, R],
        name: str = None,
        version: str = None,
        task_runner: Union[Type[BaseTaskRunner], BaseTaskRunner] = ConcurrentTaskRunner,
        description: str = None,
        timeout_seconds: Union[int, float] = None,
        validate_parameters: bool = True,
    ):
        if not callable(fn):
            raise TypeError("'fn' must be callable")

        # Validate name if given
        if name:
            raise_on_invalid_name(name)

        self.name = name or fn.__name__.replace("_", "-")
        task_runner = task_runner or ConcurrentTaskRunner()
        self.task_runner = (
            task_runner() if isinstance(task_runner, type) else task_runner
        )

        self.description = description or inspect.getdoc(fn)
        update_wrapper(self, fn)
        self.fn = fn
        self.isasync = is_async_fn(self.fn)

        # Version defaults to a hash of the function's file
        flow_file = fn.__globals__.get("__file__")  # type: ignore
        self.version = version or (file_hash(flow_file) if flow_file else None)

        self.timeout_seconds = float(timeout_seconds) if timeout_seconds else None

        self.parameters = parameter_schema(self.fn)
        self.should_validate_parameters = validate_parameters

        if self.should_validate_parameters:
            # Try to create the validated function now so that incompatibility can be
            # raised at declaration time rather than at runtime
            # We cannot, however, store the validated function on the flow because it
            # is not picklable in some environments
            try:
                ValidatedFunction(self.fn, config=None)
            except pydantic.ConfigError as exc:
                raise ValueError(
                    "Flow function is not compatible with `validate_parameters`. "
                    "Disable validation or change the argument names."
                ) from exc

    def with_options(
        self,
        *,
        name: str = None,
        version: str = None,
        description: str = None,
        task_runner: Union[Type[BaseTaskRunner], BaseTaskRunner] = None,
        timeout_seconds: Union[int, float] = None,
        validate_parameters: bool = None,
    ):
        """
        Create a new flow from the current object, updating provided options.

        Args:
            name: A new name for the flow.
            version: A new version for the flow.
            description: A new description for the flow.
            task_runner: A new task runner for the flow.
            timeout_seconds: A new number of seconds to fail the flow after if still
                running.
            validate_parameters: A new value indicating if flow calls should validate
                given parameters.

        Returns:
            A new `Flow` instance.

        Examples:

            Create a new flow from an existing flow and update the name:

            >>> @flow(name="My flow")
            >>> def my_flow():
            >>>     return 1
            >>>
            >>> new_flow = my_flow.with_options(name="My new flow")

            Create a new flow from an existing flow, update the task runner, and call
            it without an intermediate variable:

            >>> from prefect.task_runners import SequentialTaskRunner
            >>>
            >>> @flow
            >>> def my_flow(x, y):
            >>>     return x + y
            >>>
            >>> state = my_flow.with_options(task_runner=SequentialTaskRunner)(1, 3)
            >>> assert state.result() == 4

        """
        return Flow(
            fn=self.fn,
            name=name or self.name,
            description=description or self.description,
            version=version or self.version,
            task_runner=task_runner or self.task_runner,
            timeout_seconds=(
                timeout_seconds if timeout_seconds is not None else self.timeout_seconds
            ),
            validate_parameters=(
                validate_parameters
                if validate_parameters is not None
                else self.should_validate_parameters
            ),
        )

    def validate_parameters(self, parameters: Dict[str, Any]) -> Dict[str, Any]:
        """
        Validate parameters for compatibility with the flow by attempting to cast the inputs to the
        associated types specified by the function's type annotations.

        Returns:
            A new dict of parameters that have been cast to the appropriate types

        Raises:
            FlowParameterError: if the provided parameters are not valid
        """
        validated_fn = ValidatedFunction(self.fn, config=None)
        args, kwargs = parameters_to_args_kwargs(self.fn, parameters)

        validation_err = None
        try:
            model = validated_fn.init_model_instance(*args, **kwargs)
        except pydantic.ValidationError as exc:
            # We capture the pydantic exception and raise our own because the pydantic
            # exception is not picklable when using a cythonized pydantic installation
            validation_err = ParameterTypeError(str(exc))

        if validation_err:
            # Raise the valdiation error outside of the `except` so the pydandic
            # internals are not included
            raise validation_err

        # Get the updated parameter dict with cast values from the model
        cast_parameters = {
            k: v
            for k, v in model._iter()
            if k in model.__fields_set__ or model.__fields__[k].default_factory
        }
        return cast_parameters

    def serialize_parameters(self, parameters: Dict[str, Any]) -> Dict[str, Any]:
        """
        Convert parameters to a serializable form.

        Uses FastAPI's `jsonable_encoder` to convert to JSON compatible objects without
        converting everything directly to a string. This maintains basic types like
        integers during API roundtrips.
        """
        serialized_parameters = {}
        for key, value in parameters.items():
            try:
                serialized_parameters[key] = jsonable_encoder(value)
            except (TypeError, ValueError):
                logger.debug(
                    f"Parameter {key!r} for flow {self.name!r} is of unserializable "
                    f"type {type(value).__name__!r} and will not be stored "
                    "in the backend."
                )
                serialized_parameters[key] = f"<{type(value).__name__}>"
        return serialized_parameters

    @overload
    def __call__(
        self: "Flow[P, NoReturn]", *args: P.args, **kwargs: P.kwargs
    ) -> State[T]:
        # `NoReturn` matches if a type can't be inferred for the function which stops a
        # sync function from matching the `Coroutine` overload
        ...

    @overload
    def __call__(
        self: "Flow[P, Coroutine[Any, Any, T]]", *args: P.args, **kwargs: P.kwargs
    ) -> Awaitable[State[T]]:
        ...

    @overload
    def __call__(self: "Flow[P, T]", *args: P.args, **kwargs: P.kwargs) -> State[T]:
        ...

    def __call__(
        self,
        *args: "P.args",
        **kwargs: "P.kwargs",
    ):
        """
        Run the flow using the Prefect engine against a backing API (note this will create a new flow run in the backend).

        Flow parameter values must be serializable by Pydantic.

        If writing an async flow, this call must be awaited.

        Args:
            *args: Arguments to run the flow with.
            **kwargs: Keyword arguments to run the flow with.

        Returns:
            The final state of the flow run.

        Examples:

            Define a flow

            >>> @flow
            >>> def my_flow(name):
            >>>     print(f"hello {name}")
            >>>     return f"goodbye {name}"

            Run a flow

            >>> my_flow("marvin")
            hello marvin

            Run a flow and get the returned result

            >>> my_flow("marvin").result()
            "goodbye marvin"

            Run a flow with additional tags

            >>> from prefect import tags
            >>> with tags("db", "blue"):
            >>>     my_flow("foo")
        """
        from prefect.engine import enter_flow_run_engine_from_flow_call

        # Convert the call args/kwargs to a parameter dict
        parameters = get_call_parameters(self.fn, args, kwargs)

        return enter_flow_run_engine_from_flow_call(self, parameters)

Flow.__call__ special

Run the flow using the Prefect engine against a backing API (note this will create a new flow run in the backend).

Flow parameter values must be serializable by Pydantic.

If writing an async flow, this call must be awaited.

Parameters:

Name Description Default
*args

Arguments to run the flow with.

P.args
()
**kwargs

Keyword arguments to run the flow with.

P.kwargs
{}

Returns:

Type Description

The final state of the flow run.

Examples:

Define a flow

>>> @flow
>>> def my_flow(name):
>>>     print(f"hello {name}")
>>>     return f"goodbye {name}"

Run a flow

>>> my_flow("marvin")
hello marvin

Run a flow and get the returned result

>>> my_flow("marvin").result()
"goodbye marvin"

Run a flow with additional tags

>>> from prefect import tags
>>> with tags("db", "blue"):
>>>     my_flow("foo")
Source code in prefect/flows.py
def __call__(
    self,
    *args: "P.args",
    **kwargs: "P.kwargs",
):
    """
    Run the flow using the Prefect engine against a backing API (note this will create a new flow run in the backend).

    Flow parameter values must be serializable by Pydantic.

    If writing an async flow, this call must be awaited.

    Args:
        *args: Arguments to run the flow with.
        **kwargs: Keyword arguments to run the flow with.

    Returns:
        The final state of the flow run.

    Examples:

        Define a flow

        >>> @flow
        >>> def my_flow(name):
        >>>     print(f"hello {name}")
        >>>     return f"goodbye {name}"

        Run a flow

        >>> my_flow("marvin")
        hello marvin

        Run a flow and get the returned result

        >>> my_flow("marvin").result()
        "goodbye marvin"

        Run a flow with additional tags

        >>> from prefect import tags
        >>> with tags("db", "blue"):
        >>>     my_flow("foo")
    """
    from prefect.engine import enter_flow_run_engine_from_flow_call

    # Convert the call args/kwargs to a parameter dict
    parameters = get_call_parameters(self.fn, args, kwargs)

    return enter_flow_run_engine_from_flow_call(self, parameters)

Flow.serialize_parameters

Convert parameters to a serializable form.

Uses FastAPI's jsonable_encoder to convert to JSON compatible objects without converting everything directly to a string. This maintains basic types like integers during API roundtrips.

Source code in prefect/flows.py
def serialize_parameters(self, parameters: Dict[str, Any]) -> Dict[str, Any]:
    """
    Convert parameters to a serializable form.

    Uses FastAPI's `jsonable_encoder` to convert to JSON compatible objects without
    converting everything directly to a string. This maintains basic types like
    integers during API roundtrips.
    """
    serialized_parameters = {}
    for key, value in parameters.items():
        try:
            serialized_parameters[key] = jsonable_encoder(value)
        except (TypeError, ValueError):
            logger.debug(
                f"Parameter {key!r} for flow {self.name!r} is of unserializable "
                f"type {type(value).__name__!r} and will not be stored "
                "in the backend."
            )
            serialized_parameters[key] = f"<{type(value).__name__}>"
    return serialized_parameters

Flow.validate_parameters

Validate parameters for compatibility with the flow by attempting to cast the inputs to the associated types specified by the function's type annotations.

Returns:

Type Description
Dict[str, Any]

A new dict of parameters that have been cast to the appropriate types

Exceptions:

Type Description
FlowParameterError

if the provided parameters are not valid

Source code in prefect/flows.py
def validate_parameters(self, parameters: Dict[str, Any]) -> Dict[str, Any]:
    """
    Validate parameters for compatibility with the flow by attempting to cast the inputs to the
    associated types specified by the function's type annotations.

    Returns:
        A new dict of parameters that have been cast to the appropriate types

    Raises:
        FlowParameterError: if the provided parameters are not valid
    """
    validated_fn = ValidatedFunction(self.fn, config=None)
    args, kwargs = parameters_to_args_kwargs(self.fn, parameters)

    validation_err = None
    try:
        model = validated_fn.init_model_instance(*args, **kwargs)
    except pydantic.ValidationError as exc:
        # We capture the pydantic exception and raise our own because the pydantic
        # exception is not picklable when using a cythonized pydantic installation
        validation_err = ParameterTypeError(str(exc))

    if validation_err:
        # Raise the valdiation error outside of the `except` so the pydandic
        # internals are not included
        raise validation_err

    # Get the updated parameter dict with cast values from the model
    cast_parameters = {
        k: v
        for k, v in model._iter()
        if k in model.__fields_set__ or model.__fields__[k].default_factory
    }
    return cast_parameters

Flow.with_options

Create a new flow from the current object, updating provided options.

Parameters:

Name Description Default
name

A new name for the flow.

str
None
version

A new version for the flow.

str
None
description

A new description for the flow.

str
None
task_runner

A new task runner for the flow.

Union[Type[prefect.task_runners.BaseTaskRunner], prefect.task_runners.BaseTaskRunner]
None
timeout_seconds

A new number of seconds to fail the flow after if still running.

Union[int, float]
None
validate_parameters

A new value indicating if flow calls should validate given parameters.

bool
None

Returns:

Type Description

A new Flow instance.

Examples:

Create a new flow from an existing flow and update the name:

>>> @flow(name="My flow")
>>> def my_flow():
>>>     return 1
>>>
>>> new_flow = my_flow.with_options(name="My new flow")

Create a new flow from an existing flow, update the task runner, and call it without an intermediate variable:

>>> from prefect.task_runners import SequentialTaskRunner
>>>
>>> @flow
>>> def my_flow(x, y):
>>>     return x + y
>>>
>>> state = my_flow.with_options(task_runner=SequentialTaskRunner)(1, 3)
>>> assert state.result() == 4
Source code in prefect/flows.py
def with_options(
    self,
    *,
    name: str = None,
    version: str = None,
    description: str = None,
    task_runner: Union[Type[BaseTaskRunner], BaseTaskRunner] = None,
    timeout_seconds: Union[int, float] = None,
    validate_parameters: bool = None,
):
    """
    Create a new flow from the current object, updating provided options.

    Args:
        name: A new name for the flow.
        version: A new version for the flow.
        description: A new description for the flow.
        task_runner: A new task runner for the flow.
        timeout_seconds: A new number of seconds to fail the flow after if still
            running.
        validate_parameters: A new value indicating if flow calls should validate
            given parameters.

    Returns:
        A new `Flow` instance.

    Examples:

        Create a new flow from an existing flow and update the name:

        >>> @flow(name="My flow")
        >>> def my_flow():
        >>>     return 1
        >>>
        >>> new_flow = my_flow.with_options(name="My new flow")

        Create a new flow from an existing flow, update the task runner, and call
        it without an intermediate variable:

        >>> from prefect.task_runners import SequentialTaskRunner
        >>>
        >>> @flow
        >>> def my_flow(x, y):
        >>>     return x + y
        >>>
        >>> state = my_flow.with_options(task_runner=SequentialTaskRunner)(1, 3)
        >>> assert state.result() == 4

    """
    return Flow(
        fn=self.fn,
        name=name or self.name,
        description=description or self.description,
        version=version or self.version,
        task_runner=task_runner or self.task_runner,
        timeout_seconds=(
            timeout_seconds if timeout_seconds is not None else self.timeout_seconds
        ),
        validate_parameters=(
            validate_parameters
            if validate_parameters is not None
            else self.should_validate_parameters
        ),
    )

flow

Decorator to designate a function as a Prefect workflow.

This decorator may be used for asynchronous or synchronous functions.

Flow parameters must be serializable by Pydantic.

Parameters:

Name Description Default
name

An optional name for the flow; if not provided, the name will be inferred from the given function.

str
None
version

An optional version string for the flow; if not provided, we will attempt to create a version string as a hash of the file containing the wrapped function; if the file cannot be located, the version will be null.

str
None
task_runner

An optional task runner to use for task execution within the flow; if not provided, a ConcurrentTaskRunner will be instantiated.

BaseTaskRunner
<class 'prefect.task_runners.ConcurrentTaskRunner'>
description

An optional string description for the flow; if not provided, the description will be pulled from the docstring for the decorated function.

str
None
timeout_seconds

An optional number of seconds indicating a maximum runtime for the flow. If the flow exceeds this runtime, it will be marked as failed. Flow execution may continue until the next task is called.

Union[int, float]
None
validate_parameters

By default, parameters passed to flows are validated by Pydantic. This will check that input values conform to the annotated types on the function. Where possible, values will be coerced into the correct type; for example, if a parameter is defined as x: int and "5" is passed, it will be resolved to 5. If set to False, no validation will be performed on flow parameters.

bool
True

Returns:

Type Description

A callable Flow object which, when called, will run the flow and return its final state.

Examples:

Define a simple flow

>>> from prefect import flow
>>> @flow
>>> def add(x, y):
>>>     return x + y

Define an async flow

>>> @flow
>>> async def add(x, y):
>>>     return x + y

Define a flow with a version and description

>>> @flow(version="first-flow", description="This flow is empty!")
>>> def my_flow():
>>>     pass

Define a flow with a custom name

>>> @flow(name="The Ultimate Flow")
>>> def my_flow():
>>>     pass

Define a flow that submits its tasks to dask

>>> from prefect.task_runners import DaskTaskRunner
>>>
>>> @flow(task_runner=DaskTaskRunner)
>>> def my_flow():
>>>     pass
Source code in prefect/flows.py
def flow(
    __fn=None,
    *,
    name: str = None,
    version: str = None,
    task_runner: BaseTaskRunner = ConcurrentTaskRunner,
    description: str = None,
    timeout_seconds: Union[int, float] = None,
    validate_parameters: bool = True,
):
    """
    Decorator to designate a function as a Prefect workflow.

    This decorator may be used for asynchronous or synchronous functions.

    Flow parameters must be serializable by Pydantic.

    Args:
        name: An optional name for the flow; if not provided, the name will be inferred
            from the given function.
        version: An optional version string for the flow; if not provided, we will
            attempt to create a version string as a hash of the file containing the
            wrapped function; if the file cannot be located, the version will be null.
        task_runner: An optional task runner to use for task execution within the flow; if
            not provided, a `ConcurrentTaskRunner` will be instantiated.
        description: An optional string description for the flow; if not provided, the
            description will be pulled from the docstring for the decorated function.
        timeout_seconds: An optional number of seconds indicating a maximum runtime for
            the flow. If the flow exceeds this runtime, it will be marked as failed.
            Flow execution may continue until the next task is called.
        validate_parameters: By default, parameters passed to flows are validated by
            Pydantic. This will check that input values conform to the annotated types
            on the function. Where possible, values will be coerced into the correct
            type; for example, if a parameter is defined as `x: int` and "5" is passed,
            it will be resolved to `5`. If set to `False`, no validation will be
            performed on flow parameters.

    Returns:
        A callable `Flow` object which, when called, will run the flow and return its
        final state.

    Examples:
        Define a simple flow

        >>> from prefect import flow
        >>> @flow
        >>> def add(x, y):
        >>>     return x + y

        Define an async flow

        >>> @flow
        >>> async def add(x, y):
        >>>     return x + y

        Define a flow with a version and description

        >>> @flow(version="first-flow", description="This flow is empty!")
        >>> def my_flow():
        >>>     pass

        Define a flow with a custom name

        >>> @flow(name="The Ultimate Flow")
        >>> def my_flow():
        >>>     pass

        Define a flow that submits its tasks to dask

        >>> from prefect.task_runners import DaskTaskRunner
        >>>
        >>> @flow(task_runner=DaskTaskRunner)
        >>> def my_flow():
        >>>     pass
    """
    if __fn:
        return cast(
            Flow[P, R],
            Flow(
                fn=__fn,
                name=name,
                version=version,
                task_runner=task_runner,
                description=description,
                timeout_seconds=timeout_seconds,
                validate_parameters=validate_parameters,
            ),
        )
    else:
        return cast(
            Callable[[Callable[P, R]], Flow[P, R]],
            partial(
                flow,
                name=name,
                version=version,
                task_runner=task_runner,
                description=description,
                timeout_seconds=timeout_seconds,
                validate_parameters=validate_parameters,
            ),
        )