Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sentry_sdk.tracing import Span

try:
import openai
from openai.resources.chat.completions import Completions, AsyncCompletions
from openai.resources import Embeddings, AsyncEmbeddings

Expand Down Expand Up @@ -192,12 +193,12 @@ def _set_input_data(span, kwargs, operation, integration):
}
for key, attribute in kwargs_keys_to_attributes.items():
value = kwargs.get(key)
if value is not None:
if value is not openai.NOT_GIVEN and value is not None:
set_data_normalized(span, attribute, value)

# Input attributes: Tools
tools = kwargs.get("tools")
if tools is not None and len(tools) > 0:
if tools is not openai.NOT_GIVEN and tools is not None and len(tools) > 0:
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools)
)
Expand Down
28 changes: 28 additions & 0 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import openai
from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
from openai.types.chat import ChatCompletion, ChatCompletionMessage, ChatCompletionChunk
Expand Down Expand Up @@ -1387,3 +1388,30 @@ async def test_streaming_responses_api_async(
assert span["data"]["gen_ai.usage.input_tokens"] == 20
assert span["data"]["gen_ai.usage.output_tokens"] == 10
assert span["data"]["gen_ai.usage.total_tokens"] == 30


@pytest.mark.parametrize(
"tools",
[[], None, openai.NOT_GIVEN],
)
def test_empty_tools_in_chat_completion(sentry_init, capture_events, tools):
sentry_init(
integrations=[OpenAIIntegration()],
traces_sample_rate=1.0,
)
events = capture_events()

client = OpenAI(api_key="z")
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)

with start_transaction(name="openai tx"):
client.chat.completions.create(
model="some-model",
messages=[{"role": "system", "content": "hello"}],
tools=tools,
)

(event,) = events
span = event["spans"][0]

assert "gen_ai.request.available_tools" not in span["data"]
Loading