Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve pydantic model is not JSON serializable [duplicate]

I am having below pydantic models.

class SubModel(BaseModel):
    columns: Mapping
    key: List[str]
    required: Optional[List[str]]

    class Config:
        anystr_strip_whitespace: True
        extra: Extra.allow
        allow_population_by_field_name: True


class MyModel(BaseModel):
    name: str
    config1: Optional[SubModel]
    config2: Optional[Mapping]
    class Config:
        anystr_strip_whitespace: True
        extra: Extra.allow
        allow_population_by_field_name: True

When I am trying to do a dumps on this, I am getting model is not JSON serializable

from io import BytesIO
from orjson import dumps
    
bucket = s3.Bucket(bucket_name)
bucket.upload(BytesIO(dumps(data)), key, ExtraArgs={'ContentType': 'application/json'})

Error -

TypeError: Type is not JSON serializable: MyModel

data is a normal python dictionary with one of item of type MyModel. Tried to use .json() but get dict has no attribute json.

I am stuck here. Can someone help me.

like image 678
Naxi Avatar asked Sep 04 '25 04:09

Naxi


1 Answers

Got similar issue for FastAPI response, solved by:

return JSONResponse(content=jsonable_encoder(item), status_code=200)

or can be just like this:

return jsonable_encoder(item)

where jsonable_encoder is:

from fastapi.encoders import jsonable_encoder

More details are here: https://fastapi.tiangolo.com/tutorial/encoder/

like image 55
Taras Buha Avatar answered Sep 05 '25 20:09

Taras Buha