|
1 | 1 | from functools import wraps |
| 2 | +from collections.abc import Iterable |
2 | 3 |
|
3 | 4 | import sentry_sdk |
4 | 5 | from sentry_sdk import consts |
|
17 | 18 | from typing import TYPE_CHECKING |
18 | 19 |
|
19 | 20 | if TYPE_CHECKING: |
20 | | - from typing import Any, Iterable, List, Optional, Callable, AsyncIterator, Iterator |
| 21 | + from typing import Any, List, Optional, Callable, AsyncIterator, Iterator |
21 | 22 | from sentry_sdk.tracing import Span |
22 | 23 |
|
23 | 24 | try: |
24 | 25 | try: |
25 | | - from openai import NOT_GIVEN |
| 26 | + from openai import NotGiven |
26 | 27 | except ImportError: |
27 | | - NOT_GIVEN = None |
| 28 | + NotGiven = None |
| 29 | + |
| 30 | + try: |
| 31 | + from openai import Omit |
| 32 | + except ImportError: |
| 33 | + Omit = None |
28 | 34 |
|
29 | 35 | from openai.resources.chat.completions import Completions, AsyncCompletions |
30 | 36 | from openai.resources import Embeddings, AsyncEmbeddings |
@@ -204,12 +210,12 @@ def _set_input_data(span, kwargs, operation, integration): |
204 | 210 | for key, attribute in kwargs_keys_to_attributes.items(): |
205 | 211 | value = kwargs.get(key) |
206 | 212 |
|
207 | | - if value is not NOT_GIVEN and value is not None: |
| 213 | + if value is not None and _is_given(value): |
208 | 214 | set_data_normalized(span, attribute, value) |
209 | 215 |
|
210 | 216 | # Input attributes: Tools |
211 | 217 | 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: |
213 | 219 | set_data_normalized( |
214 | 220 | span, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools) |
215 | 221 | ) |
@@ -689,3 +695,15 @@ async def _sentry_patched_responses_async(*args, **kwargs): |
689 | 695 | return await _execute_async(f, *args, **kwargs) |
690 | 696 |
|
691 | 697 | 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 |
0 commit comments