<@ULVA73B9P> can i set a default text into my dict...
# ask-marvin
k
@Marvin can i set a default text into my dict in the Field?
m
To assist you better, I'll need to confirm which version of Prefect you are using, as the approach to setting default values in fields might differ between Prefect 2.x and Prefect 3.x. If you know which version you're using, please let me know! Otherwise, I can provide information for both versions, or guide you through any steps to find out.
k
@Marvin im using prefect 2
m
In Prefect 2, you can define default values within a custom block field using Pydantic models. You define default values for fields by assigning them in the class definition of your
Block
. This is illustrated in the example below:
Copy code
python
from typing import Optional
from prefect.blocks.core import Block

class MyConfig(Block):
    my_default_field: Optional[str] = "default text"
    another_field: Optional[str] = None

# Usage
config = MyConfig()
print(config.my_default_field)  # Output: "default text"
In this example, the field
my_default_field
is set with a default value of "default text". When you create an instance of
MyConfig
and don't provide a value for
my_default_field
, it defaults to this text. You can find more details on how to define custom blocks and fields in the Prefect 2 documentation on Blocks. Let me know if you need further assistance!
n
hi @Kamilly Victória Ruseler - i might be in a better position to help with pydantic questions if you're asking about
Field
from pydantic?
k
yess, @Nate. i've missed you, by the way haha.
n
🙂 can you expand on this a little?
can i set a default text into my dict in the Field?
you can set default values for fields in 1 of 2 main ways • the
default
kwarg
Copy code
class Foo(BaseModel):
  d: dict = Field(default=dict(some_key=42))
• default factories (a function that returns your default)
Copy code
class Foo(BaseModel):
  d: dict = Field(default_factory=lambda: dict(some_key=42))
if this is not what you're trying to do, please feel free to give a small example!
k
yeah! i was doing like this: hyperparams: Optional[Dict[str, Any]] = Field( default_factory=lambda: { "objective": "", "boosting": "", "num_iterations": [], "learning_rate": [], "num_leaves": [], "max_depth": [], "bagging_fraction": [], "lambda_l1": [], "lambda_l2": [], } ) i followed your example using default instead of default_factory and it worked! thank you so much, again. for the 10000th time hahah
n
no problem! just a pedantic note that you might already know, but just in case you don't: counterintuitively,
Optional
tells pydantic that "`None` is a valid value for `hyperparams`" i.e.
Optional
does not exactly mean that a user can "optionally" pass the value of
hyperparams
so if you just want
hyperparams
to be a
dict
that defaults to the return of your lambda, you would have
Copy code
class Model(BaseModel):
  hyperparams: Dict[str, Any] = Field(default_factory=lambda: dict(...))
k
thank you, nate! also, i have another doubt. when i go to the "Use json input" section, my hyperparams field receives a "prefect_kind" parameter. why does it happen? "hyperparams": { "value": "{\n \"lgbm\": {\n}", "__prefect_kind": "json" },