From 1dc9943ed1e8b71a72bfcf3dfa8378d5276b1937 Mon Sep 17 00:00:00 2001 From: yuval Date: Thu, 13 Nov 2025 16:29:04 +0200 Subject: [PATCH] fix: use SpanAttributes instead of GenAIAttributes for cache token attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed AttributeError by replacing GenAIAttributes with SpanAttributes for cache-related token attributes (GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS and GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS). These cache token attributes are custom extensions added by OpenLLMetry to support prompt caching features (Anthropic, OpenAI) and are not part of the upstream OpenTelemetry incubating semantic conventions. They are defined in the local SpanAttributes class, not in GenAIAttributes. Changed files: - opentelemetry-instrumentation-langchain/span_utils.py (line 349) - opentelemetry-instrumentation-anthropic/__init__.py (lines 286, 290, 400, 404) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../opentelemetry/instrumentation/anthropic/__init__.py | 8 ++++---- .../opentelemetry/instrumentation/langchain/span_utils.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py b/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py index f64c80776f..068d36819c 100644 --- a/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py +++ b/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py @@ -283,11 +283,11 @@ async def _aset_token_usage( set_span_attribute(span, SpanAttributes.LLM_USAGE_TOTAL_TOKENS, total_tokens) set_span_attribute( - span, GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens + span, SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens ) set_span_attribute( span, - GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, + SpanAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cache_creation_tokens, ) @@ -397,11 +397,11 @@ def _set_token_usage( set_span_attribute(span, SpanAttributes.LLM_USAGE_TOTAL_TOKENS, total_tokens) set_span_attribute( - span, GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens + span, SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens ) set_span_attribute( span, - GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, + SpanAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cache_creation_tokens, ) diff --git a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py index 33ea5d0131..60eb7caa4a 100644 --- a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py +++ b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py @@ -346,7 +346,7 @@ def set_chat_response_usage( ) _set_span_attribute( span, - GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, + SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens, ) if record_token_usage: