12

I am writing a post-api using fastapi. The required request-format is:

{
   "leadid":LD123,
   "parties":[
      {
         "uid":123123,
         "cust_name":"JOhn Doe",
      }, ...]}

The fastapi code in python is:

class Customer(BaseModel):
    UID: str
    CustName: str

class PackageIn(BaseModel): 
    lead_id: str
    parties: Set[Customer]
    # threshold: Optional[int] = 85

app = FastAPI()

@app.post('/')
async def nm_v2(package:PackageIn):
    return {"resp":"Response"}

When I visit the SwaggerUI to submit the response, the error is "422 Error: Unprocessable Entity". Also, the SwaggerUI doc states

{
  "detail": [
    {
      "loc": [
        "body"
      ],
      "msg": "unhashable type: 'Customer'",
      "type": "type_error"
    }
  ]
}

I do not know how to create this dict() structure for request payload without creating a separate pydantic based class called Customer. Pl tell me how to rectify the error.

Ajeet Mishra
  • 342
  • 2
  • 8
  • 1
    `parties` is supposed to be a `list` of customers. You've defined it as a `set` of customers. Also, the names for the Customer attributes are wrong. `UID` != `uid`, uness case insensitive and `CustName` != `cust_name` unless it's doing translation. And `uid` should be an `int`, not a `str`. – aneroid Sep 03 '20 at 10:51
  • 1
    The problem is with the `Set[Customer]`, normally pydantic model is not hashable so set is not able to check if given item already is in the collection. – Take_Care_ Sep 03 '20 at 10:51
  • Also, the value of leadid doesn't seem to be a string. It may cause problems. I suggest to wrap it into double quotes ", since the request will be in plain JSON, converted into python and then into a pydantic model. Though it may not be the root cause of the problem, just a suggestion – lsabi Sep 03 '20 at 16:11

2 Answers2

16

Pytdantic BaseClass is not hashable. There is a discussion about this feature, i guess it will not be implemented. There is workaround in the discussion, for your case you can try this:

from pydantic import BaseModel
from typing import Set


class MyBaseModel(BaseModel):
    def __hash__(self):  # make hashable BaseModel subclass
        return hash((type(self),) + tuple(self.__dict__.values()))


class Customer(MyBaseModel):  # Use hashable sublclass for your model
    UID: str
    CustName: str


class PackageIn(BaseModel):
    lead_id: str
    parties: Set[Customer]
    # threshold: Optional[int] = 85

data = {
    "lead_id": 'LD123',
    "parties": [
       {
           "UID": 123123,
           "CustName": "JOhn Doe",
       }]}

PackageIn.parse_obj(data) # This part fastapi will make on post request, just for test

> <PackageIn lead_id='LD123' parties={<Customer UID='123123' CustName='JOhn Doe'>}>

4

after https://github.com/pydantic/pydantic/pull/1881 you can add frozen = True to make yor object hashable (note instances will not be allowed to mutate)

class Customer(BaseModel)::
    UID: str
    CustName: str

    class Config:
        frozen = True
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88