docs/v3/api-ref/python/prefect-utilities-callables.mdx
prefect.utilities.callablesUtilities for working with Python callables.
get_call_parameters <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L44" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L84" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_parameter_defaults(fn: Callable[..., Any]) -> dict[str, Any]
Get default parameter values for a callable.
explode_variadic_parameter <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L101" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
```python
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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L135" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
```python
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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L258" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L339" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L351" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L392" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
process_v1_params <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L420" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>process_v1_params(param: inspect.Parameter) -> tuple[str, Any, Any]
create_v1_schema <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L451" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>create_v1_schema(name_: str, model_cfg: type[Any], model_fields: Optional[dict[str, Any]] = None) -> dict[str, Any]
parameter_schema <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L469" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 serializedReturns:
parameter_schema_from_entrypoint <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L495" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
generate_parameter_schema <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L529" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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:
raise_for_reserved_arguments <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L585" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L799" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>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 calledparameters: A dictionary of parameters with iterables to be mapped overReturns:
ParameterSchema <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L376" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Simple data model corresponding to an OpenAPI Schema.
Methods:
model_dump_for_openapi <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/callables/__init__.py#L385" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>model_dump_for_openapi(self) -> dict[str, Any]