|
1 | 1 | 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 | + |
2 | 10 | from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
|
3 | 11 | from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
|
4 | 12 | from openai.types.chat import ChatCompletion, ChatCompletionMessage, ChatCompletionChunk
|
@@ -43,6 +51,7 @@ async def __call__(self, *args, **kwargs):
|
43 | 51 | return super(AsyncMock, self).__call__(*args, **kwargs)
|
44 | 52 |
|
45 | 53 |
|
| 54 | +OPENAI_VERSION = package_version("openai") |
46 | 55 | EXAMPLE_CHAT_COMPLETION = ChatCompletion(
|
47 | 56 | id="chat-id",
|
48 | 57 | choices=[
|
@@ -1387,3 +1396,34 @@ async def test_streaming_responses_api_async(
|
1387 | 1396 | assert span["data"]["gen_ai.usage.input_tokens"] == 20
|
1388 | 1397 | assert span["data"]["gen_ai.usage.output_tokens"] == 10
|
1389 | 1398 | 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