Hi everyone! Is it possible to define a deployment...
# ask-community
g
Hi everyone! Is it possible to define a deployment to have an unspecified number of parameters? Basically the same like you would define such a function in python
def fcn(**kwargs)
. Is it possible?
1
n
I think I would reach for a pydantic model here what are you trying to do?
g
I would like to have a flexible way to input parameters in order to avoid having to change multiple dags when I need to add a new parameter. So that every flow has the same interface
I would like each flow exposing one single parameter which is a dict
dict[str, anything]
n
> I would like each flow exposing one single parameter which is a dict > dict[str, anything] if this is the case, why not just do
Copy code
@flow
def f(data: dict[str, Any]):
g
I tried this but it does not work Any from
from typing import Any
?
n
yea using
Any
isnt exactly true bc the values would need to be JSON serializable I'd be inclined to do this
Copy code
from pydantic import BaseModel

from prefect import flow


class Inputs(BaseModel):
    a: int
    b: str
    c: bool
    d: dict[str, str | dict[str, str]]


@flow
def something(data: Inputs):
    print(data)


something({"a": 1, "b": "2", "c": True, "d": {"e": "f", "g": {"h": "i"}}})
because you'd be free to change the impl of
Inputs
as needed, without changing the signature of your functions that use it, and then you get the nice form
g
I want to avoid to list all the parameters because I want to have parameter like
{"a": {"b": {"c": 0, "d": 1}}}
n
you can, this is just a matter of writing a pydantic model that suits your use case if you want to default certain things, then set defaults so users dont have to provide them
Copy code
from pydantic import BaseModel, Field

from prefect import flow


class Inputs(BaseModel):
    a: int
    b: str
    c: bool
    d: dict[str, str | dict[str, str]] = Field(default_factory=dict)


@flow
def something(data: Inputs):
    print(data)


something({"a": 1, "b": "2", "c": True})
or if you want to allow arbitrary extras, you can
Copy code
from pydantic import BaseModel

from prefect import flow


class Inputs(BaseModel, extra="allow"):
    a: int


@flow
def something(data: Inputs):
    print(data)


something({"a": 1, "b": "2", "c": True})
https://docs.pydantic.dev/latest/concepts/models/
g
would this allow me to have multiple nested dicts?
n
yes! i would encourage you to read the pydantic docs, you can have any json serializable type on your inputs model
g
great! I would give it a read, thank you Nate!
n
👍