|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Helpers for building log bodies that follow the stable OTel GenAI semconv. |
| 16 | +
|
| 17 | +This module centralizes the construction of `gen_ai.system.message`, |
| 18 | +`gen_ai.user.message`, and `gen_ai.choice` log bodies so that both the |
| 19 | +tracing layer (which emits the logs) and the ADK Web UI exporter (which |
| 20 | +rebuilds the bodies after elision) share the same shape. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +from collections.abc import Mapping |
| 26 | +from typing import TYPE_CHECKING |
| 27 | + |
| 28 | +from opentelemetry.util.types import AnyValue |
| 29 | + |
| 30 | +from ._serialization import serialize_content |
| 31 | +from .context import TelemetryConfig |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from google.genai import types |
| 35 | + |
| 36 | + from ..models.llm_request import LlmRequest |
| 37 | + from ..models.llm_response import LlmResponse |
| 38 | + |
| 39 | +# Stable OTel GenAI semantic-convention event names. |
| 40 | +GEN_AI_SYSTEM_MESSAGE_EVENT = "gen_ai.system.message" |
| 41 | +GEN_AI_USER_MESSAGE_EVENT = "gen_ai.user.message" |
| 42 | +GEN_AI_CHOICE_EVENT = "gen_ai.choice" |
| 43 | + |
| 44 | +# Standard OTEL env variable that controls whether prompt/response content is |
| 45 | +# included in log bodies. When unset/false, content is replaced with <elided>. |
| 46 | +OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = ( |
| 47 | + "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" |
| 48 | +) |
| 49 | + |
| 50 | +USER_CONTENT_ELIDED = "<elided>" |
| 51 | + |
| 52 | + |
| 53 | +def _serialize_content_with_optional_elision( |
| 54 | + content: types.ContentUnion | None, *, capture_content: bool |
| 55 | +) -> AnyValue: |
| 56 | + if not capture_content: |
| 57 | + return USER_CONTENT_ELIDED |
| 58 | + if content is None: |
| 59 | + return None |
| 60 | + return serialize_content(content) |
| 61 | + |
| 62 | + |
| 63 | +def system_message_body( |
| 64 | + llm_request: LlmRequest, |
| 65 | + telemetry_config: TelemetryConfig, |
| 66 | + *, |
| 67 | + do_not_elide: bool = False, |
| 68 | +) -> Mapping[str, AnyValue]: |
| 69 | + """Builds the body for a `gen_ai.system.message` log event. |
| 70 | +
|
| 71 | + Args: |
| 72 | + llm_request: The LLM request whose system instruction should be logged. |
| 73 | + do_not_elide_content: When True, always include the content regardless of |
| 74 | + the `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` env var. The Web |
| 75 | + UI exporter sets this to True because the UI needs the full content. |
| 76 | + """ |
| 77 | + system_instruction = None |
| 78 | + if llm_request.config is not None: |
| 79 | + system_instruction = llm_request.config.system_instruction |
| 80 | + return { |
| 81 | + "content": _serialize_content_with_optional_elision( |
| 82 | + system_instruction, |
| 83 | + capture_content=do_not_elide |
| 84 | + or telemetry_config.should_add_content_to_logs, |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +def user_message_body( |
| 90 | + content: types.ContentUnion | None, |
| 91 | + telemetry_config: TelemetryConfig, |
| 92 | + *, |
| 93 | + do_not_elide: bool = False, |
| 94 | +) -> Mapping[str, AnyValue]: |
| 95 | + """Builds the body for a single `gen_ai.user.message` log event. |
| 96 | +
|
| 97 | + Args: |
| 98 | + content: The user content for this message. Callers that emit multiple user |
| 99 | + messages (e.g. tracing's per-content loop) call this builder once per |
| 100 | + content. |
| 101 | + do_not_elide_content: When True, always include the content regardless of |
| 102 | + the `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` env var. |
| 103 | + """ |
| 104 | + return { |
| 105 | + "content": _serialize_content_with_optional_elision( |
| 106 | + content, |
| 107 | + capture_content=do_not_elide |
| 108 | + or telemetry_config.should_add_content_to_logs, |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +def choice_body( |
| 114 | + llm_response: LlmResponse | None, |
| 115 | + telemetry_config: TelemetryConfig, |
| 116 | + *, |
| 117 | + do_not_elide: bool = False, |
| 118 | +) -> Mapping[str, AnyValue]: |
| 119 | + """Builds the body for a `gen_ai.choice` log event. |
| 120 | +
|
| 121 | + ADK always returns a single candidate, so `index` is always 0. |
| 122 | + `finish_reason` is included only when present on the response. |
| 123 | +
|
| 124 | + Args: |
| 125 | + llm_response: The LLM response describing the choice. |
| 126 | + do_not_elide_content: When True, always include the content regardless of |
| 127 | + the `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` env var. |
| 128 | + """ |
| 129 | + if llm_response is None: |
| 130 | + return {"content": None, "index": 0} |
| 131 | + body: dict[str, AnyValue] = { |
| 132 | + "content": _serialize_content_with_optional_elision( |
| 133 | + llm_response.content, |
| 134 | + capture_content=do_not_elide |
| 135 | + or telemetry_config.should_add_content_to_logs, |
| 136 | + ), |
| 137 | + "index": 0, # ADK always returns a single candidate. |
| 138 | + } |
| 139 | + if llm_response.finish_reason is not None: |
| 140 | + finish_reason = llm_response.finish_reason |
| 141 | + body["finish_reason"] = ( |
| 142 | + finish_reason.value |
| 143 | + if hasattr(finish_reason, "value") |
| 144 | + else str(finish_reason) |
| 145 | + ) |
| 146 | + return body |
0 commit comments