prefect.utilities.callables

Utilities for working with Python callables.

Functions

get_call_parameters

get_call_parameters(fn: Callable[..., Any], call_args: tuple[Any, ...], call_kwargs: dict[str, Any], apply_defaults: bool = True) -> dict[str, Any]

Bind a call to a function to get parameter/value mapping. Default values on the signature will be included if not overridden.

If the function has a __prefect_self__ attribute, it will be included as the first parameter. This attribute is set when Prefect decorates a bound method, so this approach allows Prefect to work with bound methods in a way that is consistent with how Python handles them (i.e. users don’t have to pass the instance argument to the method) while still making the implicit self argument visible to all of Prefect’s parameter machinery (such as cache key functions).

Raises a ParameterBindError if the arguments/kwargs are not valid for the function

get_parameter_defaults

get_parameter_defaults(fn: Callable[..., Any]) -> dict[str, Any]

Get default parameter values for a callable.

explode_variadic_parameter

explode_variadic_parameter(fn: Callable[..., Any], parameters: dict[str, Any]) -> dict[str, Any]

Given a parameter dictionary, move any parameters stored in a variadic keyword argument parameter (i.e. **kwargs) into the top level.

Example:

def foo(a, b, **kwargs):
    pass

parameters = {"a": 1, "b": 2, "kwargs": {"c": 3, "d": 4}}
explode_variadic_parameter(foo, parameters)
# {"a": 1, "b": 2, "c": 3, "d": 4}

collapse_variadic_parameters

collapse_variadic_parameters(fn: Callable[..., Any], parameters: dict[str, Any]) -> dict[str, Any]

Given a parameter dictionary, move any parameters stored not present in the signature into the variadic keyword argument.

Example:

def foo(a, b, **kwargs):
    pass

parameters = {"a": 1, "b": 2, "c": 3, "d": 4}
collapse_variadic_parameters(foo, parameters)
# {"a": 1, "b": 2, "kwargs": {"c": 3, "d": 4}}

parameters_to_args_kwargs

parameters_to_args_kwargs(fn: Callable[..., Any], parameters: dict[str, Any]) -> tuple[tuple[Any, ...], dict[str, Any]]

Convert a parameters dictionary to positional and keyword arguments

The function must have an identical signature to the original function or this will return an empty tuple and dict.

call_with_parameters

call_with_parameters(fn: Callable[..., R], parameters: dict[str, Any]) -> R

Call a function with parameters extracted with get_call_parameters

The function must have an identical signature to the original function or this will fail. If you need to send to a function with a different signature, extract the args/kwargs using parameters_to_positional_and_keyword directly

cloudpickle_wrapped_call

cloudpickle_wrapped_call(__fn: Callable[..., Any], *args: Any, **kwargs: Any) -> Callable[[], bytes]

Serializes a function call using cloudpickle then returns a callable which will execute that call and return a cloudpickle serialized return value

This is particularly useful for sending calls to libraries that only use the Python built-in pickler (e.g. anyio.to_process and multiprocessing) but may require a wider range of pickling support.

parameter_docstrings

parameter_docstrings(docstring: Optional[str]) -> dict[str, str]

Given a docstring in Google docstring format, parse the parameter section and return a dictionary that maps parameter names to docstring.

Args:

  • docstring: The function’s docstring.

Returns:

  • Mapping from parameter names to docstrings.

process_v1_params

process_v1_params(param: inspect.Parameter) -> tuple[str, Any, Any]

create_v1_schema

create_v1_schema(name_: str, model_cfg: type[Any], model_fields: Optional[dict[str, Any]] = None) -> dict[str, Any]

parameter_schema

parameter_schema(fn: Callable[..., Any]) -> ParameterSchema

Given a function, generates an OpenAPI-compatible description of the function’s arguments, including:

  • name
  • typing information
  • whether it is required
  • a default value
  • additional constraints (like possible enum values)

Args:

  • fn: The function whose arguments will be serialized

Returns:

  • the argument schema

parameter_schema_from_entrypoint

parameter_schema_from_entrypoint(entrypoint: str) -> ParameterSchema

Generate a parameter schema from an entrypoint string.

Will load the source code of the function and extract the signature and docstring to generate the schema.

Useful for generating a schema for a function when instantiating the function may not be possible due to missing imports or other issues.

Args:

  • entrypoint: A string representing the entrypoint to a function. The string should be in the format of module.path.to.function\:do_stuff.

Returns:

  • The parameter schema for the function.

generate_parameter_schema

generate_parameter_schema(signature: inspect.Signature, docstrings: dict[str, str]) -> ParameterSchema

Generate a parameter schema from a function signature and docstrings.

To get a signature from a function, use inspect.signature(fn) or _generate_signature_from_source(source_code, func_name).

Args:

  • signature: The function signature.
  • docstrings: A dictionary mapping parameter names to docstrings.

Returns:

  • The parameter schema.

raise_for_reserved_arguments

raise_for_reserved_arguments(fn: Callable[..., Any], reserved_arguments: Iterable[str]) -> None

Raise a ReservedArgumentError if fn has any parameters that conflict with the names contained in reserved_arguments.

expand_mapping_parameters

expand_mapping_parameters(func: Callable[..., Any], parameters: dict[str, Any]) -> list[dict[str, Any]]

Generates a list of call parameters to be used for individual calls in a mapping operation.

Args:

  • func: The function to be called
  • parameters: A dictionary of parameters with iterables to be mapped over

Returns:

  • A list of dictionaries to be used as parameters for each call in the mapping operation

Classes

ParameterSchema

Simple data model corresponding to an OpenAPI Schema.

Methods:

model_dump_for_openapi

model_dump_for_openapi(self) -> dict[str, Any]

ModelConfig