Skip to content

Commit 8086d61

Browse files
fix(perf): optimize some hot paths
1 parent 284415c commit 8086d61

File tree

8 files changed

+59
-15
lines changed

8 files changed

+59
-15
lines changed

src/openai/_utils/_transform.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import pathlib
66
from typing import Any, Mapping, TypeVar, cast
77
from datetime import date, datetime
8-
from typing_extensions import Literal, get_args, override, get_type_hints
8+
from typing_extensions import Literal, get_args, override, get_type_hints as _get_type_hints
99

1010
import anyio
1111
import pydantic
1212

1313
from ._utils import (
1414
is_list,
1515
is_given,
16+
lru_cache,
1617
is_mapping,
1718
is_iterable,
1819
)
@@ -109,6 +110,7 @@ class Params(TypedDict, total=False):
109110
return cast(_T, transformed)
110111

111112

113+
@lru_cache(maxsize=8096)
112114
def _get_annotated_type(type_: type) -> type | None:
113115
"""If the given type is an `Annotated` type then it is returned, if not `None` is returned.
114116
@@ -433,3 +435,13 @@ async def _async_transform_typeddict(
433435
else:
434436
result[_maybe_transform_key(key, type_)] = await _async_transform_recursive(value, annotation=type_)
435437
return result
438+
439+
440+
@lru_cache(maxsize=8096)
441+
def get_type_hints(
442+
obj: Any,
443+
globalns: dict[str, Any] | None = None,
444+
localns: Mapping[str, Any] | None = None,
445+
include_extras: bool = False,
446+
) -> dict[str, Any]:
447+
return _get_type_hints(obj, globalns=globalns, localns=localns, include_extras=include_extras)

src/openai/_utils/_typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
get_origin,
1414
)
1515

16+
from ._utils import lru_cache
1617
from .._types import InheritsGeneric
1718
from .._compat import is_union as _is_union
1819

@@ -66,6 +67,7 @@ def is_type_alias_type(tp: Any, /) -> TypeIs[typing_extensions.TypeAliasType]:
6667

6768

6869
# Extracts T from Annotated[T, ...] or from Required[Annotated[T, ...]]
70+
@lru_cache(maxsize=8096)
6971
def strip_annotated_type(typ: type) -> type:
7072
if is_required_type(typ) or is_annotated_type(typ):
7173
return strip_annotated_type(cast(type, get_args(typ)[0]))

src/openai/resources/audio/transcriptions.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,12 @@ def create(
321321
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
322322
return self._post( # type: ignore[return-value]
323323
"/audio/transcriptions",
324-
body=maybe_transform(body, transcription_create_params.TranscriptionCreateParams),
324+
body=maybe_transform(
325+
body,
326+
transcription_create_params.TranscriptionCreateParamsStreaming
327+
if stream
328+
else transcription_create_params.TranscriptionCreateParamsNonStreaming,
329+
),
325330
files=files,
326331
options=make_request_options(
327332
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
@@ -616,7 +621,12 @@ async def create(
616621
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
617622
return await self._post(
618623
"/audio/transcriptions",
619-
body=await async_maybe_transform(body, transcription_create_params.TranscriptionCreateParams),
624+
body=await async_maybe_transform(
625+
body,
626+
transcription_create_params.TranscriptionCreateParamsStreaming
627+
if stream
628+
else transcription_create_params.TranscriptionCreateParamsNonStreaming,
629+
),
620630
files=files,
621631
options=make_request_options(
622632
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout

src/openai/resources/beta/threads/runs/runs.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def create(
587587
"top_p": top_p,
588588
"truncation_strategy": truncation_strategy,
589589
},
590-
run_create_params.RunCreateParams,
590+
run_create_params.RunCreateParamsStreaming if stream else run_create_params.RunCreateParamsNonStreaming,
591591
),
592592
options=make_request_options(
593593
extra_headers=extra_headers,
@@ -1324,7 +1324,9 @@ def submit_tool_outputs(
13241324
"tool_outputs": tool_outputs,
13251325
"stream": stream,
13261326
},
1327-
run_submit_tool_outputs_params.RunSubmitToolOutputsParams,
1327+
run_submit_tool_outputs_params.RunSubmitToolOutputsParamsStreaming
1328+
if stream
1329+
else run_submit_tool_outputs_params.RunSubmitToolOutputsParamsNonStreaming,
13281330
),
13291331
options=make_request_options(
13301332
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
@@ -1996,7 +1998,7 @@ async def create(
19961998
"top_p": top_p,
19971999
"truncation_strategy": truncation_strategy,
19982000
},
1999-
run_create_params.RunCreateParams,
2001+
run_create_params.RunCreateParamsStreaming if stream else run_create_params.RunCreateParamsNonStreaming,
20002002
),
20012003
options=make_request_options(
20022004
extra_headers=extra_headers,
@@ -2732,7 +2734,9 @@ async def submit_tool_outputs(
27322734
"tool_outputs": tool_outputs,
27332735
"stream": stream,
27342736
},
2735-
run_submit_tool_outputs_params.RunSubmitToolOutputsParams,
2737+
run_submit_tool_outputs_params.RunSubmitToolOutputsParamsStreaming
2738+
if stream
2739+
else run_submit_tool_outputs_params.RunSubmitToolOutputsParamsNonStreaming,
27362740
),
27372741
options=make_request_options(
27382742
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout

src/openai/resources/beta/threads/threads.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,9 @@ def create_and_run(
717717
"top_p": top_p,
718718
"truncation_strategy": truncation_strategy,
719719
},
720-
thread_create_and_run_params.ThreadCreateAndRunParams,
720+
thread_create_and_run_params.ThreadCreateAndRunParamsStreaming
721+
if stream
722+
else thread_create_and_run_params.ThreadCreateAndRunParamsNonStreaming,
721723
),
722724
options=make_request_options(
723725
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
@@ -1564,7 +1566,9 @@ async def create_and_run(
15641566
"top_p": top_p,
15651567
"truncation_strategy": truncation_strategy,
15661568
},
1567-
thread_create_and_run_params.ThreadCreateAndRunParams,
1569+
thread_create_and_run_params.ThreadCreateAndRunParamsStreaming
1570+
if stream
1571+
else thread_create_and_run_params.ThreadCreateAndRunParamsNonStreaming,
15681572
),
15691573
options=make_request_options(
15701574
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout

src/openai/resources/chat/completions/completions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,9 @@ def create(
947947
"user": user,
948948
"web_search_options": web_search_options,
949949
},
950-
completion_create_params.CompletionCreateParams,
950+
completion_create_params.CompletionCreateParamsStreaming
951+
if stream
952+
else completion_create_params.CompletionCreateParamsNonStreaming,
951953
),
952954
options=make_request_options(
953955
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
@@ -2033,7 +2035,9 @@ async def create(
20332035
"user": user,
20342036
"web_search_options": web_search_options,
20352037
},
2036-
completion_create_params.CompletionCreateParams,
2038+
completion_create_params.CompletionCreateParamsStreaming
2039+
if stream
2040+
else completion_create_params.CompletionCreateParamsNonStreaming,
20372041
),
20382042
options=make_request_options(
20392043
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout

src/openai/resources/completions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ def create(
559559
"top_p": top_p,
560560
"user": user,
561561
},
562-
completion_create_params.CompletionCreateParams,
562+
completion_create_params.CompletionCreateParamsStreaming
563+
if stream
564+
else completion_create_params.CompletionCreateParamsNonStreaming,
563565
),
564566
options=make_request_options(
565567
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
@@ -1101,7 +1103,9 @@ async def create(
11011103
"top_p": top_p,
11021104
"user": user,
11031105
},
1104-
completion_create_params.CompletionCreateParams,
1106+
completion_create_params.CompletionCreateParamsStreaming
1107+
if stream
1108+
else completion_create_params.CompletionCreateParamsNonStreaming,
11051109
),
11061110
options=make_request_options(
11071111
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout

src/openai/resources/responses/responses.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,9 @@ def create(
623623
"truncation": truncation,
624624
"user": user,
625625
},
626-
response_create_params.ResponseCreateParams,
626+
response_create_params.ResponseCreateParamsStreaming
627+
if stream
628+
else response_create_params.ResponseCreateParamsNonStreaming,
627629
),
628630
options=make_request_options(
629631
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
@@ -1435,7 +1437,9 @@ async def create(
14351437
"truncation": truncation,
14361438
"user": user,
14371439
},
1438-
response_create_params.ResponseCreateParams,
1440+
response_create_params.ResponseCreateParamsStreaming
1441+
if stream
1442+
else response_create_params.ResponseCreateParamsNonStreaming,
14391443
),
14401444
options=make_request_options(
14411445
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout

0 commit comments

Comments
 (0)