-
Notifications
You must be signed in to change notification settings - Fork 442
Closed
Labels
bugSomething isn't workingSomething isn't workingparserParser (Pydantic) utilityParser (Pydantic) utilityv3Features that will be included in Powertools v3.Features that will be included in Powertools v3.
Description
Expected Behaviour
When any validation in a Pydantic model fails during a call to Powertools parse()
function, I expect parse()
to raise ValidationError
. This is what happened prior to updating to version 3 of Powertools and is reflected in this example from Powertools documentation: https://docs.powertools.aws.dev/lambda/python/latest/utilities/parser/#parse-function
Current Behaviour
When the passed in object fails model validation in parse()
, it is raising InvalidModelTypeError
.
Code snippet
# Again, taken from the docs
from aws_lambda_powertools.utilities.parser import parse, BaseModel, ValidationError
from typing import List, Optional
class OrderItem(BaseModel):
id: int
quantity: int
description: str
class Order(BaseModel):
id: int
description: str
items: List[OrderItem] # nesting models are supported
optional_field: Optional[str] = None # this field may or may not be available when parsing
payload = {
"id": 10876546789,
"description": "My order",
"items": [
{
# this will cause a validation error
"id": [1015938732],
"quantity": 1,
"description": "item xpto"
}
]
}
def my_function():
try:
parsed_payload: Order = parse(event=payload, model=Order)
# payload dict is now parsed into our model
return parsed_payload.items
except ValidationError:
return {
"status_code": 400,
"message": "Invalid order"
}
Possible Solution
No response
Steps to Reproduce
I am just running this locally at the moment. I don't know if the Lambda Layer might somehow behavior differently since.
Powertools for AWS Lambda (Python) version
3.0.2.dev1
AWS Lambda function runtime
3.12
Packaging format used
PyPi
Debugging logs
/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/aws_lambda_powertools/package_logger.py:20: UserWarning: POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG.
if powertools_debug_is_set():
2024-09-26 16:07:09,201 aws_lambda_powertools.utilities.parser.parser [DEBUG] Parsing and validating event model; no envelope used
Traceback (most recent call last):
File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/aws_lambda_powertools/utilities/parser/parser.py", line 195, in parse
return adapter.validate_python(event)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/pydantic/type_adapter.py", line 135, in wrapped
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/pydantic/type_adapter.py", line 366, in validate_python
return self.validator.validate_python(object, strict=strict, from_attributes=from_attributes, context=context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Order
items.0.id
Input should be a valid integer [type=int_type, input_value=[1015938732], input_type=list]
For further information visit https://errors.pydantic.dev/2.9/v/int_type
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/john/sandbox/sftp-processor/sftp-request-processor/sftp_request_model.py", line 260, in <module>
parsed_payload: Order = parse(event=payload, model=Order)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/aws_lambda_powertools/utilities/parser/parser.py", line 203, in parse
raise InvalidModelTypeError(
aws_lambda_powertools.utilities.parser.exceptions.InvalidModelTypeError: Error: 1 validation error for Order
items.0.id
Input should be a valid integer [type=int_type, input_value=[1015938732], input_type=list]
For further information visit https://errors.pydantic.dev/2.9/v/int_type. Please ensure the Input model inherits from BaseModel,
and your payload adheres to the specified Input model structure.
Model=<class '__main__.Order'>
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingparserParser (Pydantic) utilityParser (Pydantic) utilityv3Features that will be included in Powertools v3.Features that will be included in Powertools v3.
Type
Projects
Status
Shipped