Skip to content

Commit

Permalink
perf: cache TypeAdapters (#1114)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvanglro committed Mar 18, 2024
1 parent e8e5a0d commit 41b6fee
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, cast
from datetime import date, datetime
from functools import lru_cache
from typing_extensions import (
Unpack,
Literal,
Expand Down Expand Up @@ -533,7 +534,12 @@ class GenericModel(BaseGenericModel, BaseModel):


if PYDANTIC_V2:
from pydantic import TypeAdapter

if TYPE_CHECKING:
from pydantic import TypeAdapter
else:
from pydantic import TypeAdapter as _TypeAdapter
TypeAdapter = lru_cache(_TypeAdapter)

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

0 comments on commit 41b6fee

Please sign in to comment.