-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: cache TypeAdapters #1114
perf: cache TypeAdapters #1114
Conversation
Sorry I'm late. Ci should be working now. Reference about lru_cache and pyright: |
src/openai/_models.py
Outdated
from pydantic import TypeAdapter as PyTypeAdapter | ||
|
||
def TypeAdapter(type_: type[_T]) -> PyTypeAdapter[_T]: | ||
return PyTypeAdapter(type_) | ||
|
||
if not TYPE_CHECKING: | ||
TypeAdapter = lru_cache(TypeAdapter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think this conveys the intent of this code more clearly (and you can add the lru_cache
import at the top of the file), thoughts?
from pydantic import TypeAdapter as PyTypeAdapter | |
def TypeAdapter(type_: type[_T]) -> PyTypeAdapter[_T]: | |
return PyTypeAdapter(type_) | |
if not TYPE_CHECKING: | |
TypeAdapter = lru_cache(TypeAdapter) | |
if TYPE_CHECKING: | |
from pydantic import TypeAdapter | |
else: | |
from pydantic import TypeAdapter as _TypeAdapter | |
TypeAdapter = lru_cache(TypeAdapter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, It has been modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this and sorry for the delayed review!
* perf: cache TypeAdapters (openai#1114) * perf: cache TypeAdapters (openai#1243) * docs: fix typo in CONTRIBUTING.md (openai#1245) * chore(internal): update generated pragma comment (openai#1247) * docs: assistant improvements (openai#1249) * release: 1.14.2 --------- Co-authored-by: vvanglro <[email protected]>
* perf: cache TypeAdapters (openai#1114) * perf: cache TypeAdapters (openai#1243) * docs: fix typo in CONTRIBUTING.md (openai#1245) * chore(internal): update generated pragma comment (openai#1247) * docs: assistant improvements (openai#1249) * release: 1.14.2 --------- Co-authored-by: vvanglro <[email protected]>
Simple test example:
output:
Refer to: https://docs.pydantic.dev/latest/concepts/performance/#typeadapter-instantiated-once