convert pydantic dataclass with extra arguments to dict – Pydantic

by
Ali Hasan
llama-cpp-python pydantic python-dataclasses

The Problem:

Convert a pydantic dataclass with extra arguments to a dict. The issue arises when attempting to use dataclasses.asdict() on the parsed dataclass, resulting in a TypeError. Despite allowing extra arguments in the dataclass configuration, the conversion fails.

The Solutions:

Solution 1: Using `__pydantic_model__.parse_obj` and `dict` method

Here’s how you can convert a Pydantic dataclass with extra arguments to a dict:

from pydantic.dataclasses import dataclass as pydantic_dataclass

@pydantic_dataclass(config={"extra": "allow"})
class MyDataModel:
    foo: int

data = {"foo": 1, "bar": 2}
dc = MyDataModel.__pydantic_model__.parse_obj(data)
print(dc.dict())  # {'foo': 1, 'bar': 2}

In this approach, we use the __pydantic_model__ attribute of the dataclass to access the underlying BaseModel subclass. We then use the parse_obj method of the model to create an instance of the model with the given data. Finally, we use the dict method of the model instance to convert it to a dictionary.

Solution 2: Initializing the dataclass with extra arguments

Alternatively, you can initialize the dataclass with the extra arguments directly:

dc = MyDataModel(**data)

However, the built-in dataclasses.asdict method will ignore any "extra" fields when converting the dataclass to a dict. To work around this, you can implement your own as_dict function:

def as_dict(obj: object) -> Dict[str, Any]:
    output = {}
    for name, value in obj.__dict__.items():
        if name == "__pydantic_initialised__":
            continue
        if hasattr(type(value), "__pydantic_initialised__"):
            value = as_dict(value)
        output[name] = value
    return output

You can then use the as_dict function to convert the dataclass to a dict:

print(as_dict(dc))  # {'foo': 1, 'bar': 2}

Video Explanation:

The following video, titled "This Is Why Python Data Classes Are Awesome - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

can do with dataclasses as well as new capabilities that have been added ... Attrs, Pydantic, or Python Data Classes? ArjanCodes•70K views · 24:31.