0

I am trying to use both item and optional file upload functions in FastAPI, but it is not working properly. My model is TextData, which includes a mode and a data list. I expected my request to receive TextData and optional file uploads, but I encountered an issue. Here is my request code:


router = APIRouter()


class TextData(BaseModel):

    mode: Optional[Literal["fast", "accurate", "default"]] = Field(...)

    data: List[Dict[str, Any]] = Field(...)

    class Config:
        schema_extra = {
            "example": {
                "data": [{
                    "text": "平原上的火焰宣布延期上映"
                }, {
                    "text": "第十四届全运会在西安举办"
                }]
            }
        }


@router.post("/")
async def create_upload_file(
        data: TextData,
        custom_dict: Optional[UploadFile] = File("None"),
):
    if not custom_dict:
        return {"mode": data.mode, "data": data, "custom_dict": custom_dict.filename}
    return {"mode": data.mode, "data": data}

the data is :

{
  "mode": "fast",
  "data": [
    {
      "text": "testdata1"
    },
    {
      "text": "testdata2"
    }
  ]
}

Error: Unprocessable Entity

{
  "detail": [
    {
      "loc": [
        "body",
        "data",
        0
      ],
      "msg": "value is not a valid dict",
      "type": "type_error.dict"
    }
  ]
}

0 Answers0