3030)
3131
3232
33- def _get_ttft_metric (metric_reader ):
33+ def _get_ttft_data_points (metric_reader ):
34+ results = []
3435 metrics = metric_reader .get_metrics_data ().resource_metrics
3536 if not metrics :
36- return None
37+ return results
3738 for scope_metrics in metrics [0 ].scope_metrics :
3839 for m in scope_metrics .metrics :
3940 if m .name == gen_ai_metrics .GEN_AI_SERVER_TIME_TO_FIRST_TOKEN :
40- return m
41- return None
41+ results . extend ( m . data . data_points )
42+ return results
4243
4344
4445def test_streaming_chat_records_ttft_metric (
@@ -55,12 +56,12 @@ def test_streaming_chat_records_ttft_metric(
5556 for _ in response :
5657 pass
5758
58- ttft_metric = _get_ttft_metric (metric_reader )
59- assert ttft_metric is not None , (
60- "gen_ai.server.time_to_first_token metric should be recorded for streaming"
59+ data_points = _get_ttft_data_points (metric_reader )
60+ assert len ( data_points ) == 1 , (
61+ "expected exactly one TTFT data point for streaming"
6162 )
6263
63- data_point = ttft_metric . data . data_points [0 ]
64+ data_point = data_points [0 ]
6465 assert data_point .sum >= 0
6566 assert data_point .count == 1
6667 assert data_point .explicit_bounds == _TTFT_BUCKETS
@@ -94,12 +95,12 @@ async def test_async_streaming_chat_records_ttft_metric(
9495 async for _ in response :
9596 pass
9697
97- ttft_metric = _get_ttft_metric (metric_reader )
98- assert ttft_metric is not None , (
99- "gen_ai.server.time_to_first_token metric should be recorded for async streaming"
98+ data_points = _get_ttft_data_points (metric_reader )
99+ assert len ( data_points ) == 1 , (
100+ "expected exactly one TTFT data point for async streaming"
100101 )
101102
102- data_point = ttft_metric . data . data_points [0 ]
103+ data_point = data_points [0 ]
103104 assert data_point .sum >= 0
104105 assert data_point .count == 1
105106 assert data_point .explicit_bounds == _TTFT_BUCKETS
@@ -114,8 +115,8 @@ def test_non_streaming_chat_does_not_record_ttft_metric(
114115 messages = USER_ONLY_PROMPT , model = DEFAULT_MODEL , stream = False
115116 )
116117
117- ttft_metric = _get_ttft_metric (metric_reader )
118- assert ttft_metric is None , (
118+ data_points = _get_ttft_data_points (metric_reader )
119+ assert len ( data_points ) == 0 , (
119120 "gen_ai.server.time_to_first_token metric should not be recorded for non-streaming"
120121 )
121122
@@ -150,11 +151,11 @@ def test_streaming_tool_calls_records_ttft_metric(
150151 for _ in response :
151152 pass
152153
153- ttft_metric = _get_ttft_metric (metric_reader )
154- assert ttft_metric is not None , (
155- "gen_ai.server.time_to_first_token metric should be recorded for streaming tool calls"
154+ data_points = _get_ttft_data_points (metric_reader )
155+ assert len ( data_points ) == 1 , (
156+ "expected exactly one TTFT data point for streaming tool calls"
156157 )
157158
158- data_point = ttft_metric . data . data_points [0 ]
159+ data_point = data_points [0 ]
159160 assert data_point .sum >= 0
160161 assert data_point .count == 1
0 commit comments