diff --git a/src/google/adk/telemetry/_experimental_semconv.py b/src/google/adk/telemetry/_experimental_semconv.py index 894715ec641..1c97eec0078 100644 --- a/src/google/adk/telemetry/_experimental_semconv.py +++ b/src/google/adk/telemetry/_experimental_semconv.py @@ -678,13 +678,16 @@ def set_operation_details_attributes_from_response( _build_response_common_attributes(llm_response) ) response_attributes = _build_response_operation_details(llm_response) - # A turn arriving as several responses is reported as all of them rather than - # whichever came last, one message each, as - # opentelemetry-instrumentation-google-genai reports a stream too. + # Several responses for one call are chunks no aggregator assembled + # (`partial` unset), so they extend one message. Parts are appended rather + # than text-joined: thought and answer text are both `text` parts here. recorded = operation_details_attributes.get(GEN_AI_OUTPUT_MESSAGES) arrived = response_attributes.get(GEN_AI_OUTPUT_MESSAGES) if isinstance(recorded, list) and isinstance(arrived, list): - response_attributes[GEN_AI_OUTPUT_MESSAGES] = recorded + arrived + parts = [*recorded[0]['parts'], *arrived[0]['parts']] + response_attributes[GEN_AI_OUTPUT_MESSAGES] = [ + {**arrived[0], 'parts': parts} + ] operation_details_attributes.update(response_attributes) diff --git a/src/google/adk/telemetry/_instrumentation.py b/src/google/adk/telemetry/_instrumentation.py index ede7811f8b1..270cd732cc9 100644 --- a/src/google/adk/telemetry/_instrumentation.py +++ b/src/google/adk/telemetry/_instrumentation.py @@ -580,12 +580,19 @@ def record_llm_response( self, invocation_context: InvocationContext, response: LlmResponse ) -> None: self._llm_responses.append(response) - # Anything after the span ended (a stream's aggregate, a second complete - # response) is neither traced nor timed, but still counts for the metrics. + # Anything after the span ended (a second complete response) is neither + # traced nor timed, but still counts for the metrics. if self._inference_span_ended: return - self._record_chunk_timing(response) + # A stream's aggregate (`partial=False`) is assembled by ADK rather than + # waited for, so only the chunks are timed. + if response.partial is not False: + self._record_chunk_timing(response) + # A `partial` chunk is not traced: its aggregator yields the whole answer + # after it, with `partial=False`. + if response.partial: + return tracing.trace_inference_result(invocation_context, self.span, response) # The inference ends at the response that says why generation stopped. End # the span there, before the response is handed back to the caller, so what diff --git a/src/google/adk/telemetry/_web_ui.py b/src/google/adk/telemetry/_web_ui.py index c0c8ded1485..c4c024da16e 100644 --- a/src/google/adk/telemetry/_web_ui.py +++ b/src/google/adk/telemetry/_web_ui.py @@ -157,6 +157,8 @@ def _restored_attributes(log_record: LogRecord) -> dict[str, AnyValue]: content: dict[str, AnyValue] = {} set_operation_details_attributes_from_request(content, scope.llm_request) for llm_response in scope.llm_responses: + if llm_response.partial: + continue set_operation_details_attributes_from_response(llm_response, content, {}) # The event stops at the finish reason, as InferenceScope does. if is_reported_finish_reason(llm_response.finish_reason): diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v1.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v1.json index 53ea6233119..0ee57934b85 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v1.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v1.json @@ -212,13 +212,13 @@ "gcp.vertex.agent.event_id": "PRESENT", "gcp.vertex.agent.invocation_id": "PRESENT", "user.id": "test_user", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.input.messages": [ { "role": "user", @@ -286,17 +286,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v2.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v2.json index f829b52e31e..7a4cb7b5642 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v2.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-event-only-schema-v2.json @@ -173,13 +173,13 @@ "gcp.vertex.agent.invocation_id": "PRESENT", "gen_ai.provider.name": "gcp.gemini", "user.id": "test_user", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.input.messages": [ { "role": "user", @@ -247,17 +247,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v1.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v1.json index 82f07558d7b..7c9dc40fbd2 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v1.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v1.json @@ -164,13 +164,13 @@ "gen_ai.conversation.id": "PRESENT", "gcp.vertex.agent.event_id": "PRESENT", "gcp.vertex.agent.invocation_id": "PRESENT", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.tool.definitions": [ { "name": "some_tool", diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v2.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v2.json index f78b3be461e..8ecf3c828c9 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v2.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-no-content-schema-v2.json @@ -125,13 +125,13 @@ "gcp.vertex.agent.event_id": "PRESENT", "gcp.vertex.agent.invocation_id": "PRESENT", "gen_ai.provider.name": "gcp.gemini", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.tool.definitions": [ { "name": "some_tool", diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v1.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v1.json index 58be6a73f54..d0a8a2e7da6 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v1.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v1.json @@ -305,17 +305,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], @@ -335,13 +325,13 @@ "gcp.vertex.agent.event_id": "PRESENT", "gcp.vertex.agent.invocation_id": "PRESENT", "user.id": "test_user", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.input.messages": [ { "role": "user", @@ -409,17 +399,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v2.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v2.json index 6dafe4893a0..c435aaa3059 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v2.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-and-event-schema-v2.json @@ -271,17 +271,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], @@ -302,13 +292,13 @@ "gcp.vertex.agent.invocation_id": "PRESENT", "gen_ai.provider.name": "gcp.gemini", "user.id": "test_user", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.input.messages": [ { "role": "user", @@ -376,17 +366,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v1.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v1.json index c480d7f1adc..0ae4a64c9c7 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v1.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v1.json @@ -258,17 +258,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], @@ -287,13 +277,13 @@ "gen_ai.conversation.id": "PRESENT", "gcp.vertex.agent.event_id": "PRESENT", "gcp.vertex.agent.invocation_id": "PRESENT", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.tool.definitions": [ { "name": "some_tool", diff --git a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v2.json b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v2.json index 6c4867268e3..3ceece704dc 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v2.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/experimental-span-only-schema-v2.json @@ -224,17 +224,7 @@ "role": "assistant", "parts": [ { - "content": "text ", - "type": "text" - } - ], - "finish_reason": "" - }, - { - "role": "assistant", - "parts": [ - { - "content": "response", + "content": "text response", "type": "text" } ], @@ -254,13 +244,13 @@ "gcp.vertex.agent.event_id": "PRESENT", "gcp.vertex.agent.invocation_id": "PRESENT", "gen_ai.provider.name": "gcp.gemini", + "gen_ai.response.finish_reasons": [ + "stop" + ], "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 50, "gen_ai.usage.cache_read.input_tokens": 60, "gen_ai.usage.reasoning.output_tokens": 15, - "gen_ai.response.finish_reasons": [ - "stop" - ], "gen_ai.tool.definitions": [ { "name": "some_tool", diff --git a/tests/unittests/telemetry/functional_goldens/streaming/stable-capture-schema-v1.json b/tests/unittests/telemetry/functional_goldens/streaming/stable-capture-schema-v1.json index 332466e381c..6eb2b8c8ea0 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/stable-capture-schema-v1.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/stable-capture-schema-v1.json @@ -246,24 +246,7 @@ "content": { "parts": [ { - "text": "text " - } - ], - "role": "model" - }, - "index": 0 - }, - "attributes": { - "gen_ai.system": "gemini" - } - }, - { - "event_name": "gen_ai.choice", - "body": { - "content": { - "parts": [ - { - "text": "response" + "text": "text response" } ], "role": "model" diff --git a/tests/unittests/telemetry/functional_goldens/streaming/stable-no-capture-schema-v1.json b/tests/unittests/telemetry/functional_goldens/streaming/stable-no-capture-schema-v1.json index 0d4c790e8e8..1d61aeaa6b3 100644 --- a/tests/unittests/telemetry/functional_goldens/streaming/stable-no-capture-schema-v1.json +++ b/tests/unittests/telemetry/functional_goldens/streaming/stable-no-capture-schema-v1.json @@ -185,16 +185,6 @@ "gen_ai.system": "gemini" } }, - { - "event_name": "gen_ai.choice", - "body": { - "content": "", - "index": 0 - }, - "attributes": { - "gen_ai.system": "gemini" - } - }, { "event_name": "gen_ai.choice", "body": { diff --git a/tests/unittests/telemetry/functional_test_cases.py b/tests/unittests/telemetry/functional_test_cases.py index a3fb038e561..c49a632c2d2 100644 --- a/tests/unittests/telemetry/functional_test_cases.py +++ b/tests/unittests/telemetry/functional_test_cases.py @@ -204,9 +204,9 @@ def experimental_adk_matrix( schema_version=2, tool_exception=TOOL_ERROR, ), - # The same turn streamed, its answer arriving in two chunks that leave - # `partial` unset: one inference span per turn either way, and a completion - # log holding the whole answer rather than whichever chunk arrived first. + # The same turn streamed through Gemini's aggregator, its answer arriving in + # two chunks: one inference span per turn either way, and a completion log + # holding the aggregated answer rather than the chunks. *semconv_matrix("streaming"), # Compaction scenarios. Both triggers, because they place the # `compact_events` span differently: token-threshold nests inside diff --git a/tests/unittests/telemetry/test_experimental_semconv.py b/tests/unittests/telemetry/test_experimental_semconv.py index 82d63ca246b..9afd1edce3a 100644 --- a/tests/unittests/telemetry/test_experimental_semconv.py +++ b/tests/unittests/telemetry/test_experimental_semconv.py @@ -309,60 +309,31 @@ def test_response_attributes_split_between_details_and_common(): } -def test_response_attributes_accumulate_output_messages_across_a_stream(): - """Keeping only the last chunk truncated the log to it.""" +def test_unaggregated_chunks_are_reported_as_one_message(): + """Chunks no aggregator assembled extend one message, ending at the last.""" details: dict = {} common: dict = {} - for text in ('text ', 'response'): + for text, finish_reason in (('text ', None), ('response', 'STOP')): set_operation_details_attributes_from_response( LlmResponse( content=types.Content(role='model', parts=[types.Part(text=text)]), - finish_reason=types.FinishReason.STOP, + finish_reason=finish_reason, ), details, common, ) assert details == { - 'gen_ai.output.messages': [ - { - 'role': 'assistant', - 'parts': [{'content': 'text ', 'type': 'text'}], - 'finish_reason': 'stop', - }, - { - 'role': 'assistant', - 'parts': [{'content': 'response', 'type': 'text'}], - 'finish_reason': 'stop', - }, - ] + 'gen_ai.output.messages': [{ + 'role': 'assistant', + 'parts': [ + {'content': 'text ', 'type': 'text'}, + {'content': 'response', 'type': 'text'}, + ], + 'finish_reason': 'stop', + }] } - - -def test_streamed_chunks_are_reported_one_message_each(): - """Each chunk is its own message, as the OTel instrumentor reports a stream.""" - details: dict = {} - common: dict = {} - - # Only the chunk that ends the turn reports why generation stopped. - for text, finish_reason in ( - ('text ', types.FinishReason.FINISH_REASON_UNSPECIFIED), - ('response', types.FinishReason.STOP), - ): - set_operation_details_attributes_from_response( - LlmResponse( - content=types.Content(role='model', parts=[types.Part(text=text)]), - finish_reason=finish_reason, - ), - details, - common, - ) - - assert [ - message['parts'][0]['content'] - for message in details['gen_ai.output.messages'] - ] == ['text ', 'response'] assert common == {'gen_ai.response.finish_reasons': ['stop']} diff --git a/tests/unittests/telemetry/test_instrumentation.py b/tests/unittests/telemetry/test_instrumentation.py index 992aa9ec956..381fae3ec98 100644 --- a/tests/unittests/telemetry/test_instrumentation.py +++ b/tests/unittests/telemetry/test_instrumentation.py @@ -1665,7 +1665,7 @@ async def test_record_inference_telemetry_times_each_streamed_chunk( (500_000_000, unfinished_chunk), (750_000_000, unfinished_chunk), (1_250_000_000, _llm_response(partial=True)), - (10_000_000_000, _llm_response()), + (10_000_000_000, _llm_response(partial=False)), ] async with _instrumentation.record_inference_telemetry(