Skip to content

Commit 41b6fee

Browse files
authored
perf: cache TypeAdapters (#1114)
1 parent e8e5a0d commit 41b6fee

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/openai/_models.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, cast
55
from datetime import date, datetime
6+
from functools import lru_cache
67
from typing_extensions import (
78
Unpack,
89
Literal,
@@ -533,7 +534,12 @@ class GenericModel(BaseGenericModel, BaseModel):
533534

534535

535536
if PYDANTIC_V2:
536-
from pydantic import TypeAdapter
537+
538+
if TYPE_CHECKING:
539+
from pydantic import TypeAdapter
540+
else:
541+
from pydantic import TypeAdapter as _TypeAdapter
542+
TypeAdapter = lru_cache(_TypeAdapter)
537543

538544
def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
539545
return TypeAdapter(type_).validate_python(value)

0 commit comments

Comments
 (0)