I'm working on an python AWS Lambda function that utilises pydantic to perform input validation. I have recently found that Lambda times out (with a timeout of 15 seconds) when executing the following code:
def _stringify(v):
return str(v)
class SomeModel(BaseModel):
a: int
_stringify = validator("a", allow_reuse=True)(_stringify)
SomeModel(a=12)
I have identified that the issue occurs when calling _stringify = validator("a", allow_reuse=True)(_stringify) which passes the _stringify function to the validator decorator. This is fully functional in a local environment but not within AWS Lambda. The following alternative definition of 'SomeModel' also works within the AWS Lambda environment.
class SomeModel(BaseModel):
a: int
@validator("a")
def stringify(cls, v):
return str(v)
Does anyone with a better understanding of AWS Lambda have any ideas as to why _stringify = validator("a", allow_reuse=True)(_stringify) results in a timeout and can you suggest any possible fixes?
(Note: The alternative definition of SomeModel is undesirable as it violates DRY principles as we want to use _stringify in multiple models.)
The code raises no exceptions when running both locally and in the Lambda environment.
Environment:
AWS Lambda
python 3.8 - Custom runtime build through docker. Lambda layers supporting:
- pydantic - v1.6.1 (python 3.8)