Skip to content

Commit a311e3b

Browse files
authored
Generalize NOT_GIVEN check with omit for openai (#4926)
### Description openai uses `Omit` now instead of `NotGiven` openai/openai-python@8260288 #### Issues * resolves: #4923 * resolves: PY-1885
1 parent 749e409 commit a311e3b

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from functools import wraps
2+
from collections.abc import Iterable
23

34
import sentry_sdk
45
from sentry_sdk import consts
@@ -17,14 +18,19 @@
1718
from typing import TYPE_CHECKING
1819

1920
if TYPE_CHECKING:
20-
from typing import Any, Iterable, List, Optional, Callable, AsyncIterator, Iterator
21+
from typing import Any, List, Optional, Callable, AsyncIterator, Iterator
2122
from sentry_sdk.tracing import Span
2223

2324
try:
2425
try:
25-
from openai import NOT_GIVEN
26+
from openai import NotGiven
2627
except ImportError:
27-
NOT_GIVEN = None
28+
NotGiven = None
29+
30+
try:
31+
from openai import Omit
32+
except ImportError:
33+
Omit = None
2834

2935
from openai.resources.chat.completions import Completions, AsyncCompletions
3036
from openai.resources import Embeddings, AsyncEmbeddings
@@ -204,12 +210,12 @@ def _set_input_data(span, kwargs, operation, integration):
204210
for key, attribute in kwargs_keys_to_attributes.items():
205211
value = kwargs.get(key)
206212

207-
if value is not NOT_GIVEN and value is not None:
213+
if value is not None and _is_given(value):
208214
set_data_normalized(span, attribute, value)
209215

210216
# Input attributes: Tools
211217
tools = kwargs.get("tools")
212-
if tools is not NOT_GIVEN and tools is not None and len(tools) > 0:
218+
if tools is not None and _is_given(tools) and len(tools) > 0:
213219
set_data_normalized(
214220
span, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools)
215221
)
@@ -689,3 +695,15 @@ async def _sentry_patched_responses_async(*args, **kwargs):
689695
return await _execute_async(f, *args, **kwargs)
690696

691697
return _sentry_patched_responses_async
698+
699+
700+
def _is_given(obj):
701+
# type: (Any) -> bool
702+
"""
703+
Check for givenness safely across different openai versions.
704+
"""
705+
if NotGiven is not None and isinstance(obj, NotGiven):
706+
return False
707+
if Omit is not None and isinstance(obj, Omit):
708+
return False
709+
return True

tests/integrations/openai/test_openai.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
except ImportError:
88
NOT_GIVEN = None
99

10+
try:
11+
from openai import omit
12+
except ImportError:
13+
omit = None
14+
1015
from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
1116
from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
1217
from openai.types.chat import ChatCompletion, ChatCompletionMessage, ChatCompletionChunk
@@ -1424,7 +1429,7 @@ async def test_streaming_responses_api_async(
14241429
)
14251430
@pytest.mark.parametrize(
14261431
"tools",
1427-
[[], None, NOT_GIVEN],
1432+
[[], None, NOT_GIVEN, omit],
14281433
)
14291434
def test_empty_tools_in_chat_completion(sentry_init, capture_events, tools):
14301435
sentry_init(

0 commit comments

Comments
 (0)