diff --git a/src/deepgram/core/pydantic_utilities.py b/src/deepgram/core/pydantic_utilities.py index 8906cdfa..09ff2778 100644 --- a/src/deepgram/core/pydantic_utilities.py +++ b/src/deepgram/core/pydantic_utilities.py @@ -2,11 +2,14 @@ # nopycln: file import datetime as dt +import functools from collections import defaultdict from typing import Any, Callable, ClassVar, Dict, List, Mapping, Optional, Set, Tuple, Type, TypeVar, Union, cast import pydantic +from deepgram.core.serialization import convert_and_respect_annotation_metadata + IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.") if IS_PYDANTIC_V2: @@ -39,7 +42,7 @@ def parse_obj_as(type_: Type[T], object_: Any) -> T: dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read") if IS_PYDANTIC_V2: - adapter = pydantic.TypeAdapter(type_) # type: ignore[attr-defined] + adapter = _get_adapter(type_) return adapter.validate_python(dealiased_object) return pydantic.parse_obj_as(type_, dealiased_object) @@ -256,3 +259,8 @@ def _get_field_default(field: PydanticField) -> Any: return None return value return value + + +@functools.lru_cache(maxsize=32) +def _get_adapter(type_: Type[Any]) -> Any: + return pydantic.TypeAdapter(type_) # type: ignore[attr-defined]