<@ULVA73B9P> I have a flow that accepts `pydantic....
# ask-marvin
t
@Marvin I have a flow that accepts
pydantic.BaseModel
configuration class that can be created from
yaml.safe_load(yaml_file)
. After deploying the flow to prefect server, the deployment can be run by passing a json configuration. How do I define a default value for this JSON when deploying this flow so that the UI provides default value for this object then run from the UI?
m
To define a default JSON value for a parameter in a Prefect deployment, you can utilize the
prefect.yaml
file. This file allows you to set base settings for deployments, including default parameter values. Here's a general approach to achieve this: 1. Initialize Deployment Configuration: Use the Prefect command line interface to initialize your deployment configuration. This will generate a
prefect.yaml
file in your project directory.
Copy code
bash
   prefect init
2. Edit the
prefect.yaml
File
: In the
prefect.yaml
file, you can define default parameter values under the
parameters
section. This will ensure that when the deployment is run from the UI, the specified default values are used unless overridden. Here's an example structure for the
prefect.yaml
file:
Copy code
yaml
   deployments:
     - name: my-flow-deployment
       parameters:
         my_config: 
           key1: default_value1
           key2: default_value2
3. Deploy with Defaults: When you deploy your flow using the Prefect CLI, it will use the
prefect.yaml
file to set up the deployment with the specified default parameters. For more detailed information about configuring deployments with YAML, you can refer to the Prefect documentation. This approach will allow you to provide default JSON values for your parameters when running the deployment from the UI.
t
Is there a way to do this directly from the python flow code? For example, the code below works in prefilling the UI's string argument field
name
with
Marvin
on UI:
Copy code
@flow
def hello(name: str = "Marvin"):
n
yeah you can just say like
Copy code
@flow
def f(foo: FooModel = FooModel())
t
tried it, doesn't work
n
can you elaborate on what you tried / what you were expecting / what you saw? it'd be a surprising bug if that doesnt work in general
t
my python code is like this:
Copy code
parameters = { 'field_1': 123, .... }

@flow
def f(foo: FooModel = FooModel(**parameters))
I expect the UI to pre-fill field
field_1
to
123
but it's not
n
hmm on
main
im seeing it work
Copy code
from pydantic import BaseModel
from prefect import flow


class FooModel(BaseModel):
    name: str
    age: int


@flow
def foo(model: FooModel = FooModel(name="John", age=30)):
    print(model)


if __name__ == "__main__":
    foo.serve()
so if you could share your version and an MRE that'd be appreciated
t
i see
actually, nvm, it works, my json config incorrectly has 1 additional level
{ "config": {...} }
which is what the output of the prefect UI's run on tab parameter
n
catjam