Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ class SPANDATA:
Example: [{"role": "assistant", "parts": [{"type": "text", "content": "The weather in Paris is currently rainy with a temperature of 57°F."}], "finish_reason": "stop"}]
"""

GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK = "gen_ai.response.time_to_first_chunk"
"""
Time in seconds when the first response content chunk arrived in streaming responses.
Example: 0.6853435
"""

GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN = "gen_ai.response.time_to_first_token"
"""
The time it took to receive the first token from the model.
Expand Down
8 changes: 4 additions & 4 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def _wrap_synchronous_completions_chunk_iterator(
with capture_internal_exceptions():
if ttft is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
)
all_responses = None
if len(data_buf) > 0:
Expand Down Expand Up @@ -1046,7 +1046,7 @@ async def _wrap_asynchronous_completions_chunk_iterator(
with capture_internal_exceptions():
if ttft is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
)
all_responses = None
if len(data_buf) > 0:
Expand Down Expand Up @@ -1117,7 +1117,7 @@ def _wrap_synchronous_responses_event_iterator(
with capture_internal_exceptions():
if ttft is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
)
if len(data_buf) > 0:
all_responses = ["".join(chunk) for chunk in data_buf]
Expand Down Expand Up @@ -1187,7 +1187,7 @@ async def _wrap_asynchronous_responses_event_iterator(
with capture_internal_exceptions():
if ttft is not None:
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
)
if len(data_buf) > 0:
all_responses = ["".join(chunk) for chunk in data_buf]
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/openai_agents/patches/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async def wrapped_stream_response(*args: "Any", **kwargs: "Any") -> "Any":
if not ttft_recorded and hasattr(event, "delta"):
ttft = time.perf_counter() - start_time
span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
)
ttft_recorded = True

Expand Down
18 changes: 9 additions & 9 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4991,7 +4991,7 @@ async def test_ai_client_span_streaming_responses_async_api(
"gen_ai.response.model": "response-model-id",
"gen_ai.response.streaming": True,
SPANDATA.GEN_AI_PROVIDER_NAME: "openai",
"gen_ai.response.time_to_first_token": mock.ANY,
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK: mock.ANY,
"gen_ai.usage.input_tokens": 20,
"gen_ai.usage.input_tokens.cached": 5,
"gen_ai.usage.output_tokens": 10,
Expand Down Expand Up @@ -5481,8 +5481,8 @@ def test_streaming_chat_completion_ttft(
assert span["attributes"]["sentry.op"] == "gen_ai.chat"

# Verify TTFT is captured
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN]
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]

assert isinstance(ttft, float)
assert ttft > 0
Expand Down Expand Up @@ -5564,8 +5564,8 @@ async def test_streaming_chat_completion_ttft_async(
assert span["attributes"]["sentry.op"] == "gen_ai.chat"

# Verify TTFT is captured
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN]
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]

assert isinstance(ttft, float)
assert ttft > 0
Expand Down Expand Up @@ -5614,8 +5614,8 @@ def test_streaming_responses_api_ttft(
assert span["attributes"]["sentry.op"] == "gen_ai.responses"

# Verify TTFT is captured
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN]
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]

assert isinstance(ttft, float)
assert ttft > 0
Expand Down Expand Up @@ -5666,8 +5666,8 @@ async def test_streaming_responses_api_ttft_async(
assert span["attributes"]["sentry.op"] == "gen_ai.responses"

# Verify TTFT is captured
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN]
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"]
ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]

assert isinstance(ttft, float)
assert ttft > 0
2 changes: 1 addition & 1 deletion tests/integrations/openai_agents/test_openai_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4107,7 +4107,7 @@ async def test_streaming_ttft_on_chat_span(
assert len(chat_spans) >= 1
chat_span = chat_spans[0]

assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN in chat_span["attributes"]
assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in chat_span["attributes"]
assert chat_span["attributes"].get(SPANDATA.GEN_AI_RESPONSE_STREAMING) is True


Expand Down
Loading