2

I have data coming into my FastAPI that can take any shape/form and as such I need an empty Pydantic model. I tried creating a dynamic model like this:

DynamicModel = create_model('RandomData', random_data=(dict, ...))

However it requires the model to follow this structure:

{"random_data": { } }

What I would like is for the model to accept an empty json dictionary like this: { }

What am I missing?

cp-stack
  • 785
  • 3
  • 17
  • 40
  • why not just use the request body directly? – python_user Feb 02 '23 at 17:42
  • as in do not use the pydantic model at all and instead just use the response body? – cp-stack Feb 02 '23 at 17:45
  • 3
    yeah, something like https://stackoverflow.com/questions/64379089/how-to-read-body-as-any-valid-json, I am suggesting this because, one of the reasons to use pydantic is for data validation, and in your case that is not what you want – python_user Feb 02 '23 at 17:48
  • You are 100% correct. I will go ahead and do this. Thanks so much! – cp-stack Feb 02 '23 at 17:55
  • Does this answer your question? [How to read body as any valid json?](https://stackoverflow.com/questions/64379089/how-to-read-body-as-any-valid-json) – Yaakov Bressler Feb 04 '23 at 03:30

0 Answers0