Commit 284415c9

stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
2025-04-11 20:22:53
fix(perf): skip traversing types for NotGiven values
1 parent c8833fc
Changed files (2)
src
openai
tests
src/openai/_utils/_transform.py
@@ -12,6 +12,7 @@ import pydantic
 
 from ._utils import (
     is_list,
+    is_given,
     is_mapping,
     is_iterable,
 )
@@ -258,6 +259,11 @@ def _transform_typeddict(
     result: dict[str, object] = {}
     annotations = get_type_hints(expected_type, include_extras=True)
     for key, value in data.items():
+        if not is_given(value):
+            # we don't need to include `NotGiven` values here as they'll
+            # be stripped out before the request is sent anyway
+            continue
+
         type_ = annotations.get(key)
         if type_ is None:
             # we do not have a type annotation for this field, leave it as is
@@ -415,6 +421,11 @@ async def _async_transform_typeddict(
     result: dict[str, object] = {}
     annotations = get_type_hints(expected_type, include_extras=True)
     for key, value in data.items():
+        if not is_given(value):
+            # we don't need to include `NotGiven` values here as they'll
+            # be stripped out before the request is sent anyway
+            continue
+
         type_ = annotations.get(key)
         if type_ is None:
             # we do not have a type annotation for this field, leave it as is
tests/test_transform.py
@@ -8,7 +8,7 @@ from typing_extensions import Required, Annotated, TypedDict
 
 import pytest
 
-from openai._types import Base64FileInput
+from openai._types import NOT_GIVEN, Base64FileInput
 from openai._utils import (
     PropertyInfo,
     transform as _transform,
@@ -444,3 +444,10 @@ async def test_transform_skipping(use_async: bool) -> None:
     # iterables of ints are converted to a list
     data = iter([1, 2, 3])
     assert await transform(data, Iterable[int], use_async) == [1, 2, 3]
+
+
+@parametrize
+@pytest.mark.asyncio
+async def test_strips_notgiven(use_async: bool) -> None:
+    assert await transform({"foo_bar": "bar"}, Foo1, use_async) == {"fooBar": "bar"}
+    assert await transform({"foo_bar": NOT_GIVEN}, Foo1, use_async) == {}