Make Pydantic chuck error for wrong argument names

Viewed 78

Suppose I have the following class:

class ModelConfig(pydantic.BaseModel):
    name: str = "bert"

If I were to instantiate it with model_config = ModelConfig(name2="hello"), this simply ignores that there is no name2 and just keeps name="bert". Is there a way to raise an error saying unknown argument in pydantic?

1 Answers

You can do this using the forbid Model Config

For example:

class ModelConfig(pydantic.BaseModel, extra=pydantic.Extra.forbid): name: str = "bert"

Passing model_config = ModelConfig(name2="hello") will throw an error

Related