Skip to content

Commit 5fe637b

Browse files
committed
more lint
1 parent 99e2f46 commit 5fe637b

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def start_workflow(
211211
@deprecated(
212212
"handler.stop_llm() is deprecated. Use invocation.stop() instead."
213213
)
214-
def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation: # pyright: ignore[reportDeprecated]
214+
def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation: # pylint: disable=no-self-use # pyright: ignore[reportDeprecated]
215215
"""Finalize an LLM invocation successfully and end its span.
216216
217217
.. deprecated::
@@ -223,7 +223,7 @@ def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation: # pyright: igno
223223
@deprecated(
224224
"handler.fail_llm() is deprecated. Use invocation.fail(error) instead."
225225
)
226-
def fail_llm(
226+
def fail_llm( # pylint: disable=no-self-use
227227
self,
228228
invocation: LLMInvocation, # pyright: ignore[reportDeprecated]
229229
error: Error,

util/opentelemetry-util-genai/src/opentelemetry/util/genai/inference_invocation.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
gen_ai_attributes as GenAI,
2525
)
2626
from opentelemetry.semconv.attributes import server_attributes
27-
from opentelemetry.trace import INVALID_SPAN, SpanKind, Tracer
27+
from opentelemetry.trace import (
28+
INVALID_SPAN,
29+
SpanKind,
30+
Tracer,
31+
set_span_in_context,
32+
)
2833
from opentelemetry.util.genai.metrics import InvocationMetricsRecorder
2934
from opentelemetry.util.genai.types import (
3035
Error,
@@ -216,7 +221,7 @@ def _get_metric_attributes(self) -> dict[str, Any]:
216221
return attrs
217222

218223
def _get_metric_token_counts(self) -> dict[str, int]:
219-
counts = {}
224+
counts: dict[str, int] = {}
220225
if self.input_tokens is not None:
221226
counts[GenAI.GenAiTokenTypeValues.INPUT.value] = self.input_tokens
222227
if self.output_tokens is not None:
@@ -319,14 +324,15 @@ def __init__( # pylint: disable=too-many-locals
319324
# Old-style: data container, started later via handler.start_llm()
320325
# _tracer/_metrics_recorder/_logger are set by _start_with_handler() in that case
321326
self._operation_name = GenAI.GenAiOperationNameValues.CHAT.value
322-
self._tracer = None # type: ignore[assignment]
323-
self._metrics_recorder = None # type: ignore[assignment]
324-
self._logger = None # type: ignore[assignment]
327+
self._tracer = None # type: ignore[assignment] # pyright: ignore[reportUnnecessaryTypeIgnoreComment]
328+
self._metrics_recorder = None
329+
self._logger = None
325330
self.attributes = {} if attributes is None else attributes
326331
self.metric_attributes = (
327332
{} if metric_attributes is None else metric_attributes
328333
)
329334
self.span = INVALID_SPAN
335+
self._span_context = set_span_in_context(INVALID_SPAN)
330336
self._span_kind = SpanKind.CLIENT
331337
self._context_token = None
332338
self._monotonic_start_s = None
@@ -360,7 +366,7 @@ def __init__( # pylint: disable=too-many-locals
360366
)
361367

362368
@property
363-
def invocation(self) -> "LLMInvocation | None":
369+
def invocation(self) -> "LLMInvocation | None": # pyright: ignore[reportDeprecated]
364370
"""Returns self once started, None before handler.start_llm() is called."""
365371
return self if self._context_token is not None else None
366372

util/opentelemetry-util-genai/src/opentelemetry/util/genai/tool_invocation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
from typing_extensions import deprecated
2020

2121
from opentelemetry._logs import Logger
22+
from opentelemetry.semconv._incubating.attributes import (
23+
gen_ai_attributes as GenAI,
24+
)
2225
from opentelemetry.trace import Tracer
2326
from opentelemetry.util.genai.metrics import InvocationMetricsRecorder
2427
from opentelemetry.util.genai.types import Error, GenAIInvocation
@@ -80,6 +83,18 @@ def __init__(
8083
def _apply_finish(self, error: Error | None = None) -> None:
8184
if error is not None:
8285
self._apply_error_attributes(error)
86+
optional_attrs = (
87+
(GenAI.GEN_AI_TOOL_NAME, self.name),
88+
(GenAI.GEN_AI_TOOL_CALL_ID, self.tool_call_id),
89+
(GenAI.GEN_AI_TOOL_TYPE, self.tool_type),
90+
(GenAI.GEN_AI_TOOL_DESCRIPTION, self.tool_description),
91+
)
92+
attributes: dict[str, Any] = {
93+
GenAI.GEN_AI_OPERATION_NAME: self._operation_name,
94+
**{k: v for k, v in optional_attrs if v is not None},
95+
}
96+
attributes.update(self.attributes)
97+
self.span.set_attributes(attributes)
8398

8499

85100
@deprecated("ToolCall is deprecated. Use ToolInvocation instead.")

util/opentelemetry-util-genai/src/opentelemetry/util/genai/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def _get_metric_attributes(self) -> dict[str, Any]:
307307
"""Return low-cardinality attributes for metric recording."""
308308
return dict(self.metric_attributes)
309309

310-
def _get_metric_token_counts(self) -> dict[str, int]:
310+
def _get_metric_token_counts(self) -> dict[str, int]: # pylint: disable=no-self-use
311311
"""Return {token_type: count} for token histogram recording."""
312312
return {}
313313

0 commit comments

Comments
 (0)