bipster
bipster

Reputation: 474

"FastAPIError: Invalid args for response field! Hint: check that <class 'typing._UnionGenericAlias'> is a valid pydantic field type"

I have the following Pydantic schema for a FastAPI app.

In the following schema, whenever I have ParameterSchema as the schema validator for params, it gives me the following error:

fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'typing._GenericAlias'> is a valid pydantic field type

I have no idea what's going on!

class ParameterSchema(BaseModel):
    expiryDate = Optional[datetime]

    class Config:
        arbitrary_types_allowed = True


class RequestProvisioningEventData(BaseModel):
    some_attribute: List[str]
    other_attribute: Optional[List[str]] = []
    bool_attribute: bool
    params: ParameterSchema

    class Config:
        use_enum_values = True

Upvotes: 6

Views: 9169

Answers (1)

Gino Mempin
Gino Mempin

Reputation: 29710

This is because expiryDate was assigned (=) a value

class ParameterSchema(BaseModel):
    expiryDate = Optional[datetime]

It should be using a type hint (:):

class ParameterSchema(BaseModel):
    expiryDate: Optional[datetime]

Pay attention to use colons : for type hints, not equals =.

Upvotes: 7

Related Questions