Skip to content

Commit 0cc687a

Browse files
authored
fix(event_handler): lazy load Pydantic to improve cold start (#3397)
1 parent 481905e commit 0cc687a

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

aws_lambda_powertools/event_handler/openapi/compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
from pydantic import BaseModel, create_model
1616
from pydantic.fields import FieldInfo
1717

18+
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
1819
from aws_lambda_powertools.event_handler.openapi.types import (
1920
COMPONENT_REF_PREFIX,
20-
PYDANTIC_V2,
2121
ModelNameMap,
2222
UnionType,
2323
)

aws_lambda_powertools/event_handler/openapi/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pydantic import AnyUrl, BaseModel, Field
55

66
from aws_lambda_powertools.event_handler.openapi.compat import model_rebuild
7-
from aws_lambda_powertools.event_handler.openapi.types import PYDANTIC_V2
7+
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
88
from aws_lambda_powertools.shared.types import Annotated, Literal
99

1010
"""

aws_lambda_powertools/event_handler/openapi/params.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
field_annotation_is_scalar,
1616
get_annotation_from_field_info,
1717
)
18-
from aws_lambda_powertools.event_handler.openapi.types import PYDANTIC_V2, CacheKey
18+
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
19+
from aws_lambda_powertools.event_handler.openapi.types import CacheKey
1920
from aws_lambda_powertools.shared.types import Annotated, Literal, get_args, get_origin
2021

2122
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
try:
2+
from pydantic.version import VERSION as PYDANTIC_VERSION
3+
4+
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
5+
except ImportError:
6+
PYDANTIC_V2 = False

aws_lambda_powertools/event_handler/openapi/types.py

-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
COMPONENT_REF_TEMPLATE = "#/components/schemas/{model}"
1717
METHODS_WITH_BODY = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"}
1818

19-
try:
20-
from pydantic.version import VERSION as PYDANTIC_VERSION
21-
22-
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
23-
except ImportError:
24-
PYDANTIC_V2 = False
25-
2619

2720
validation_error_definition = {
2821
"title": "ValidationError",

tests/functional/event_handler/test_bedrock_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any, Dict
33

44
from aws_lambda_powertools.event_handler import BedrockAgentResolver, Response, content_types
5-
from aws_lambda_powertools.event_handler.openapi.types import PYDANTIC_V2
5+
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
66
from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent
77
from tests.functional.utils import load_event
88

0 commit comments

Comments
 (0)