Skip to content

Error when generate openapi.json, Object of type 'type' is not JSON serializable #5201

@yudataguy

Description

@yudataguy

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from pydantic import BaseModel

class BaseResponse(BaseModel):
    success: bool = True
    message: str = "Success"
    data: dict = {}
    execution_time_ms: float = 0.0

class Detect(BaseResponse):
    data: dict = {}

router = APIRouter()

@router.post("/detect", response_model=Detect, status_code=200)
def detect(
    image_file: UploadFile = File(None),
    image_base64: str = Form(None),
    image_url: str = Form(None),
    detection: bool = Form(...),
) -> Detect:
    return Detect(
        success=True,
        message="detected in the image",
        data=response_data,
        execution_time_ms=0,
    )

Description

 File "/home/sam/Documents/onp/app/venv/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 418, in get_openapi
    definitions = get_model_definitions(
                  └ <function get_model_definitions at 0x7f72d5015c10>
  File "/home/sam/Documents/onp/app/venv/lib/python3.8/site-packages/fastapi/utils.py", line 28, in get_model_definitions
    m_schema, m_definitions, m_nested_models = model_process_schema(
    │         │                                └ <cyfunction model_process_schema at 0x7f72d54f6040>
    │         └ {}
    └ {'title': 'Detect', 'type': 'object', 'properties': {'success': {'title': 'Success', 'default': True, 'type': 'boolean'},...

  File "pydantic/schema.py", line 617, in pydantic.schema.model_process_schema

  File "pydantic/schema.py", line 658, in pydantic.schema.model_type_schema

  File "pydantic/schema.py", line 251, in pydantic.schema.field_schema

  File "pydantic/schema.py", line 219, in pydantic.schema.get_field_info_schema

  File "pydantic/schema.py", line 982, in pydantic.schema.encode_default

  File "pydantic/schema.py", line 986, in pydantic.schema.encode_default

  File "pydantic/json.py", line 97, in pydantic.json.pydantic_encoder

TypeError: Object of type 'type' is not JSON serializable

Since the error got cut off half way, I can not know which part of the model is referring to type. I've removed Optional from model, and make it as simple as possible. But this error still shows throughout the app, I'm not sure whether it's my BaseResponse issue or function definition issue. Any suggestion on how to debut the issue?

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.75.2

Python Version

3.8.10

Additional Context

No response

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