pythonpython-3.xpydanticpydantic-v2

Pydantic TypeError: validate() takes 2 positional arguments but 3 were given


This is my JsonFeedOptions class.

class JsonFeedOptions(BaseModel):
    address: Optional[AddressType] = None
    signer: Optional[Union[AccountAPI, str]] = None
    Type: Optional[FeedType] = None

And I'm trying to validate the data like this.

opts = {"signer": "abcd"}

options = JsonFeedOptions.model_validate(opts)

But got this error

>           options = JsonFeedOptions.model_validate(opts)
E           TypeError: validate() takes 2 positional arguments but 3 were given

Solution

  • Which Pydantic version do you use? You can check this by pip list and review the library version (do not forget to activate virtual environment if you have one)

    The error TypeError: BaseModel.validate() takes 2 positional arguments, but 3 were given is relevant to the difference between Pydantic versions 1 and 2.

    In Pydantic version 2, the method signature for BaseModel.validate() has been modified, which may lead to this error.

    In any case, you cannot touch the Pydantic library method, but you have two options defined here.

    Option 1: Migrate your code to new Pydantic version Option 2: Use the old version with from pydantic.v1 import BaseModel instead of from pydantic import BaseModel. Keep in the mind that this v1 call is possible if you have new version.