Skip to content

Commit 493ac4b

Browse files
authored
Better checking for empty tools list (#4647)
Fixes #4646
1 parent 70e2b59 commit 493ac4b

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
from sentry_sdk.tracing import Span
2121

2222
try:
23+
try:
24+
from openai import NOT_GIVEN
25+
except ImportError:
26+
NOT_GIVEN = None
27+
2328
from openai.resources.chat.completions import Completions, AsyncCompletions
2429
from openai.resources import Embeddings, AsyncEmbeddings
2530

@@ -192,12 +197,13 @@ def _set_input_data(span, kwargs, operation, integration):
192197
}
193198
for key, attribute in kwargs_keys_to_attributes.items():
194199
value = kwargs.get(key)
195-
if value is not None:
200+
201+
if value is not NOT_GIVEN and value is not None:
196202
set_data_normalized(span, attribute, value)
197203

198204
# Input attributes: Tools
199205
tools = kwargs.get("tools")
200-
if tools is not None and len(tools) > 0:
206+
if tools is not NOT_GIVEN and tools is not None and len(tools) > 0:
201207
set_data_normalized(
202208
span, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools)
203209
)

tests/integrations/openai/test_openai.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import pytest
2+
3+
from sentry_sdk.utils import package_version
4+
5+
try:
6+
from openai import NOT_GIVEN
7+
except ImportError:
8+
NOT_GIVEN = None
9+
210
from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
311
from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
412
from openai.types.chat import ChatCompletion, ChatCompletionMessage, ChatCompletionChunk
@@ -43,6 +51,7 @@ async def __call__(self, *args, **kwargs):
4351
return super(AsyncMock, self).__call__(*args, **kwargs)
4452

4553

54+
OPENAI_VERSION = package_version("openai")
4655
EXAMPLE_CHAT_COMPLETION = ChatCompletion(
4756
id="chat-id",
4857
choices=[
@@ -1387,3 +1396,34 @@ async def test_streaming_responses_api_async(
13871396
assert span["data"]["gen_ai.usage.input_tokens"] == 20
13881397
assert span["data"]["gen_ai.usage.output_tokens"] == 10
13891398
assert span["data"]["gen_ai.usage.total_tokens"] == 30
1399+
1400+
1401+
@pytest.mark.skipif(
1402+
OPENAI_VERSION <= (1, 1, 0),
1403+
reason="OpenAI versions <=1.1.0 do not support the tools parameter.",
1404+
)
1405+
@pytest.mark.parametrize(
1406+
"tools",
1407+
[[], None, NOT_GIVEN],
1408+
)
1409+
def test_empty_tools_in_chat_completion(sentry_init, capture_events, tools):
1410+
sentry_init(
1411+
integrations=[OpenAIIntegration()],
1412+
traces_sample_rate=1.0,
1413+
)
1414+
events = capture_events()
1415+
1416+
client = OpenAI(api_key="z")
1417+
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
1418+
1419+
with start_transaction(name="openai tx"):
1420+
client.chat.completions.create(
1421+
model="some-model",
1422+
messages=[{"role": "system", "content": "hello"}],
1423+
tools=tools,
1424+
)
1425+
1426+
(event,) = events
1427+
span = event["spans"][0]
1428+
1429+
assert "gen_ai.request.available_tools" not in span["data"]

0 commit comments

Comments
 (0)