Skip to content

fix(genai-utils): use AttributeValue instead of Any for attributes/metric_attributes#153

Open
bhumikadangayach wants to merge 2 commits into
open-telemetry:mainfrom
bhumikadangayach:fix/typing-any-attributes
Open

fix(genai-utils): use AttributeValue instead of Any for attributes/metric_attributes#153
bhumikadangayach wants to merge 2 commits into
open-telemetry:mainfrom
bhumikadangayach:fix/typing-any-attributes

Conversation

@bhumikadangayach

Copy link
Copy Markdown

Addresses the span/metric attribute portion of #41.

Replaces typing.Any with opentelemetry.util.types.AttributeValue for attributes and metric_attributes fields and their related getter methods, since these values are ultimately serialized to OTel span/metric attributes and AttributeValue is the correct constraint.

Files changed:

  • _invocation.py (base class)
  • _tool_invocation.py
  • _embedding_invocation.py
  • _agent_invocation.py
  • _workflow_invocation.py
  • _inference_invocation.py

Out of scope (left for follow-up, per discussion in #41):

  • Dataclass fields in types.py holding structured/nested payloads (ToolCallRequest.arguments, ServerToolCall.server_tool_call, FunctionToolDefinition.parameters, etc.) — these aren't flat attribute values and need separate discussion on the right type given semantic-conventions/messages models.
  • Metrics/logs usages not yet audited (per @DylanRussell's note in the issue).

All existing tests pass (240/240, no regressions).

Fixes #41 (partial — see scope note above)

…tric_attributes

Replaces typing.Any with opentelemetry.util.types.AttributeValue for
attributes and metric_attributes dicts across invocation types, since
these values must serialize to OTel span/metric attributes.

Scoped to the span/metric attribute path per discussion in open-telemetry#41.
Follow-up needed for: dataclass fields in types.py that hold nested/
structured payloads (ToolCallRequest.arguments, ServerToolCall, etc.),
and metrics/logs usages not yet audited.

Fixes open-telemetry#41 (partial)
Copilot AI review requested due to automatic review settings June 19, 2026 06:57
@bhumikadangayach bhumikadangayach requested a review from a team as a code owner June 19, 2026 06:57
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 19, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: bhumikadangayach / name: bhumikadangayach (32d75dc)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR tightens typing in opentelemetry-util-genai invocation helpers by replacing typing.Any with opentelemetry.util.types.AttributeValue for span/metric attribute dictionaries, aligning the public surface with what OpenTelemetry spans/metrics can actually serialize.

Changes:

  • Updates GenAIInvocation and multiple invocation subclasses to type attributes / metric_attributes as dict[str, AttributeValue].
  • Updates helper/getter methods that build attribute maps to return dict[str, AttributeValue].
  • Removes now-unneeded typing.Any imports from several modules.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py Tightens base invocation attribute types; updates get_content_attributes signature to AttributeValue.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_tool_invocation.py Updates tool invocation attribute typing to AttributeValue.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_embedding_invocation.py Updates embedding invocation attribute typing to AttributeValue.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_agent_invocation.py Updates agent invocation attribute typing to AttributeValue.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_workflow_invocation.py Updates workflow invocation attribute typing to AttributeValue.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py Updates inference invocation attribute typing to AttributeValue (including message attributes).
Comments suppressed due to low confidence (1)

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py:107

  • _get_message_attributes is annotated as returning dict[str, AttributeValue], but when for_span=False it includes structured message attributes intended for log events (lists/dicts). The annotation is therefore inaccurate and will become a typecheck blocker once get_content_attributes is typed correctly for the event case. Consider adding overloads keyed on Literal[True/False] (or otherwise splitting span vs event attribute builders) so span attributes remain AttributeValue while event attributes can be structured.
    def _get_message_attributes(self, *, for_span: bool) -> dict[str, AttributeValue]:
        return get_content_attributes(
            input_messages=self.input_messages,
            output_messages=self.output_messages,
            system_instruction=self.system_instruction,
            tool_definitions=self.tool_definitions,
            for_span=for_span,
        )

Comment on lines 4 to 8
from __future__ import annotations

from typing import Any


from opentelemetry._logs import Logger
Comment on lines 4 to 8
from __future__ import annotations

from typing import Any


from opentelemetry._logs import Logger
Comment on lines +12 to +13
from typing import TYPE_CHECKING, Any, Sequence, TypeAlias
from opentelemetry.util.types import AttributeValue
Comment on lines +183 to +190
tool_definitions: Sequence[ToolDefinition] | None,
for_span: bool,
) -> dict[str, Any]:
) -> dict[str, AttributeValue]:
)

def _get_metric_attributes(self) -> dict[str, Any]:
def _get_metric_attributes(self) ->dict[str, AttributeValue]:
Comment on lines +89 to 91
attributes: dict[str, AttributeValue] = {
GenAI.GEN_AI_OPERATION_NAME: self._operation_name
}
- Revert get_content_attributes return type to dict[str, Any] in
  _invocation.py since it returns list[dict] for event attributes
  when for_span=False
- Fix import grouping in _invocation.py and _agent_invocation.py
- Minor formatting fixes (arrow spacing, trailing comma)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[genai-utils] Remove all usages of typing.Any

2 participants