[Python] Unhashable type in FastAPI request

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página