If you return response type with JSON, it will reorder the keys as fastAPI uses json_enocode (https://fastapi.tiangolo.com/tutorial/encoder/)
To tackle this issue, you can return the json response media-type application/text; this will return a text response, i.e., return will be a string.
from fastapi import Response
@classifier_router.post("/groupClassifier")
async def group_classifier(
# current_user: User = Depends(get_current_user),
group_id: str = Query(default="1069302375", description=GROUP_ID_DESC),
starttime: Union[datetime , None] = DEFAULT_START_TIME,
endtime: Union[datetime , None] = DEFAULT_END_TIME):
result = handler.group_classifier(
[group_id],
starttime,
endtime)
if result==None:
raise HTTPException(
status_code=500
)
else:
return Response(
content=json.dumps(result), media_type="application/text"
)
I hope this will work for you. I have the same issue I have resolved using the above change, i.e., changing the media_type in response.