Recommended config_dict for pydantic.BaseModel

Tags:

It’s difficult to understand why the below is not the default of the pydantic.BaseModel since they look so basic, but anyhow, these are ones I found and keep using.

model_config = ConfigDict(
    arbitrary_types_allowed=True,  # to allow arbitrary type like pl.DataFrame
    extra="forbid",  # extra parameters are not accepted
    protected_namespaces=("protected_",),  # default is 'model_'
    validate_assignment=True,  # assignment is also validated
)

extra=”forbid” prevents following from passing.

classs Foo(BaseModel):
  ohlc_ratio: bool

Foo(ohlc=True)

protected_namespace=(“protected_”, ) makes it possible to use field name that starts with model_. Without it, model_name field causes a trouble.

class Foo(BaseModel):  # error
  model_name: str