Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/deepgram/core/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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_type_adapter(type_)
return adapter.validate_python(dealiased_object)
return pydantic.parse_obj_as(type_, dealiased_object)

Expand Down Expand Up @@ -256,3 +259,9 @@ def _get_field_default(field: PydanticField) -> Any:
return None
return value
return value


# Cache TypeAdapter creation for improved performance
@functools.lru_cache(maxsize=32)
def _get_type_adapter(type_: Type[Any]) -> Any:
return pydantic.TypeAdapter(type_) # type: ignore[attr-defined]