Skip to content

TypeError: Object of type '_GenericAlias' is not JSON serializable #1504

@Pk13055

Description

@Pk13055

Describe the bug

I am unable to debug the issue with some of my model schemas (the traceback for this issue is not helpful). For context, I have a Base model that contains the relevant json_encoders for the non standard fields (datetime, ObjectId, etc), yet there is some pydantic field that isn't being translated.

The schemas uses the following EXTRA fields (from pydantic):

  • AnyUrl
  • Dict
  • SecretStr
  • Field
  • Union

Environment

  • OS: Linux
  • FastAPI Version 0.5.2
  • Python version: 3.7.4

Traceback

...
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 503, in model_type_schema
f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, known_models=known_models
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 186, in field_schema
s['default'] = encode_default(field.default)
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 725, in encode_default
return pydantic_encoder(dft)
File "/usr/local/lib/python3.7/site-packages/pydantic/json.py", line 59, in pydantic_encoder
raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
TypeError: Object of type '_GenericAlias' is not JSON serializable

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions