Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat: Support OpenAI V2 (#1121)

Ishan Jain30c09d8b0bdd
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index a28c4687..0d941c9b 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -43,7 +43,7 @@ opentelemetry-instrumentation-urllib = ">=0.52b0,<1.0.0"
opentelemetry-instrumentation-urllib3 = ">=0.52b0,<1.0.0"
# Official OpenTelemetry System Metrics (optional)
opentelemetry-instrumentation-system-metrics = {version = ">=0.52b0,<1.0.0", optional = true}
-openai = "^1.1.1"
+openai = ">=1.92.0,<3.0.0"
anthropic = ">=0.42.0,<1.0.0"
[tool.poetry.scripts]
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 18957347..acc2319f 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -605,7 +605,7 @@ def otel_event(name, attributes, body):
context_attrs = _custom_span_attributes.get()
if context_attrs:
base_attrs.update(context_attrs)
- return LogRecord(
+ return LogRecord( # pylint: disable=unexpected-keyword-arg
attributes=base_attrs,
body=body,
event_name=name,
diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.py
index 02f4e756..1b55202a 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -247,9 +247,7 @@ def init(
]
for invalid_name in invalid_instrumentors:
lower_name = invalid_name.lower()
- suggestions = [
- k for k in MODULE_NAME_MAP if lower_name in k or k in lower_name
- ]
+ suggestions = [k for k in MODULE_NAME_MAP if lower_name in k or k in lower_name]
if suggestions:
logger.warning(
"Invalid instrumentor name '%s'. Did you mean: %s?",
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/__init__.py b/sdk/python/src/openlit/instrumentation/anthropic/__init__.py
index 0ce90bc8..849e799d 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/__init__.py
@@ -8,7 +8,10 @@ from wrapt import wrap_function_wrapper
from openlit._config import OpenlitConfig
from openlit.instrumentation.anthropic.anthropic import messages, messages_stream
-from openlit.instrumentation.anthropic.async_anthropic import async_messages, async_messages_stream
+from openlit.instrumentation.anthropic.async_anthropic import (
+ async_messages,
+ async_messages_stream,
+)
_instruments = ("anthropic >= 0.21.0",)
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py b/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py
index f546728f..93b251b7 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py
@@ -277,19 +277,23 @@ def messages_stream(
"""Drains the stream to finalize the span before returning the result."""
for _ in self:
pass
- original_get_final_message = getattr(self.__wrapped__, 'get_final_message')
+ original_get_final_message = getattr(self.__wrapped__, "get_final_message")
return original_get_final_message()
@property
def _instrumented_text_stream(self):
"""Yields text while ensuring every chunk hits our instrumentation."""
+
def text_generator():
for event in self:
- if (hasattr(event, 'delta') and
- hasattr(event.delta, 'type') and
- event.delta.type == 'text_delta' and
- hasattr(event.delta, 'text')):
+ if (
+ hasattr(event, "delta")
+ and hasattr(event.delta, "type")
+ and event.delta.type == "text_delta"
+ and hasattr(event.delta, "text")
+ ):
yield event.delta.text
+
return text_generator()
def _instrumented_until_done(self):
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py b/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py
index 2c42b839..de9a0c99 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py
@@ -277,19 +277,23 @@ def async_messages_stream(
"""Awaits stream completion via proxy then returns the final message."""
async for _ in self:
pass
- original_get_final_message = getattr(self.__wrapped__, 'get_final_message')
+ original_get_final_message = getattr(self.__wrapped__, "get_final_message")
return await original_get_final_message()
@property
def _instrumented_text_stream(self):
"""Async generator that processes chunks through our proxy."""
+
async def text_generator():
async for event in self:
- if (hasattr(event, 'delta') and
- hasattr(event.delta, 'type') and
- event.delta.type == 'text_delta' and
- hasattr(event.delta, 'text')):
+ if (
+ hasattr(event, "delta")
+ and hasattr(event.delta, "type")
+ and event.delta.type == "text_delta"
+ and hasattr(event.delta, "text")
+ ):
yield event.delta.text
+
return text_generator()
async def _instrumented_until_done(self):
diff --git a/sdk/python/src/openlit/instrumentation/openai/__init__.py b/sdk/python/src/openlit/instrumentation/openai/__init__.py
index 9197d4cc..863df1ed 100644
--- a/sdk/python/src/openlit/instrumentation/openai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/openai/__init__.py
@@ -16,6 +16,48 @@ from openlit.instrumentation.openai.openai import (
image_generate,
image_variatons,
audio_create,
+ audio_transcription,
+ audio_translation,
+ image_edit,
+ moderation,
+ responses_retrieve,
+ responses_cancel,
+ responses_token_count,
+ chat_messages_list,
+ batch_create,
+ batch_retrieve,
+ batch_list,
+ batch_cancel,
+ fine_tuning_create,
+ fine_tuning_retrieve,
+ fine_tuning_list,
+ fine_tuning_cancel,
+ vector_store_create,
+ vector_store_retrieve,
+ vector_store_update,
+ vector_store_delete,
+ vector_store_list,
+ vector_store_search,
+ file_create,
+ file_retrieve,
+ file_delete,
+ file_content,
+ video_create,
+ video_retrieve,
+ video_list,
+ video_delete,
+ video_edit_op,
+ video_extend,
+ video_remix,
+ conversation_create,
+ conversation_retrieve,
+ conversation_update,
+ conversation_delete,
+ conversation_item_create,
+ conversation_item_list,
+ conversation_item_retrieve,
+ conversation_item_delete,
+ realtime_session_create,
)
from openlit.instrumentation.openai.async_openai import (
async_chat_completions,
@@ -25,11 +67,78 @@ from openlit.instrumentation.openai.async_openai import (
async_image_variations,
async_audio_create,
async_responses,
+ async_audio_transcription,
+ async_audio_translation,
+ async_image_edit,
+ async_moderation,
+ async_responses_retrieve,
+ async_responses_cancel,
+ async_responses_token_count,
+ async_chat_messages_list,
+ async_batch_create,
+ async_batch_retrieve,
+ async_batch_list,
+ async_batch_cancel,
+ async_fine_tuning_create,
+ async_fine_tuning_retrieve,
+ async_fine_tuning_list,
+ async_fine_tuning_cancel,
+ async_vector_store_create,
+ async_vector_store_retrieve,
+ async_vector_store_update,
+ async_vector_store_delete,
+ async_vector_store_list,
+ async_vector_store_search,
+ async_file_create,
+ async_file_retrieve,
+ async_file_delete,
+ async_file_content,
+ async_video_create,
+ async_video_retrieve,
+ async_video_list,
+ async_video_delete,
+ async_video_edit_op,
+ async_video_extend,
+ async_video_remix,
+ async_conversation_create,
+ async_conversation_retrieve,
+ async_conversation_update,
+ async_conversation_delete,
+ async_conversation_item_create,
+ async_conversation_item_list,
+ async_conversation_item_retrieve,
+ async_conversation_item_delete,
+ async_realtime_session_create,
)
_instruments = ("openai >= 1.92.0",)
+def _standard_args(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider,
+):
+ """Return the standard argument tuple used by all wrapper constructors."""
+ return (
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider,
+ )
+
+
class OpenAIInstrumentor(BaseInstrumentor):
"""
An instrumentor for OpenAI client library.
@@ -49,235 +158,552 @@ class OpenAIInstrumentor(BaseInstrumentor):
disable_metrics = kwargs.get("disable_metrics")
event_provider = _logs.get_logger_provider().get_logger(__name__)
+ sa = _standard_args(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider,
+ )
+
# chat completions
wrap_function_wrapper(
"openai.resources.chat.completions",
"Completions.create",
- chat_completions(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ chat_completions(*sa),
)
-
wrap_function_wrapper(
"openai.resources.chat.completions",
"AsyncCompletions.create",
- async_chat_completions(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_chat_completions(*sa),
)
# chat completions parse
wrap_function_wrapper(
"openai.resources.chat.completions",
"Completions.parse",
- chat_completions_parse(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ chat_completions_parse(*sa),
)
-
wrap_function_wrapper(
"openai.resources.chat.completions",
"AsyncCompletions.parse",
- async_chat_completions_parse(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_chat_completions_parse(*sa),
)
# responses
wrap_function_wrapper(
"openai.resources.responses.responses",
"Responses.create",
- responses(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ responses(*sa),
)
-
wrap_function_wrapper(
"openai.resources.responses.responses",
"AsyncResponses.create",
- async_responses(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_responses(*sa),
+ )
+
+ # responses retrieve
+ wrap_function_wrapper(
+ "openai.resources.responses.responses",
+ "Responses.retrieve",
+ responses_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.responses.responses",
+ "AsyncResponses.retrieve",
+ async_responses_retrieve(*sa),
+ )
+
+ # responses cancel
+ wrap_function_wrapper(
+ "openai.resources.responses.responses",
+ "Responses.cancel",
+ responses_cancel(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.responses.responses",
+ "AsyncResponses.cancel",
+ async_responses_cancel(*sa),
+ )
+
+ # responses input tokens count
+ wrap_function_wrapper(
+ "openai.resources.responses.input_tokens",
+ "InputTokens.count",
+ responses_token_count(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.responses.input_tokens",
+ "AsyncInputTokens.count",
+ async_responses_token_count(*sa),
+ )
+
+ # chat messages list
+ wrap_function_wrapper(
+ "openai.resources.chat.completions.messages",
+ "Messages.list",
+ chat_messages_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.chat.completions.messages",
+ "AsyncMessages.list",
+ async_chat_messages_list(*sa),
)
# embeddings
wrap_function_wrapper(
"openai.resources.embeddings",
"Embeddings.create",
- embedding(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ embedding(*sa),
)
-
wrap_function_wrapper(
"openai.resources.embeddings",
"AsyncEmbeddings.create",
- async_embedding(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_embedding(*sa),
)
# image generation
wrap_function_wrapper(
"openai.resources.images",
"Images.generate",
- image_generate(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ image_generate(*sa),
)
-
wrap_function_wrapper(
"openai.resources.images",
"AsyncImages.generate",
- async_image_generate(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_image_generate(*sa),
)
# image variations
wrap_function_wrapper(
"openai.resources.images",
"Images.create_variation",
- image_variatons(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ image_variatons(*sa),
)
-
wrap_function_wrapper(
"openai.resources.images",
"AsyncImages.create_variation",
- async_image_variations(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_image_variations(*sa),
)
- # audio generation
+ # image edit
+ wrap_function_wrapper(
+ "openai.resources.images",
+ "Images.edit",
+ image_edit(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.images",
+ "AsyncImages.edit",
+ async_image_edit(*sa),
+ )
+
+ # audio generation (TTS)
wrap_function_wrapper(
"openai.resources.audio.speech",
"Speech.create",
- audio_create(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ audio_create(*sa),
)
-
wrap_function_wrapper(
"openai.resources.audio.speech",
"AsyncSpeech.create",
- async_audio_create(
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- event_provider,
- ),
+ async_audio_create(*sa),
+ )
+
+ # audio transcription (STT)
+ wrap_function_wrapper(
+ "openai.resources.audio.transcriptions",
+ "Transcriptions.create",
+ audio_transcription(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.audio.transcriptions",
+ "AsyncTranscriptions.create",
+ async_audio_transcription(*sa),
+ )
+
+ # audio translation
+ wrap_function_wrapper(
+ "openai.resources.audio.translations",
+ "Translations.create",
+ audio_translation(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.audio.translations",
+ "AsyncTranslations.create",
+ async_audio_translation(*sa),
+ )
+
+ # moderations
+ wrap_function_wrapper(
+ "openai.resources.moderations",
+ "Moderations.create",
+ moderation(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.moderations",
+ "AsyncModerations.create",
+ async_moderation(*sa),
+ )
+
+ # batch API
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "Batches.create",
+ batch_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "AsyncBatches.create",
+ async_batch_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "Batches.retrieve",
+ batch_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "AsyncBatches.retrieve",
+ async_batch_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "Batches.list",
+ batch_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "AsyncBatches.list",
+ async_batch_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "Batches.cancel",
+ batch_cancel(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.batches",
+ "AsyncBatches.cancel",
+ async_batch_cancel(*sa),
+ )
+
+ # fine-tuning jobs
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "Jobs.create",
+ fine_tuning_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "AsyncJobs.create",
+ async_fine_tuning_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "Jobs.retrieve",
+ fine_tuning_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "AsyncJobs.retrieve",
+ async_fine_tuning_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "Jobs.list",
+ fine_tuning_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "AsyncJobs.list",
+ async_fine_tuning_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "Jobs.cancel",
+ fine_tuning_cancel(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.fine_tuning.jobs.jobs",
+ "AsyncJobs.cancel",
+ async_fine_tuning_cancel(*sa),
+ )
+
+ # vector stores
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "VectorStores.create",
+ vector_store_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "AsyncVectorStores.create",
+ async_vector_store_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "VectorStores.retrieve",
+ vector_store_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "AsyncVectorStores.retrieve",
+ async_vector_store_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "VectorStores.update",
+ vector_store_update(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "AsyncVectorStores.update",
+ async_vector_store_update(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "VectorStores.delete",
+ vector_store_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "AsyncVectorStores.delete",
+ async_vector_store_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "VectorStores.list",
+ vector_store_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "AsyncVectorStores.list",
+ async_vector_store_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "VectorStores.search",
+ vector_store_search(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.vector_stores.vector_stores",
+ "AsyncVectorStores.search",
+ async_vector_store_search(*sa),
+ )
+
+ # files API
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "Files.create",
+ file_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "AsyncFiles.create",
+ async_file_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "Files.retrieve",
+ file_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "AsyncFiles.retrieve",
+ async_file_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "Files.delete",
+ file_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "AsyncFiles.delete",
+ async_file_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "Files.content",
+ file_content(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.files",
+ "AsyncFiles.content",
+ async_file_content(*sa),
+ )
+
+ # video / sora API
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.create",
+ video_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.create",
+ async_video_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.retrieve",
+ video_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.retrieve",
+ async_video_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.list",
+ video_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.list",
+ async_video_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.delete",
+ video_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.delete",
+ async_video_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.edit",
+ video_edit_op(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.edit",
+ async_video_edit_op(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.extend",
+ video_extend(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.extend",
+ async_video_extend(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "Videos.remix",
+ video_remix(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.videos",
+ "AsyncVideos.remix",
+ async_video_remix(*sa),
+ )
+
+ # conversations API
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "Conversations.create",
+ conversation_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "AsyncConversations.create",
+ async_conversation_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "Conversations.retrieve",
+ conversation_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "AsyncConversations.retrieve",
+ async_conversation_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "Conversations.update",
+ conversation_update(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "AsyncConversations.update",
+ async_conversation_update(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "Conversations.delete",
+ conversation_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.conversations",
+ "AsyncConversations.delete",
+ async_conversation_delete(*sa),
+ )
+
+ # conversation items
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "Items.create",
+ conversation_item_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "AsyncItems.create",
+ async_conversation_item_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "Items.list",
+ conversation_item_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "AsyncItems.list",
+ async_conversation_item_list(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "Items.retrieve",
+ conversation_item_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "AsyncItems.retrieve",
+ async_conversation_item_retrieve(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "Items.delete",
+ conversation_item_delete(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.conversations.items",
+ "AsyncItems.delete",
+ async_conversation_item_delete(*sa),
+ )
+
+ # realtime API
+ wrap_function_wrapper(
+ "openai.resources.realtime.sessions",
+ "Sessions.create",
+ realtime_session_create(*sa),
+ )
+ wrap_function_wrapper(
+ "openai.resources.realtime.sessions",
+ "AsyncSessions.create",
+ async_realtime_session_create(*sa),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/openai/async_openai.py b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
index b0edf3f8..ed645ed6 100644
--- a/sdk/python/src/openlit/instrumentation/openai/async_openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
@@ -22,6 +22,9 @@ from openlit.instrumentation.openai.utils import (
process_embedding_response,
process_image_response,
process_audio_response,
+ process_transcription_response,
+ process_moderation_response,
+ process_lightweight_response,
)
from openlit.semcov import SemanticConvention
@@ -509,6 +512,7 @@ def async_embedding(
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
+ event_provider=event_provider,
**kwargs,
)
@@ -585,6 +589,7 @@ def async_image_generate(
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
+ event_provider=event_provider,
**kwargs,
)
@@ -664,6 +669,7 @@ def async_image_variations(
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
+ event_provider=event_provider,
**kwargs,
)
@@ -743,6 +749,7 @@ def async_audio_create(
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
+ event_provider=event_provider,
**kwargs,
)
@@ -772,3 +779,422 @@ def async_audio_create(
return response
return wrapper
+
+
+def async_audio_transcription(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI async audio transcription.
+ """
+
+ async def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "whisper-1")
+
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT} {request_model}"
+ )
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = await wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_transcription_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def async_audio_translation(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI async audio translation.
+ """
+
+ async def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "whisper-1")
+
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT} {request_model}"
+ )
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = await wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_transcription_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def async_image_edit(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI async image editing.
+ """
+
+ async def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "gpt-image-1")
+
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = await wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_image_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def async_moderation(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI async moderation.
+ """
+
+ async def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "omni-moderation-latest")
+
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_MODERATION} {request_model}"
+ )
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = await wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_moderation_response(
+ response=response,
+ request_model=request_model,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def _make_async_lightweight_wrapper(
+ span_prefix, operation_type, default_model="unknown"
+):
+ """Factory for creating lightweight async wrappers for infrastructure APIs."""
+
+ def outer(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+ ):
+ async def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", default_model)
+ span_name = f"{span_prefix} {request_model}"
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = await wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_lightweight_response(
+ response=response,
+ operation_type=operation_type,
+ request_model=request_model,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs,
+ )
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+ return outer
+
+
+# Responses API extras
+async_responses_retrieve = _make_async_lightweight_wrapper(
+ "responses.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+async_responses_cancel = _make_async_lightweight_wrapper(
+ "responses.cancel", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+async_responses_token_count = _make_async_lightweight_wrapper(
+ "responses.token_count", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+
+# Chat messages
+async_chat_messages_list = _make_async_lightweight_wrapper(
+ "chat.messages.list", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+
+# Batch
+async_batch_create = _make_async_lightweight_wrapper(
+ "batch.create", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+async_batch_retrieve = _make_async_lightweight_wrapper(
+ "batch.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+async_batch_list = _make_async_lightweight_wrapper(
+ "batch.list", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+async_batch_cancel = _make_async_lightweight_wrapper(
+ "batch.cancel", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+
+# Fine-tuning
+async_fine_tuning_create = _make_async_lightweight_wrapper(
+ "fine_tuning.create", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+async_fine_tuning_retrieve = _make_async_lightweight_wrapper(
+ "fine_tuning.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+async_fine_tuning_list = _make_async_lightweight_wrapper(
+ "fine_tuning.list", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+async_fine_tuning_cancel = _make_async_lightweight_wrapper(
+ "fine_tuning.cancel", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+
+# Vector stores
+async_vector_store_create = _make_async_lightweight_wrapper(
+ "vector_store.create", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+async_vector_store_retrieve = _make_async_lightweight_wrapper(
+ "vector_store.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+async_vector_store_update = _make_async_lightweight_wrapper(
+ "vector_store.update", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+async_vector_store_delete = _make_async_lightweight_wrapper(
+ "vector_store.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+async_vector_store_list = _make_async_lightweight_wrapper(
+ "vector_store.list", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+async_vector_store_search = _make_async_lightweight_wrapper(
+ "vector_store.search", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+
+# Files
+async_file_create = _make_async_lightweight_wrapper(
+ "file.create", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+async_file_retrieve = _make_async_lightweight_wrapper(
+ "file.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+async_file_delete = _make_async_lightweight_wrapper(
+ "file.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+async_file_content = _make_async_lightweight_wrapper(
+ "file.content", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+
+# Video
+async_video_create = _make_async_lightweight_wrapper(
+ "video.create", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+async_video_retrieve = _make_async_lightweight_wrapper(
+ "video.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+async_video_list = _make_async_lightweight_wrapper(
+ "video.list", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+async_video_delete = _make_async_lightweight_wrapper(
+ "video.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+async_video_edit_op = _make_async_lightweight_wrapper(
+ "video.edit", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+async_video_extend = _make_async_lightweight_wrapper(
+ "video.extend", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+async_video_remix = _make_async_lightweight_wrapper(
+ "video.remix", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+
+# Conversations
+async_conversation_create = _make_async_lightweight_wrapper(
+ "conversation.create", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_retrieve = _make_async_lightweight_wrapper(
+ "conversation.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_update = _make_async_lightweight_wrapper(
+ "conversation.update", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_delete = _make_async_lightweight_wrapper(
+ "conversation.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_item_create = _make_async_lightweight_wrapper(
+ "conversation.item.create", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_item_list = _make_async_lightweight_wrapper(
+ "conversation.item.list", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_item_retrieve = _make_async_lightweight_wrapper(
+ "conversation.item.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+async_conversation_item_delete = _make_async_lightweight_wrapper(
+ "conversation.item.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+
+# Realtime
+async_realtime_session_create = _make_async_lightweight_wrapper(
+ "realtime.session",
+ SemanticConvention.GEN_AI_OPERATION_TYPE_REALTIME,
+ "gpt-4o-realtime-preview",
+)
diff --git a/sdk/python/src/openlit/instrumentation/openai/openai.py b/sdk/python/src/openlit/instrumentation/openai/openai.py
index aa6cc9d1..bded6f0e 100644
--- a/sdk/python/src/openlit/instrumentation/openai/openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/openai.py
@@ -22,6 +22,9 @@ from openlit.instrumentation.openai.utils import (
process_embedding_response,
process_image_response,
process_audio_response,
+ process_transcription_response,
+ process_moderation_response,
+ process_lightweight_response,
)
from openlit.semcov import SemanticConvention
@@ -781,3 +784,420 @@ def audio_create(
return response
return wrapper
+
+
+def audio_transcription(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI audio transcription.
+ """
+
+ def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "whisper-1")
+
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT} {request_model}"
+ )
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_transcription_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def audio_translation(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI audio translation.
+ """
+
+ def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "whisper-1")
+
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT} {request_model}"
+ )
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_transcription_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def image_edit(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI image editing.
+ """
+
+ def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "gpt-image-1")
+
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_image_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def moderation(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+):
+ """
+ Generates a telemetry wrapper for OpenAI moderation.
+ """
+
+ def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", "omni-moderation-latest")
+
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_MODERATION} {request_model}"
+ )
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_moderation_response(
+ response=response,
+ request_model=request_model,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ event_provider=event_provider,
+ **kwargs,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+
+def _make_lightweight_wrapper(span_prefix, operation_type, default_model="unknown"):
+ """Factory for creating lightweight sync wrappers for infrastructure APIs."""
+
+ def outer(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ event_provider=None,
+ ):
+ def wrapper(wrapped, instance, args, kwargs):
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
+ request_model = kwargs.get("model", default_model)
+ span_name = f"{span_prefix} {request_model}"
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ response = wrapped(*args, **kwargs)
+ end_time = time.time()
+
+ try:
+ response = process_lightweight_response(
+ response=response,
+ operation_type=operation_type,
+ request_model=request_model,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ end_time=end_time,
+ span=span,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs,
+ )
+ except Exception as e:
+ handle_exception(span, e)
+
+ return response
+
+ return wrapper
+
+ return outer
+
+
+# Responses API extras
+responses_retrieve = _make_lightweight_wrapper(
+ "responses.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+responses_cancel = _make_lightweight_wrapper(
+ "responses.cancel", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+responses_token_count = _make_lightweight_wrapper(
+ "responses.token_count", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+
+# Chat messages
+chat_messages_list = _make_lightweight_wrapper(
+ "chat.messages.list", SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, "gpt-4o"
+)
+
+# Batch
+batch_create = _make_lightweight_wrapper(
+ "batch.create", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+batch_retrieve = _make_lightweight_wrapper(
+ "batch.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+batch_list = _make_lightweight_wrapper(
+ "batch.list", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+batch_cancel = _make_lightweight_wrapper(
+ "batch.cancel", SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH
+)
+
+# Fine-tuning
+fine_tuning_create = _make_lightweight_wrapper(
+ "fine_tuning.create", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+fine_tuning_retrieve = _make_lightweight_wrapper(
+ "fine_tuning.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+fine_tuning_list = _make_lightweight_wrapper(
+ "fine_tuning.list", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+fine_tuning_cancel = _make_lightweight_wrapper(
+ "fine_tuning.cancel", SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING
+)
+
+# Vector stores
+vector_store_create = _make_lightweight_wrapper(
+ "vector_store.create", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+vector_store_retrieve = _make_lightweight_wrapper(
+ "vector_store.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+vector_store_update = _make_lightweight_wrapper(
+ "vector_store.update", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+vector_store_delete = _make_lightweight_wrapper(
+ "vector_store.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+vector_store_list = _make_lightweight_wrapper(
+ "vector_store.list", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+vector_store_search = _make_lightweight_wrapper(
+ "vector_store.search", SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB
+)
+
+# Files
+file_create = _make_lightweight_wrapper(
+ "file.create", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+file_retrieve = _make_lightweight_wrapper(
+ "file.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+file_delete = _make_lightweight_wrapper(
+ "file.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+file_content = _make_lightweight_wrapper(
+ "file.content", SemanticConvention.GEN_AI_OPERATION_TYPE_FILE
+)
+
+# Video
+video_create = _make_lightweight_wrapper(
+ "video.create", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+video_retrieve = _make_lightweight_wrapper(
+ "video.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+video_list = _make_lightweight_wrapper(
+ "video.list", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+video_delete = _make_lightweight_wrapper(
+ "video.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+video_edit_op = _make_lightweight_wrapper(
+ "video.edit", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+video_extend = _make_lightweight_wrapper(
+ "video.extend", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+video_remix = _make_lightweight_wrapper(
+ "video.remix", SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO, "sora-2"
+)
+
+# Conversations
+conversation_create = _make_lightweight_wrapper(
+ "conversation.create", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_retrieve = _make_lightweight_wrapper(
+ "conversation.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_update = _make_lightweight_wrapper(
+ "conversation.update", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_delete = _make_lightweight_wrapper(
+ "conversation.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_item_create = _make_lightweight_wrapper(
+ "conversation.item.create", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_item_list = _make_lightweight_wrapper(
+ "conversation.item.list", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_item_retrieve = _make_lightweight_wrapper(
+ "conversation.item.retrieve", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+conversation_item_delete = _make_lightweight_wrapper(
+ "conversation.item.delete", SemanticConvention.GEN_AI_OPERATION_TYPE_CONVERSATION
+)
+
+# Realtime
+realtime_session_create = _make_lightweight_wrapper(
+ "realtime.session",
+ SemanticConvention.GEN_AI_OPERATION_TYPE_REALTIME,
+ "gpt-4o-realtime-preview",
+)
diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.py
index 6aab926a..03279e8e 100644
--- a/sdk/python/src/openlit/instrumentation/openai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py
@@ -928,6 +928,27 @@ def common_response_logic(
SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
)
+ # Built-in tool attributes (Responses API)
+ if hasattr(scope, "_built_in_tools") and scope._built_in_tools:
+ tool_types = [t["type"] for t in scope._built_in_tools]
+ scope._span.set_attribute(
+ SemanticConvention.OPENAI_RESPONSE_BUILT_IN_TOOLS,
+ ", ".join(tool_types),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.OPENAI_RESPONSE_BUILT_IN_TOOL_COUNT,
+ len(scope._built_in_tools),
+ )
+
+ if (
+ hasattr(scope, "_web_search_citations_count")
+ and scope._web_search_citations_count > 0
+ ):
+ scope._span.set_attribute(
+ SemanticConvention.OPENAI_RESPONSE_WEB_SEARCH_CITATIONS_COUNT,
+ scope._web_search_citations_count,
+ )
+
# Span Attributes for Cost and Tokens
scope._span.set_attribute(
SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
@@ -1134,6 +1155,53 @@ def process_response_response(
if content and len(content) > 0:
scope._llmresponse = content[0].get("text", "")
+ # Track built-in tool calls from Responses API
+ built_in_tools = []
+ web_search_citations_count = 0
+ for item in output:
+ item_type = item.get("type", "")
+ if item_type == "web_search_call":
+ built_in_tools.append(
+ {
+ "type": "web_search",
+ "id": item.get("id", ""),
+ "status": item.get("status", ""),
+ }
+ )
+ elif item_type == "file_search_call":
+ results = item.get("results", [])
+ built_in_tools.append(
+ {
+ "type": "file_search",
+ "id": item.get("id", ""),
+ "status": item.get("status", ""),
+ "results_count": len(results),
+ }
+ )
+ elif item_type == "code_interpreter_call":
+ built_in_tools.append(
+ {
+ "type": "code_interpreter",
+ "id": item.get("id", ""),
+ "status": item.get("status", ""),
+ }
+ )
+ elif item_type == "computer_use_call":
+ built_in_tools.append(
+ {
+ "type": "computer_use",
+ "id": item.get("id", ""),
+ "status": item.get("status", ""),
+ }
+ )
+ elif item_type == "message":
+ for content_part in item.get("content", []):
+ for annotation in content_part.get("annotations", []):
+ if annotation.get("type") == "url_citation":
+ web_search_citations_count += 1
+ scope._built_in_tools = built_in_tools
+ scope._web_search_citations_count = web_search_citations_count
+
scope._response_id = response_dict.get("id", "")
scope._response_model = response_dict.get("model", "")
@@ -2153,3 +2221,514 @@ def process_image_response(
)
return response
+
+
+def common_transcription_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ event_provider=None,
+):
+ """Common logic for processing audio transcription/translation operations."""
+
+ cost = get_audio_model_cost(
+ request_model, pricing_info, None, duration=getattr(scope, "_duration", 0)
+ )
+
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
+
+ language = scope._kwargs.get("language")
+ if language:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_LANGUAGE, language
+ )
+ response_format = scope._kwargs.get("response_format")
+ if response_format:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,
+ str(response_format),
+ )
+ temperature = handle_not_given(scope._kwargs.get("temperature"))
+ if temperature is not None:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, temperature
+ )
+
+ if hasattr(scope, "_duration") and scope._duration:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_AUDIO_DURATION, scope._duration
+ )
+
+ if hasattr(scope, "_input_tokens") and scope._input_tokens:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
+
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ if capture_message_content:
+ file_obj = scope._kwargs.get("file")
+ file_name = getattr(file_obj, "name", "unknown") if file_obj else "unknown"
+ input_msgs = [
+ {
+ "role": "user",
+ "parts": [{"type": "text", "content": f"[audio file: {file_name}]"}],
+ }
+ ]
+ output_msgs = None
+ if hasattr(scope, "_transcription_text") and scope._transcription_text:
+ output_msgs = [
+ {
+ "role": "assistant",
+ "parts": [{"type": "text", "content": scope._transcription_text}],
+ "finish_reason": "stop",
+ }
+ ]
+ _set_span_messages_as_array(scope._span, input_msgs, output_msgs)
+
+ if event_provider:
+ try:
+ emit_inference_event(
+ event_provider=event_provider,
+ operation_name=SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT,
+ request_model=request_model,
+ response_model=request_model,
+ input_messages=input_msgs,
+ output_messages=output_msgs,
+ server_address=scope._server_address,
+ server_port=scope._server_port,
+ )
+ except Exception as e:
+ logger.warning("Failed to emit inference event: %s", e, exc_info=True)
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ if not disable_metrics:
+ record_audio_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_SPEECH_TO_TEXT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ )
+
+
+def process_transcription_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ end_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ event_provider=None,
+ **kwargs,
+):
+ """Process audio transcription response and generate telemetry."""
+
+ scope = type("GenericScope", (), {})()
+ response_dict = response_as_dict(response)
+
+ scope._start_time = start_time
+ scope._end_time = end_time
+ scope._span = span
+ scope._timestamps = []
+ scope._ttft, scope._tbt = end_time - start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+
+ usage = response_dict.get("usage", {})
+ if isinstance(usage, dict):
+ usage_type = usage.get("type", "duration")
+ if usage_type == "tokens":
+ scope._input_tokens = usage.get("input_tokens", 0)
+ scope._output_tokens = usage.get("output_tokens", 0)
+ else:
+ scope._input_tokens = 0
+ scope._output_tokens = 0
+ else:
+ scope._input_tokens = 0
+ scope._output_tokens = 0
+
+ scope._transcription_text = response_dict.get("text", "")
+ scope._language = response_dict.get("language", "")
+ scope._duration = response_dict.get("duration", 0)
+
+ common_transcription_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ event_provider=event_provider,
+ )
+
+ return response
+
+
+def process_moderation_response(
+ response,
+ request_model,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ end_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ event_provider=None,
+ **kwargs,
+):
+ """Process moderation response and generate telemetry."""
+
+ scope = type("GenericScope", (), {})()
+ response_dict = response_as_dict(response)
+
+ scope._start_time = start_time
+ scope._end_time = end_time
+ scope._span = span
+ scope._timestamps = []
+ scope._ttft, scope._tbt = end_time - start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_MODERATION,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ server_address,
+ server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._ttft,
+ version,
+ )
+
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_ID, response_dict.get("id", "")
+ )
+
+ results = response_dict.get("results", [])
+ if results:
+ first_result = results[0]
+ flagged = first_result.get("flagged", False)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_MODERATION_FLAGGED, flagged)
+
+ categories = first_result.get("categories", {})
+ flagged_categories = [cat for cat, val in categories.items() if val]
+ if flagged_categories:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_MODERATION_CATEGORIES,
+ json.dumps(flagged_categories),
+ )
+
+ category_scores = first_result.get("category_scores", {})
+ if flagged_categories:
+ top_scores = {
+ cat: category_scores.get(cat, 0) for cat in flagged_categories
+ }
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_MODERATION_CATEGORY_SCORES,
+ json.dumps(top_scores),
+ )
+
+ if capture_message_content:
+ input_data = kwargs.get("input", "")
+ if isinstance(input_data, str):
+ input_msgs = [
+ {"role": "user", "parts": [{"type": "text", "content": input_data}]}
+ ]
+ elif isinstance(input_data, list):
+ input_msgs = [
+ {"role": "user", "parts": [{"type": "text", "content": str(item)}]}
+ for item in input_data
+ ]
+ else:
+ input_msgs = []
+ _set_span_messages_as_array(scope._span, input_msgs, None)
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ if not disable_metrics:
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_MODERATION,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ server_address,
+ server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ 0,
+ 0,
+ 0,
+ None,
+ None,
+ )
+
+ return response
+
+
+def process_lightweight_response(
+ response,
+ operation_type,
+ request_model,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ end_time,
+ span,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
+ """
+ Generic lightweight processor for infrastructure/CRUD APIs.
+ Used by batch, fine-tuning, vector stores, files, video, conversations,
+ responses.retrieve/cancel, and chat messages list endpoints.
+ Extracts response data and sets span attributes.
+ """
+
+ scope = type("GenericScope", (), {})()
+ response_dict = response_as_dict(response)
+
+ scope._start_time = start_time
+ scope._end_time = end_time
+ scope._span = span
+ scope._timestamps = []
+ scope._ttft, scope._tbt = end_time - start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+
+ common_span_attributes(
+ scope,
+ operation_type,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ server_address,
+ server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._ttft,
+ version,
+ )
+
+ response_id = response_dict.get("id", "")
+ if response_id:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, response_id)
+
+ status = response_dict.get("status", "")
+ if status:
+ scope._span.set_attribute("gen_ai.response.status", status)
+
+ resp_model = response_dict.get("model", "")
+ if resp_model:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, resp_model)
+
+ if operation_type == SemanticConvention.GEN_AI_OPERATION_TYPE_BATCH:
+ if response_dict.get("endpoint"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_BATCH_ENDPOINT,
+ response_dict["endpoint"],
+ )
+ if response_dict.get("input_file_id"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_BATCH_INPUT_FILE_ID,
+ response_dict["input_file_id"],
+ )
+ if response_dict.get("output_file_id"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_BATCH_OUTPUT_FILE_ID,
+ response_dict["output_file_id"],
+ )
+ request_counts = response_dict.get("request_counts", {})
+ if request_counts:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_BATCH_REQUEST_COUNTS_TOTAL,
+ request_counts.get("total", 0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_BATCH_REQUEST_COUNTS_COMPLETED,
+ request_counts.get("completed", 0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_BATCH_REQUEST_COUNTS_FAILED,
+ request_counts.get("failed", 0),
+ )
+ usage = response_dict.get("usage", {})
+ if usage:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
+ usage.get("input_tokens", 0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
+ usage.get("output_tokens", 0),
+ )
+
+ elif operation_type == SemanticConvention.GEN_AI_OPERATION_TYPE_FINE_TUNING:
+ if response_dict.get("fine_tuned_model"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FINE_TUNING_FINE_TUNED_MODEL,
+ response_dict["fine_tuned_model"],
+ )
+ if response_dict.get("training_file"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FINE_TUNING_TRAINING_FILE,
+ response_dict["training_file"],
+ )
+ if response_dict.get("trained_tokens"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FINE_TUNING_TRAINED_TOKENS,
+ response_dict["trained_tokens"],
+ )
+ hyperparams = response_dict.get("hyperparameters", {})
+ if hyperparams:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FINE_TUNING_HYPERPARAMETERS,
+ json.dumps(hyperparams),
+ )
+
+ elif operation_type == SemanticConvention.GEN_AI_OPERATION_TYPE_FILE:
+ if response_dict.get("filename"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FILE_FILENAME,
+ response_dict["filename"],
+ )
+ if response_dict.get("purpose"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FILE_PURPOSE, response_dict["purpose"]
+ )
+ if response_dict.get("bytes"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_FILE_BYTES, response_dict["bytes"]
+ )
+
+ elif operation_type == SemanticConvention.GEN_AI_OPERATION_TYPE_VIDEO:
+ if response_dict.get("size"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_VIDEO_SIZE, response_dict["size"]
+ )
+ if response_dict.get("seconds"):
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_VIDEO_SECONDS,
+ str(response_dict["seconds"]),
+ )
+
+ elif operation_type == SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB:
+ data = response_dict.get("data", [])
+ if data:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_VECTORDB_SEARCH_RESULTS_COUNT, len(data)
+ )
+ scores = [
+ item.get("score", 0) for item in data[:5] if isinstance(item, dict)
+ ]
+ if scores:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_VECTORDB_SEARCH_TOP_SCORES,
+ json.dumps(scores),
+ )
+ search_query = kwargs.get("query", "")
+ if search_query:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_VECTORDB_SEARCH_QUERY, str(search_query)
+ )
+
+ if operation_type == SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT:
+ usage = response_dict.get("usage", {})
+ if usage:
+ input_tokens = usage.get("input_tokens", 0)
+ output_tokens = usage.get("output_tokens", 0)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ if not disable_metrics:
+ record_completion_metrics(
+ metrics,
+ operation_type,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ server_address,
+ server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ 0,
+ 0,
+ 0,
+ None,
+ None,
+ )
+
+ return response
diff --git a/sdk/python/src/openlit/semcov/__init__.py b/sdk/python/src/openlit/semcov/__init__.py
index d342b4a5..372663db 100644
--- a/sdk/python/src/openlit/semcov/__init__.py
+++ b/sdk/python/src/openlit/semcov/__init__.py
@@ -956,6 +956,81 @@ class SemanticConvention:
GEN_AI_BROWSER_CONSOLE_LOGS = "gen_ai.browser.console.logs"
GEN_AI_BROWSER_NETWORK_TIMING = "gen_ai.browser.network.timing"
+ # OpenAI Responses API Built-in Tool Tracking
+ OPENAI_RESPONSE_BUILT_IN_TOOLS = "openai.response.built_in_tools"
+ OPENAI_RESPONSE_BUILT_IN_TOOL_COUNT = "openai.response.built_in_tool.count"
+ OPENAI_RESPONSE_WEB_SEARCH_CITATIONS_COUNT = (
+ "openai.response.web_search.citations_count"
+ )
+
+ # Audio Transcription/Translation (STT) Attributes
+ GEN_AI_REQUEST_AUDIO_LANGUAGE = "gen_ai.request.audio.language"
+ GEN_AI_RESPONSE_AUDIO_DURATION = "gen_ai.response.audio.duration"
+ GEN_AI_RESPONSE_AUDIO_TEXT = "gen_ai.response.audio.text"
+
+ # Moderation API Attributes
+ GEN_AI_OPERATION_TYPE_MODERATION = "moderation"
+ GEN_AI_MODERATION_FLAGGED = "gen_ai.moderation.flagged"
+ GEN_AI_MODERATION_CATEGORIES = "gen_ai.moderation.categories"
+ GEN_AI_MODERATION_CATEGORY_SCORES = "gen_ai.moderation.category_scores"
+
+ # Batch API Attributes
+ GEN_AI_OPERATION_TYPE_BATCH = "batch"
+ GEN_AI_OPENAI_BATCH_ID = "gen_ai.openai.batch.id"
+ GEN_AI_OPENAI_BATCH_STATUS = "gen_ai.openai.batch.status"
+ GEN_AI_OPENAI_BATCH_ENDPOINT = "gen_ai.openai.batch.endpoint"
+ GEN_AI_OPENAI_BATCH_INPUT_FILE_ID = "gen_ai.openai.batch.input_file_id"
+ GEN_AI_OPENAI_BATCH_OUTPUT_FILE_ID = "gen_ai.openai.batch.output_file_id"
+ GEN_AI_OPENAI_BATCH_REQUEST_COUNTS_TOTAL = (
+ "gen_ai.openai.batch.request_counts.total"
+ )
+ GEN_AI_OPENAI_BATCH_REQUEST_COUNTS_COMPLETED = (
+ "gen_ai.openai.batch.request_counts.completed"
+ )
+ GEN_AI_OPENAI_BATCH_REQUEST_COUNTS_FAILED = (
+ "gen_ai.openai.batch.request_counts.failed"
+ )
+ GEN_AI_OPENAI_BATCH_MODEL = "gen_ai.openai.batch.model"
+
+ # Fine-tuning API Attributes
+ GEN_AI_OPERATION_TYPE_FINE_TUNING = "fine_tuning"
+ GEN_AI_OPENAI_FINE_TUNING_JOB_ID = "gen_ai.openai.fine_tuning.job_id"
+ GEN_AI_OPENAI_FINE_TUNING_STATUS = "gen_ai.openai.fine_tuning.status"
+ GEN_AI_OPENAI_FINE_TUNING_MODEL = "gen_ai.openai.fine_tuning.model"
+ GEN_AI_OPENAI_FINE_TUNING_FINE_TUNED_MODEL = (
+ "gen_ai.openai.fine_tuning.fine_tuned_model"
+ )
+ GEN_AI_OPENAI_FINE_TUNING_TRAINING_FILE = "gen_ai.openai.fine_tuning.training_file"
+ GEN_AI_OPENAI_FINE_TUNING_TRAINED_TOKENS = (
+ "gen_ai.openai.fine_tuning.trained_tokens"
+ )
+ GEN_AI_OPENAI_FINE_TUNING_HYPERPARAMETERS = (
+ "gen_ai.openai.fine_tuning.hyperparameters"
+ )
+
+ # Files API Attributes
+ GEN_AI_OPERATION_TYPE_FILE = "file"
+ GEN_AI_OPENAI_FILE_ID = "gen_ai.openai.file.id"
+ GEN_AI_OPENAI_FILE_FILENAME = "gen_ai.openai.file.filename"
+ GEN_AI_OPENAI_FILE_PURPOSE = "gen_ai.openai.file.purpose"
+ GEN_AI_OPENAI_FILE_BYTES = "gen_ai.openai.file.bytes"
+
+ # Video/Sora API Attributes
+ GEN_AI_OPERATION_TYPE_VIDEO = "video"
+ GEN_AI_OPENAI_VIDEO_ID = "gen_ai.openai.video.id"
+ GEN_AI_OPENAI_VIDEO_STATUS = "gen_ai.openai.video.status"
+ GEN_AI_OPENAI_VIDEO_MODEL = "gen_ai.openai.video.model"
+ GEN_AI_OPENAI_VIDEO_SIZE = "gen_ai.openai.video.size"
+ GEN_AI_OPENAI_VIDEO_SECONDS = "gen_ai.openai.video.seconds"
+
+ # Realtime API Attributes
+ GEN_AI_OPERATION_TYPE_REALTIME = "realtime"
+ GEN_AI_OPENAI_REALTIME_SESSION_ID = "gen_ai.openai.realtime.session_id"
+
+ # Conversations API Attributes
+ GEN_AI_OPERATION_TYPE_CONVERSATION = "conversation"
+ GEN_AI_OPENAI_CONVERSATION_ID = "gen_ai.openai.conversation.id"
+
# Guard
GUARD_REQUESTS = "guard.requests"
GUARD_VERDICT = "guard.verdict"
diff --git a/sdk/python/tests/requirements.txt b/sdk/python/tests/requirements.txt
index c0364637..8e446b66 100644
--- a/sdk/python/tests/requirements.txt
+++ b/sdk/python/tests/requirements.txt
@@ -25,6 +25,7 @@ together
transformers
groq
elevenlabs
+crewai>=1.10.0; python_version >= "3.10"
agno
# Vector DBs
diff --git a/sdk/python/tests/test_agent_framework.py b/sdk/python/tests/test_agent_framework.py
index 2e8af706..67066e82 100644
--- a/sdk/python/tests/test_agent_framework.py
+++ b/sdk/python/tests/test_agent_framework.py
@@ -91,8 +91,7 @@ class TestOperationMap:
def test_agent_run_maps_to_invoke_agent(self):
assert (
- OPERATION_MAP["agent_run"]
- == SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT
+ OPERATION_MAP["agent_run"] == SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT
)
def test_workflow_run_maps_to_invoke_workflow(self):
@@ -143,9 +142,7 @@ class TestSpanNameGeneration:
def test_agent_run_span_name(self):
instance = MagicMock()
instance.name = "weather_agent"
- assert (
- generate_span_name("agent_run", instance) == "invoke_agent weather_agent"
- )
+ assert generate_span_name("agent_run", instance) == "invoke_agent weather_agent"
def test_agent_run_fallback_to_id(self):
instance = MagicMock(spec=["id"])
@@ -374,7 +371,8 @@ class TestToolAttributes:
tool.description = None
_set_tool_attributes(
- span, tool,
+ span,
+ tool,
capture_message_content=True,
tool_call_id="call-123",
arguments={"query": "weather"},
@@ -385,10 +383,7 @@ class TestToolAttributes:
SemanticConvention.GEN_AI_TOOL_CALL_ID, "call-123"
)
# Check arguments and result are JSON
- call_args = {
- c[0][0]: c[0][1]
- for c in span.set_attribute.call_args_list
- }
+ call_args = {c[0][0]: c[0][1] for c in span.set_attribute.call_args_list}
assert SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS in call_args
assert SemanticConvention.GEN_AI_TOOL_CALL_RESULT in call_args
@@ -399,7 +394,8 @@ class TestToolAttributes:
tool.description = None
_set_tool_attributes(
- span, tool,
+ span,
+ tool,
capture_message_content=False,
arguments={"query": "test"},
result="some result",
@@ -652,12 +648,13 @@ class TestMetricsRecording:
def test_skips_unknown_model(self):
metrics = {"genai_client_operation_duration": MagicMock()}
- _record_metrics(
- metrics, "invoke_agent", 1.0, "dev", "app", "unknown", "", 0
+ _record_metrics(metrics, "invoke_agent", 1.0, "dev", "app", "unknown", "", 0)
+ call_attrs = (
+ metrics["genai_client_operation_duration"]
+ .record.call_args[1]
+ .get("attributes")
+ or metrics["genai_client_operation_duration"].record.call_args[0][1]
)
- call_attrs = metrics["genai_client_operation_duration"].record.call_args[1].get(
- "attributes"
- ) or metrics["genai_client_operation_duration"].record.call_args[0][1]
assert SemanticConvention.GEN_AI_REQUEST_MODEL not in call_attrs
def test_no_metrics_dict(self):
@@ -708,9 +705,7 @@ class TestWrapAgentInit:
)
registry = _AgentCreationRegistry()
- wrapper_fn = _wrap_agent_init(
- tracer, "test", "app", True, registry
- )
+ wrapper_fn = _wrap_agent_init(tracer, "test", "app", True, registry)
original_init = MagicMock(return_value=None)
instance = MagicMock()
@@ -740,9 +735,7 @@ class TestWrapAgentInit:
)
registry = _AgentCreationRegistry()
- wrapper_fn = _wrap_agent_init(
- tracer, "test", "app", False, registry
- )
+ wrapper_fn = _wrap_agent_init(tracer, "test", "app", False, registry)
original_init = MagicMock(return_value=None)
instance = MagicMock()
@@ -894,7 +887,9 @@ class TestToolExecuteWrap:
instance.description = "Get weather for a city"
result_coro = wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "call-1", "city": "NYC"},
)
result = await result_coro
@@ -917,16 +912,19 @@ class TestToolExecuteWrap:
instance.description = "Get weather"
await wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "call-42", "city": "SF"},
)
- call_attrs = {
- c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list
- }
+ call_attrs = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
assert call_attrs[SemanticConvention.GEN_AI_TOOL_NAME] == "get_weather"
assert call_attrs[SemanticConvention.GEN_AI_TOOL_CALL_ID] == "call-42"
- assert call_attrs[SemanticConvention.GEN_AI_OPERATION] == SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS
+ assert (
+ call_attrs[SemanticConvention.GEN_AI_OPERATION]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS
+ )
@pytest.mark.asyncio
async def test_captures_arguments_when_enabled(self):
@@ -940,13 +938,13 @@ class TestToolExecuteWrap:
instance.description = None
await wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "call-1", "arguments": {"query": "hello"}},
)
- call_attrs = {
- c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list
- }
+ call_attrs = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
assert SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS in call_attrs
parsed = json.loads(call_attrs[SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS])
assert parsed == {"query": "hello"}
@@ -963,13 +961,13 @@ class TestToolExecuteWrap:
instance.description = None
await wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "call-1"},
)
- call_attrs = {
- c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list
- }
+ call_attrs = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
assert SemanticConvention.GEN_AI_TOOL_CALL_RESULT in call_attrs
@pytest.mark.asyncio
@@ -984,13 +982,13 @@ class TestToolExecuteWrap:
instance.description = None
await wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "call-1", "password": "abc"},
)
- call_attrs = {
- c[0][0] for c in mock_span.set_attribute.call_args_list
- }
+ call_attrs = {c[0][0] for c in mock_span.set_attribute.call_args_list}
assert SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS not in call_attrs
assert SemanticConvention.GEN_AI_TOOL_CALL_RESULT not in call_attrs
@@ -1007,7 +1005,9 @@ class TestToolExecuteWrap:
with pytest.raises(ValueError, match="tool error"):
await wrapper_fn(
- failing_invoke, instance, [],
+ failing_invoke,
+ instance,
+ [],
{"tool_call_id": "call-1"},
)
@@ -1024,8 +1024,16 @@ class TestToolExecuteWrap:
)
registry = _AgentCreationRegistry()
wrapper_fn = tool_execute_wrap(
- "tool_execute", "1.0.0", "prod", "app",
- tracer, {}, False, metrics_mock, False, registry,
+ "tool_execute",
+ "1.0.0",
+ "prod",
+ "app",
+ tracer,
+ {},
+ False,
+ metrics_mock,
+ False,
+ registry,
)
async def fake_invoke(*args, **kwargs):
@@ -1051,13 +1059,13 @@ class TestToolExecuteWrap:
instance.description = None
await wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "c1", "arguments": {"city": "SF"}, "context": None},
)
- call_attrs = {
- c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list
- }
+ call_attrs = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
args_str = call_attrs.get(SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS, "")
assert "context" not in args_str
assert "city" in args_str
@@ -1080,13 +1088,13 @@ class TestToolExecuteWrap:
instance.description = None
await wrapper_fn(
- fake_invoke, instance, [],
+ fake_invoke,
+ instance,
+ [],
{"tool_call_id": "c1"},
)
- call_attrs = {
- c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list
- }
+ call_attrs = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
result_str = call_attrs.get(SemanticConvention.GEN_AI_TOOL_CALL_RESULT, "")
assert "62°F, foggy" in result_str
diff --git a/sdk/python/tests/test_claude_agent_sdk.py b/sdk/python/tests/test_claude_agent_sdk.py
index d8d196e0..0e08d545 100644
--- a/sdk/python/tests/test_claude_agent_sdk.py
+++ b/sdk/python/tests/test_claude_agent_sdk.py
@@ -109,8 +109,9 @@ def _make_tool_use_block(name="Bash", input_data=None, tool_id="toolu_001"):
return block
-def _make_tool_result_block(tool_use_id="toolu_001", content="file1.txt\nfile2.txt",
- is_error=False):
+def _make_tool_result_block(
+ tool_use_id="toolu_001", content="file1.txt\nfile2.txt", is_error=False
+):
block = MagicMock()
type(block).__name__ = "ToolResultBlock"
block.tool_use_id = tool_use_id
@@ -126,6 +127,7 @@ def _make_usage(input_tokens=100, output_tokens=50, cache_read=0, cache_creation
(e.g. cache_write_input_tokens) that break int() coercion.
"""
from types import SimpleNamespace
+
return SimpleNamespace(
input_tokens=input_tokens,
output_tokens=output_tokens,
@@ -176,7 +178,14 @@ def _get_span_attrs(span):
# ===========================================================================
class TestOperationMap:
def test_has_required_endpoints(self):
- for key in ("query", "receive_response", "execute_tool", "subagent", "chat", "create_agent"):
+ for key in (
+ "query",
+ "receive_response",
+ "execute_tool",
+ "subagent",
+ "chat",
+ "create_agent",
+ ):
assert key in OPERATION_MAP, f"Missing endpoint: {key}"
def test_query_maps_to_invoke_agent(self):
@@ -186,24 +195,42 @@ class TestOperationMap:
assert OPERATION_MAP["chat"] == SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT
def test_execute_tool_maps_to_tools(self):
- assert OPERATION_MAP["execute_tool"] == SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS
+ assert (
+ OPERATION_MAP["execute_tool"]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS
+ )
def test_create_agent_maps_correctly(self):
- assert OPERATION_MAP["create_agent"] == SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT
+ assert (
+ OPERATION_MAP["create_agent"]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT
+ )
class TestSpanKindMap:
def test_agent_is_internal(self):
- assert SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT] == SpanKind.INTERNAL
+ assert (
+ SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT]
+ == SpanKind.INTERNAL
+ )
def test_chat_is_client(self):
- assert SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT] == SpanKind.CLIENT
+ assert (
+ SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT]
+ == SpanKind.CLIENT
+ )
def test_tools_is_internal(self):
- assert SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS] == SpanKind.INTERNAL
+ assert (
+ SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS]
+ == SpanKind.INTERNAL
+ )
def test_create_agent_is_client(self):
- assert SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT] == SpanKind.CLIENT
+ assert (
+ SPAN_KIND_MAP[SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT]
+ == SpanKind.CLIENT
+ )
class TestFinishReasonMap:
@@ -259,16 +286,25 @@ class TestSpanNameGeneration:
)
def test_chat_span_name_with_model(self):
- assert generate_span_name("chat", "claude-sonnet-4-20250514") == "chat claude-sonnet-4-20250514"
+ assert (
+ generate_span_name("chat", "claude-sonnet-4-20250514")
+ == "chat claude-sonnet-4-20250514"
+ )
def test_execute_tool_span_name(self):
assert generate_span_name("execute_tool", "Bash") == "execute_tool Bash"
def test_create_agent_span_name(self):
- assert generate_span_name("create_agent", "claude_agent_sdk") == "create_agent claude_agent_sdk"
+ assert (
+ generate_span_name("create_agent", "claude_agent_sdk")
+ == "create_agent claude_agent_sdk"
+ )
def test_subagent_span_name(self):
- assert generate_span_name("subagent", "search_agent") == "invoke_agent search_agent"
+ assert (
+ generate_span_name("subagent", "search_agent")
+ == "invoke_agent search_agent"
+ )
def test_unknown_operation_falls_back(self):
name = generate_span_name("unknown_op")
@@ -287,8 +323,14 @@ class TestSpanNameGeneration:
class TestGetSpanKind:
def test_known_operation_types(self):
- assert get_span_kind(SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT) == SpanKind.CLIENT
- assert get_span_kind(SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT) == SpanKind.INTERNAL
+ assert (
+ get_span_kind(SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
+ == SpanKind.CLIENT
+ )
+ assert (
+ get_span_kind(SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
+ == SpanKind.INTERNAL
+ )
def test_unknown_defaults_to_internal(self):
assert get_span_kind("completely_unknown") == SpanKind.INTERNAL
@@ -299,7 +341,9 @@ class TestGetSpanKind:
# ===========================================================================
class TestExtractUsage:
def test_full_usage_object(self):
- usage = _make_usage(input_tokens=10, output_tokens=50, cache_read=90, cache_creation=5)
+ usage = _make_usage(
+ input_tokens=10, output_tokens=50, cache_read=90, cache_creation=5
+ )
attrs = extract_usage(usage)
assert attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 10 + 90 + 5
@@ -315,9 +359,12 @@ class TestExtractUsage:
def test_no_cache_tokens(self):
from types import SimpleNamespace
+
usage = SimpleNamespace(
- input_tokens=100, output_tokens=50,
- cache_read_input_tokens=None, cache_creation_input_tokens=None,
+ input_tokens=100,
+ output_tokens=50,
+ cache_read_input_tokens=None,
+ cache_creation_input_tokens=None,
)
attrs = extract_usage(usage)
assert attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 100
@@ -343,9 +390,12 @@ class TestExtractUsage:
def test_non_numeric_handled_gracefully(self):
from types import SimpleNamespace
+
usage = SimpleNamespace(
- input_tokens="not_a_number", output_tokens="bad",
- cache_read_input_tokens=None, cache_creation_input_tokens=None,
+ input_tokens="not_a_number",
+ output_tokens="bad",
+ cache_read_input_tokens=None,
+ cache_creation_input_tokens=None,
)
attrs = extract_usage(usage)
assert attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 0
@@ -365,10 +415,12 @@ class TestMapFinishReason:
def test_none_defaults_to_stop(self):
from openlit.instrumentation.claude_agent_sdk.utils import _map_finish_reason
+
assert _map_finish_reason(None) == "stop"
def test_unknown_passed_through(self):
from openlit.instrumentation.claude_agent_sdk.utils import _map_finish_reason
+
assert _map_finish_reason("custom_reason") == "custom_reason"
@@ -422,9 +474,11 @@ class TestHasMeaningfulContent:
# ===========================================================================
class TestBuildInputFromToolResults:
def test_builds_tool_call_response_parts(self):
- msg = _make_user_message(content=[
- _make_tool_result_block("toolu_001", "result_data"),
- ])
+ msg = _make_user_message(
+ content=[
+ _make_tool_result_block("toolu_001", "result_data"),
+ ]
+ )
result = build_input_from_tool_results(msg)
assert result is not None
@@ -435,10 +489,12 @@ class TestBuildInputFromToolResults:
assert "result_data" in result[0]["parts"][0]["response"]
def test_multiple_tool_results(self):
- msg = _make_user_message(content=[
- _make_tool_result_block("toolu_001", "res1"),
- _make_tool_result_block("toolu_002", "res2"),
- ])
+ msg = _make_user_message(
+ content=[
+ _make_tool_result_block("toolu_001", "res1"),
+ _make_tool_result_block("toolu_002", "res2"),
+ ]
+ )
result = build_input_from_tool_results(msg)
assert len(result[0]["parts"]) == 2
@@ -469,7 +525,9 @@ class TestSetChatSpanAttributes:
content=[_make_text_block("Hello!")],
)
set_chat_span_attributes(
- span, message, capture,
+ span,
+ message,
+ capture,
environment="test",
application_name="test_app",
version="0.1.0",
@@ -481,8 +539,14 @@ class TestSetChatSpanAttributes:
def test_required_attributes_present(self):
_, attrs = self._call()
- assert attrs[SemanticConvention.GEN_AI_OPERATION] == SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT
- assert attrs[SemanticConvention.GEN_AI_PROVIDER_NAME] == SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC
+ assert (
+ attrs[SemanticConvention.GEN_AI_OPERATION]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT
+ )
+ assert (
+ attrs[SemanticConvention.GEN_AI_PROVIDER_NAME]
+ == SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC
+ )
def test_gen_ai_system_set(self):
_, attrs = self._call()
@@ -495,8 +559,13 @@ class TestSetChatSpanAttributes:
def test_model_attributes(self):
_, attrs = self._call()
- assert attrs[SemanticConvention.GEN_AI_REQUEST_MODEL] == "claude-sonnet-4-20250514"
- assert attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL] == "claude-sonnet-4-20250514"
+ assert (
+ attrs[SemanticConvention.GEN_AI_REQUEST_MODEL] == "claude-sonnet-4-20250514"
+ )
+ assert (
+ attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL]
+ == "claude-sonnet-4-20250514"
+ )
def test_usage_tokens(self):
_, attrs = self._call()
@@ -568,7 +637,9 @@ class TestSetChatSpanAttributes:
# ===========================================================================
class TestOutputMessageSchema:
def test_text_output_follows_parts_schema(self):
- from openlit.instrumentation.claude_agent_sdk.utils import _build_output_messages
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _build_output_messages,
+ )
msg = _make_assistant_message(content=[_make_text_block("Hello!")])
result = _build_output_messages(msg, "stop")
@@ -580,11 +651,15 @@ class TestOutputMessageSchema:
assert result[0]["parts"][0]["content"] == "Hello!"
def test_tool_call_output_follows_parts_schema(self):
- from openlit.instrumentation.claude_agent_sdk.utils import _build_output_messages
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _build_output_messages,
+ )
- msg = _make_assistant_message(content=[
- _make_tool_use_block("Bash", {"command": "ls"}, "toolu_001"),
- ])
+ msg = _make_assistant_message(
+ content=[
+ _make_tool_use_block("Bash", {"command": "ls"}, "toolu_001"),
+ ]
+ )
result = _build_output_messages(msg, "tool_call")
tc_part = result[0]["parts"][0]
@@ -596,12 +671,16 @@ class TestOutputMessageSchema:
def test_reasoning_output_uses_correct_type(self):
"""OTel ReasoningPart requires type='reasoning', not 'thinking'."""
- from openlit.instrumentation.claude_agent_sdk.utils import _build_output_messages
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _build_output_messages,
+ )
- msg = _make_assistant_message(content=[
- _make_thinking_block("Let me analyze..."),
- _make_text_block("The answer is 42."),
- ])
+ msg = _make_assistant_message(
+ content=[
+ _make_thinking_block("Let me analyze..."),
+ _make_text_block("The answer is 42."),
+ ]
+ )
result = _build_output_messages(msg, "stop")
parts = result[0]["parts"]
@@ -612,13 +691,17 @@ class TestOutputMessageSchema:
assert text_part["type"] == "text"
def test_mixed_content_output(self):
- from openlit.instrumentation.claude_agent_sdk.utils import _build_output_messages
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _build_output_messages,
+ )
- msg = _make_assistant_message(content=[
- _make_thinking_block("Thinking..."),
- _make_text_block("Here's the plan:"),
- _make_tool_use_block("Bash", {"command": "ls"}, "toolu_x"),
- ])
+ msg = _make_assistant_message(
+ content=[
+ _make_thinking_block("Thinking..."),
+ _make_text_block("Here's the plan:"),
+ _make_tool_use_block("Bash", {"command": "ls"}, "toolu_x"),
+ ]
+ )
result = _build_output_messages(msg, "tool_call")
parts = result[0]["parts"]
@@ -628,7 +711,9 @@ class TestOutputMessageSchema:
assert parts[2]["type"] == "tool_call"
def test_empty_content_returns_none(self):
- from openlit.instrumentation.claude_agent_sdk.utils import _build_output_messages
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _build_output_messages,
+ )
msg = _make_assistant_message(content=[])
assert _build_output_messages(msg, "stop") is None
@@ -638,18 +723,28 @@ class TestOutputMessageSchema:
# Tool Span Attributes
# ===========================================================================
class TestSetToolSpanAttributes:
- def _call(self, tool_name="Bash", tool_input=None, tool_use_id="toolu_001",
- capture=True):
+ def _call(
+ self, tool_name="Bash", tool_input=None, tool_use_id="toolu_001", capture=True
+ ):
span = MagicMock()
set_tool_span_attributes(
- span, tool_name, tool_input or {"command": "ls"}, tool_use_id,
- capture, "test", "test_app", "0.1.0",
+ span,
+ tool_name,
+ tool_input or {"command": "ls"},
+ tool_use_id,
+ capture,
+ "test",
+ "test_app",
+ "0.1.0",
)
return span, _get_span_attrs(span)
def test_operation_name_is_execute_tool(self):
_, attrs = self._call()
- assert attrs[SemanticConvention.GEN_AI_OPERATION] == SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS
+ assert (
+ attrs[SemanticConvention.GEN_AI_OPERATION]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS
+ )
def test_gen_ai_system_set(self):
_, attrs = self._call()
@@ -708,8 +803,11 @@ class TestFinalizeToolSpan:
def test_error_sets_error_type_and_status(self):
span = MagicMock()
finalize_tool_span(
- span, None, capture_message_content=True,
- is_error=True, error_message="Permission denied",
+ span,
+ None,
+ capture_message_content=True,
+ is_error=True,
+ error_message="Permission denied",
)
attrs = _get_span_attrs(span)
@@ -727,7 +825,10 @@ class TestSetCreateAgentAttributes:
set_create_agent_attributes(span, "0.1.0", "test", "test_app")
attrs = _get_span_attrs(span)
- assert attrs[SemanticConvention.GEN_AI_OPERATION] == SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT
+ assert (
+ attrs[SemanticConvention.GEN_AI_OPERATION]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT
+ )
assert attrs[GEN_AI_SYSTEM_ATTR] == "anthropic"
assert attrs[SemanticConvention.GEN_AI_AGENT_NAME] == "claude_agent_sdk"
assert attrs[SemanticConvention.SERVER_ADDRESS] == "api.anthropic.com"
@@ -746,26 +847,43 @@ class TestSetCreateAgentAttributes:
# Root Span (invoke_agent) Attributes
# ===========================================================================
class TestSetInitialSpanAttributes:
- def _call(self, model="claude-sonnet-4-20250514", prompt="Hello", capture=True,
- system_prompt=None):
+ def _call(
+ self,
+ model="claude-sonnet-4-20250514",
+ prompt="Hello",
+ capture=True,
+ system_prompt=None,
+ ):
span = MagicMock()
options = MagicMock()
options.model = model
options.system_prompt = system_prompt
set_initial_span_attributes(
- span, 1000.0, "0.1.0", "test", "test_app",
- options=options, prompt=prompt, capture_message_content=capture,
+ span,
+ 1000.0,
+ "0.1.0",
+ "test",
+ "test_app",
+ options=options,
+ prompt=prompt,
+ capture_message_content=capture,
)
return span, _get_span_attrs(span)
def test_operation_name(self):
_, attrs = self._call()
- assert attrs[SemanticConvention.GEN_AI_OPERATION] == SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT
+ assert (
+ attrs[SemanticConvention.GEN_AI_OPERATION]
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT
+ )
def test_provider_name(self):
_, attrs = self._call()
- assert attrs[SemanticConvention.GEN_AI_PROVIDER_NAME] == SemanticConvention.GEN_AI_SYSTEM_CLAUDE_AGENT_SDK
+ assert (
+ attrs[SemanticConvention.GEN_AI_PROVIDER_NAME]
+ == SemanticConvention.GEN_AI_SYSTEM_CLAUDE_AGENT_SDK
+ )
def test_gen_ai_system(self):
_, attrs = self._call()
@@ -780,8 +898,14 @@ class TestSetInitialSpanAttributes:
options = MagicMock()
options.model = None
set_initial_span_attributes(
- span, 1000.0, "0.1.0", "test", "test_app",
- options=options, prompt="Hi", capture_message_content=False,
+ span,
+ 1000.0,
+ "0.1.0",
+ "test",
+ "test_app",
+ options=options,
+ prompt="Hi",
+ capture_message_content=False,
)
attrs = _get_span_attrs(span)
assert attrs[SemanticConvention.GEN_AI_AGENT_NAME] == (
@@ -790,7 +914,9 @@ class TestSetInitialSpanAttributes:
def test_request_model(self):
_, attrs = self._call(model="claude-sonnet-4-20250514")
- assert attrs[SemanticConvention.GEN_AI_REQUEST_MODEL] == "claude-sonnet-4-20250514"
+ assert (
+ attrs[SemanticConvention.GEN_AI_REQUEST_MODEL] == "claude-sonnet-4-20250514"
+ )
def test_input_messages_parts_schema(self):
"""gen_ai.input.messages must use [{role, parts}] schema."""
@@ -809,7 +935,9 @@ class TestSetInitialSpanAttributes:
def test_system_instructions_json_schema(self):
"""gen_ai.system_instructions must follow [{'type': 'text', 'content': '...'}]."""
- _, attrs = self._call(system_prompt="You are a helpful assistant.", capture=True)
+ _, attrs = self._call(
+ system_prompt="You are a helpful assistant.", capture=True
+ )
raw = attrs.get(SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS)
assert raw is not None
parsed = json.loads(raw)
@@ -821,12 +949,19 @@ class TestSetInitialSpanAttributes:
class TestUpdateRootFromAssistant:
def test_sets_model_and_session(self):
span = MagicMock()
- msg = _make_assistant_message(model="claude-sonnet-4-20250514", session_id="sess_x")
+ msg = _make_assistant_message(
+ model="claude-sonnet-4-20250514", session_id="sess_x"
+ )
update_root_from_assistant(span, msg)
attrs = _get_span_attrs(span)
- assert attrs[SemanticConvention.GEN_AI_REQUEST_MODEL] == "claude-sonnet-4-20250514"
- assert attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL] == "claude-sonnet-4-20250514"
+ assert (
+ attrs[SemanticConvention.GEN_AI_REQUEST_MODEL] == "claude-sonnet-4-20250514"
+ )
+ assert (
+ attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL]
+ == "claude-sonnet-4-20250514"
+ )
assert attrs[SemanticConvention.GEN_AI_CONVERSATION_ID] == "sess_x"
def test_handles_none_model_gracefully(self):
@@ -851,9 +986,13 @@ class TestProcessResultMessage:
assert attrs[SemanticConvention.GEN_AI_CONVERSATION_ID] == "sess_abc"
def test_usage_extracted(self):
- usage = _make_usage(input_tokens=100, output_tokens=50, cache_read=200, cache_creation=50)
+ usage = _make_usage(
+ input_tokens=100, output_tokens=50, cache_read=200, cache_creation=50
+ )
_, attrs, result_usage = self._call(usage=usage)
- assert attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 350 # 100 + 200 + 50
+ assert (
+ attrs[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 350
+ ) # 100 + 200 + 50
assert attrs[SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS] == 50
assert result_usage["input_tokens"] == 350
assert result_usage["output_tokens"] == 50
@@ -864,7 +1003,10 @@ class TestProcessResultMessage:
def test_response_model_from_model_usage(self):
_, attrs, _ = self._call(model_usage={"claude-sonnet-4-20250514": {}})
- assert attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL] == "claude-sonnet-4-20250514"
+ assert (
+ attrs[SemanticConvention.GEN_AI_RESPONSE_MODEL]
+ == "claude-sonnet-4-20250514"
+ )
def test_num_turns_set(self):
_, attrs, _ = self._call(num_turns=3)
@@ -937,8 +1079,14 @@ class TestFinalizeSpan:
}
span = MagicMock()
finalize_span(
- span, 1000.0, metrics, False, "test", "test_app",
- input_tokens=500, output_tokens=200,
+ span,
+ 1000.0,
+ metrics,
+ False,
+ "test",
+ "test_app",
+ input_tokens=500,
+ output_tokens=200,
)
assert metrics["genai_client_usage_tokens"].record.call_count == 2
@@ -955,8 +1103,14 @@ class TestFinalizeSpan:
}
span = MagicMock()
finalize_span(
- span, 1000.0, metrics, False, "test", "test_app",
- input_tokens=0, output_tokens=0,
+ span,
+ 1000.0,
+ metrics,
+ False,
+ "test",
+ "test_app",
+ input_tokens=0,
+ output_tokens=0,
)
metrics["genai_client_usage_tokens"].record.assert_not_called()
@@ -967,10 +1121,12 @@ class TestFinalizeSpan:
class TestCostCalculation:
def test_no_pricing_returns_zero(self):
from openlit.instrumentation.claude_agent_sdk.utils import _calculate_cost
+
assert _calculate_cost("claude-sonnet-4-20250514", None, 100, 50) == 0
def test_no_model_returns_zero(self):
from openlit.instrumentation.claude_agent_sdk.utils import _calculate_cost
+
assert _calculate_cost(None, {"some": "info"}, 100, 50) == 0
@@ -979,7 +1135,9 @@ class TestCostCalculation:
# ===========================================================================
class TestEmitChatInferenceEvent:
def test_emits_event(self):
- from openlit.instrumentation.claude_agent_sdk.utils import _emit_chat_inference_event
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _emit_chat_inference_event,
+ )
event_provider = MagicMock()
_emit_chat_inference_event(
@@ -995,7 +1153,9 @@ class TestEmitChatInferenceEvent:
event_provider.emit.assert_called_once()
def test_no_event_when_provider_is_none(self):
- from openlit.instrumentation.claude_agent_sdk.utils import _emit_chat_inference_event
+ from openlit.instrumentation.claude_agent_sdk.utils import (
+ _emit_chat_inference_event,
+ )
_emit_chat_inference_event(None, "model", "id", "sess", "stop", {}, None, None)
@@ -1005,13 +1165,19 @@ class TestEmitChatInferenceEvent:
# ===========================================================================
class TestToolSpanTracker:
def _make_tracker(self):
- from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import _ToolSpanTracker
+ from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import (
+ _ToolSpanTracker,
+ )
tracer = MagicMock()
tracer.start_span.return_value = MagicMock()
parent_span = MagicMock()
return _ToolSpanTracker(
- tracer, parent_span, "0.1.0", "test", "test_app",
+ tracer,
+ parent_span,
+ "0.1.0",
+ "test",
+ "test_app",
capture_message_content=True,
)
@@ -1059,17 +1225,27 @@ class TestToolSpanTracker:
class TestSubagentSpanTracker:
def _make_tracker(self):
from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import (
- _SubagentSpanTracker, _ToolSpanTracker,
+ _SubagentSpanTracker,
+ _ToolSpanTracker,
)
tracer = MagicMock()
tracer.start_span.return_value = MagicMock()
parent_span = MagicMock()
tool_tracker = _ToolSpanTracker(
- tracer, parent_span, "0.1.0", "test", "test_app", True,
+ tracer,
+ parent_span,
+ "0.1.0",
+ "test",
+ "test_app",
+ True,
)
return _SubagentSpanTracker(
- tracer, tool_tracker, "0.1.0", "test", "test_app",
+ tracer,
+ tool_tracker,
+ "0.1.0",
+ "test",
+ "test_app",
)
def test_start_and_end_subagent(self):
@@ -1135,13 +1311,16 @@ class TestBufferChatMessage:
from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import (
_buffer_chat_message,
)
+
if chat_state is None:
chat_state = {"last_boundary_ns": 1_000_000}
_buffer_chat_message(message, chat_state)
return chat_state
def test_buffers_message_with_llm_call_data(self):
- msg = _make_assistant_message(usage=_make_usage(), content=[_make_text_block("Hi")])
+ msg = _make_assistant_message(
+ usage=_make_usage(), content=[_make_text_block("Hi")]
+ )
state = self._call(msg)
assert state["pending_chat_msg"] is msg
assert state["pending_chat_msg_id"] == "msg_001"
@@ -1149,7 +1328,9 @@ class TestBufferChatMessage:
def test_replaces_on_same_message_id(self):
msg1 = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Sure!")], message_id="msg_X",
+ usage=_make_usage(),
+ content=[_make_text_block("Sure!")],
+ message_id="msg_X",
)
msg2 = _make_assistant_message(
usage=_make_usage(),
@@ -1177,6 +1358,7 @@ class TestFlushPendingChat:
from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import (
_flush_pending_chat,
)
+
tracer = overrides.pop("tracer", MagicMock())
span = MagicMock()
tracer.start_span.return_value = span
@@ -1197,7 +1379,8 @@ class TestFlushPendingChat:
def test_creates_span_when_buffer_present(self):
msg = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Hello")],
+ usage=_make_usage(),
+ content=[_make_text_block("Hello")],
)
state = {
"last_boundary_ns": 1_000_000,
@@ -1215,7 +1398,9 @@ class TestFlushPendingChat:
span.end.assert_called_once_with(end_time=2_000_000)
def test_clears_buffer_after_flush(self):
- msg = _make_assistant_message(usage=_make_usage(), content=[_make_text_block("Hi")])
+ msg = _make_assistant_message(
+ usage=_make_usage(), content=[_make_text_block("Hi")]
+ )
state = {
"last_boundary_ns": 1_000_000,
"pending_chat_msg": msg,
@@ -1229,7 +1414,9 @@ class TestFlushPendingChat:
assert "pending_end_ns" not in state
def test_updates_last_boundary_ns(self):
- msg = _make_assistant_message(usage=_make_usage(), content=[_make_text_block("Hi")])
+ msg = _make_assistant_message(
+ usage=_make_usage(), content=[_make_text_block("Hi")]
+ )
state = {
"last_boundary_ns": 1_000_000,
"pending_chat_msg": msg,
@@ -1245,8 +1432,12 @@ class TestFlushPendingChat:
tracer.start_span.assert_not_called()
def test_consumes_pending_input(self):
- msg = _make_assistant_message(usage=_make_usage(), content=[_make_text_block("Hi")])
- pending_input = [{"role": "user", "parts": [{"type": "text", "content": "Hello"}]}]
+ msg = _make_assistant_message(
+ usage=_make_usage(), content=[_make_text_block("Hi")]
+ )
+ pending_input = [
+ {"role": "user", "parts": [{"type": "text", "content": "Hello"}]}
+ ]
state = {
"last_boundary_ns": 1_000_000,
"pending_chat_msg": msg,
@@ -1263,16 +1454,27 @@ class TestProcessMessageFlushTriggers:
def _make_deps(self):
from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import (
- _ToolSpanTracker, _SubagentSpanTracker,
+ _ToolSpanTracker,
+ _SubagentSpanTracker,
)
+
tracer = MagicMock()
tracer.start_span.return_value = MagicMock()
parent_span = MagicMock()
tool_tracker = _ToolSpanTracker(
- tracer, parent_span, "0.1.0", "test", "test_app", True,
+ tracer,
+ parent_span,
+ "0.1.0",
+ "test",
+ "test_app",
+ True,
)
subagent_tracker = _SubagentSpanTracker(
- tracer, tool_tracker, "0.1.0", "test", "test_app",
+ tracer,
+ tool_tracker,
+ "0.1.0",
+ "test",
+ "test_app",
)
return tracer, parent_span, tool_tracker, subagent_tracker
@@ -1280,19 +1482,28 @@ class TestProcessMessageFlushTriggers:
from openlit.instrumentation.claude_agent_sdk.claude_agent_sdk import (
_process_message,
)
+
tracer, parent_span, tool_tracker, subagent_tracker = self._make_deps()
if chat_state is None:
chat_state = {"last_boundary_ns": 1_000_000}
result = _process_message(
- message, parent_span, tool_tracker, subagent_tracker,
- capture_message_content=True, tracer=tracer, chat_state=chat_state,
- version="0.1.0", environment="test", application_name="test_app",
+ message,
+ parent_span,
+ tool_tracker,
+ subagent_tracker,
+ capture_message_content=True,
+ tracer=tracer,
+ chat_state=chat_state,
+ version="0.1.0",
+ environment="test",
+ application_name="test_app",
)
return tracer, chat_state, result
def test_assistant_message_buffers_not_flushes(self):
msg = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Hello")],
+ usage=_make_usage(),
+ content=[_make_text_block("Hello")],
)
tracer, state, _ = self._process(msg)
assert "pending_chat_msg" in state
@@ -1300,7 +1511,8 @@ class TestProcessMessageFlushTriggers:
def test_user_message_flushes_pending_chat(self):
buffered_msg = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Planning...")],
+ usage=_make_usage(),
+ content=[_make_text_block("Planning...")],
message_id="msg_A",
)
state = {
@@ -1309,16 +1521,19 @@ class TestProcessMessageFlushTriggers:
"pending_chat_msg_id": "msg_A",
"pending_end_ns": 2_000_000,
}
- user_msg = _make_user_message(content=[
- _make_tool_result_block("toolu_001", "result_data"),
- ])
+ user_msg = _make_user_message(
+ content=[
+ _make_tool_result_block("toolu_001", "result_data"),
+ ]
+ )
tracer, state, _ = self._process(user_msg, chat_state=state)
tracer.start_span.assert_called_once()
assert "pending_chat_msg" not in state
def test_result_message_flushes_pending_chat(self):
buffered_msg = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Summary")],
+ usage=_make_usage(),
+ content=[_make_text_block("Summary")],
message_id="msg_B",
)
state = {
@@ -1333,7 +1548,8 @@ class TestProcessMessageFlushTriggers:
def test_different_message_id_flushes_previous(self):
msg1 = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("First")],
+ usage=_make_usage(),
+ content=[_make_text_block("First")],
message_id="msg_1",
)
state = {
@@ -1343,7 +1559,8 @@ class TestProcessMessageFlushTriggers:
"pending_end_ns": 2_000_000,
}
msg2 = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Second")],
+ usage=_make_usage(),
+ content=[_make_text_block("Second")],
message_id="msg_2",
)
tracer, state, _ = self._process(msg2, chat_state=state)
@@ -1353,7 +1570,8 @@ class TestProcessMessageFlushTriggers:
def test_same_message_id_replaces_buffer(self):
msg1 = _make_assistant_message(
- usage=_make_usage(), content=[_make_text_block("Text only")],
+ usage=_make_usage(),
+ content=[_make_text_block("Text only")],
message_id="msg_same",
)
state = {
@@ -1378,10 +1596,12 @@ class TestProcessMessageFlushTriggers:
class TestInstrumentorImport:
def test_import_instrumentor(self):
from openlit.instrumentation.claude_agent_sdk import ClaudeAgentSDKInstrumentor
+
assert ClaudeAgentSDKInstrumentor is not None
def test_import_utils(self):
from openlit.instrumentation.claude_agent_sdk import utils
+
assert hasattr(utils, "OPERATION_MAP")
assert hasattr(utils, "extract_usage")
assert hasattr(utils, "set_chat_span_attributes")
@@ -1394,6 +1614,7 @@ class TestInstrumentorImport:
wrap_client_query,
wrap_receive_response,
)
+
assert wrap_query is not None
assert wrap_connect is not None
assert wrap_client_query is not None
@@ -1405,9 +1626,11 @@ class TestInstrumentorImport:
# ===========================================================================
class TestInputMessageSchema:
def test_tool_result_follows_otel_schema(self):
- msg = _make_user_message(content=[
- _make_tool_result_block("toolu_001", "file1.txt\nfile2.txt"),
- ])
+ msg = _make_user_message(
+ content=[
+ _make_tool_result_block("toolu_001", "file1.txt\nfile2.txt"),
+ ]
+ )
result = build_input_from_tool_results(msg)
assert result[0]["role"] == "user"
@@ -1417,10 +1640,12 @@ class TestInputMessageSchema:
assert "file1.txt" in part["response"]
def test_multiple_tool_results(self):
- msg = _make_user_message(content=[
- _make_tool_result_block("toolu_a", "res_a"),
- _make_tool_result_block("toolu_b", "res_b"),
- ])
+ msg = _make_user_message(
+ content=[
+ _make_tool_result_block("toolu_a", "res_a"),
+ _make_tool_result_block("toolu_b", "res_b"),
+ ]
+ )
result = build_input_from_tool_results(msg)
parts = result[0]["parts"]
@@ -1441,27 +1666,40 @@ class TestRecordMetrics:
"genai_client_usage_tokens": MagicMock(),
}
_record_metrics(
- metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
- 1.5, "test", "test_app",
- input_tokens=500, output_tokens=200,
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ 1.5,
+ "test",
+ "test_app",
+ input_tokens=500,
+ output_tokens=200,
)
token_calls = metrics["genai_client_usage_tokens"].record.call_args_list
assert len(token_calls) == 2
input_call_attrs = token_calls[0][0][1]
- assert input_call_attrs[SemanticConvention.GEN_AI_TOKEN_TYPE] == SemanticConvention.GEN_AI_TOKEN_TYPE_INPUT
+ assert (
+ input_call_attrs[SemanticConvention.GEN_AI_TOKEN_TYPE]
+ == SemanticConvention.GEN_AI_TOKEN_TYPE_INPUT
+ )
output_call_attrs = token_calls[1][0][1]
- assert output_call_attrs[SemanticConvention.GEN_AI_TOKEN_TYPE] == SemanticConvention.GEN_AI_TOKEN_TYPE_OUTPUT
+ assert (
+ output_call_attrs[SemanticConvention.GEN_AI_TOKEN_TYPE]
+ == SemanticConvention.GEN_AI_TOKEN_TYPE_OUTPUT
+ )
def test_metrics_include_gen_ai_system(self):
from openlit.instrumentation.claude_agent_sdk.utils import _record_metrics
metrics = {"genai_client_operation_duration": MagicMock()}
_record_metrics(
- metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
- 1.0, "test", "test_app",
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ 1.0,
+ "test",
+ "test_app",
)
call_attrs = metrics["genai_client_operation_duration"].record.call_args[0][1]
diff --git a/sdk/python/tests/test_evaluate_rule.py b/sdk/python/tests/test_evaluate_rule.py
index 8aa625d1..30885988 100644
--- a/sdk/python/tests/test_evaluate_rule.py
+++ b/sdk/python/tests/test_evaluate_rule.py
@@ -6,6 +6,7 @@ from unittest.mock import patch, MagicMock
def test_evaluate_rule_exists():
"""evaluate_rule is importable from openlit."""
from openlit import evaluate_rule
+
assert callable(evaluate_rule)
@@ -111,7 +112,9 @@ def test_evaluate_rule_http_error_returns_none(mock_post):
assert result is None
[email protected]("os.environ", {"OPENLIT_URL": "http://env-url:3000", "OPENLIT_API_KEY": "env-key"})
+ "os.environ", {"OPENLIT_URL": "http://env-url:3000", "OPENLIT_API_KEY": "env-key"}
+)
@patch("openlit.requests.post")
def test_evaluate_rule_uses_env_vars(mock_post):
"""Falls back to environment variables for url and api_key."""
diff --git a/sdk/python/tests/test_google_adk.py b/sdk/python/tests/test_google_adk.py
index bc7c279e..7d04a37f 100644
--- a/sdk/python/tests/test_google_adk.py
+++ b/sdk/python/tests/test_google_adk.py
@@ -70,8 +70,14 @@ class TestOperationMap:
assert key in SPAN_KIND_MAP, f"Missing span kind: {key}"
def test_get_operation_type_defaults(self):
- assert get_operation_type("agent_init") == SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT
- assert get_operation_type("unknown_key") == SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT
+ assert (
+ get_operation_type("agent_init")
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT
+ )
+ assert (
+ get_operation_type("unknown_key")
+ == SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT
+ )
def test_runner_endpoints_map_to_invoke_agent(self):
for key in ("runner_run_async", "runner_run", "runner_run_live"):
@@ -99,7 +105,9 @@ class TestSpanNameGeneration:
def test_agent_run_async_span_name(self):
instance = MagicMock()
instance.name = "sub_agent"
- assert generate_span_name("agent_run_async", instance) == "invoke_agent sub_agent"
+ assert (
+ generate_span_name("agent_run_async", instance) == "invoke_agent sub_agent"
+ )
def test_fallback_app_name(self):
instance = MagicMock(spec=[])
@@ -424,7 +432,9 @@ class TestToolSpanEnrichment:
span.set_attribute.assert_any_call(
SemanticConvention.GEN_AI_TOOL_NAME, "get_weather"
)
- set_calls = {call[0][0]: call[0][1] for call in span.set_attribute.call_args_list}
+ set_calls = {
+ call[0][0]: call[0][1] for call in span.set_attribute.call_args_list
+ }
assert SemanticConvention.GEN_AI_TOOL_CALL_ID in set_calls
assert set_calls[SemanticConvention.GEN_AI_TOOL_CALL_ID] == "call_abc"
assert SemanticConvention.GEN_AI_TOOL_CALL_RESULT in set_calls
@@ -460,14 +470,13 @@ class TestToolSpanEnrichment:
tool.description = None
enrich_tool_span(
- span, tool,
+ span,
+ tool,
function_args=None,
function_response_event=None,
capture_message_content=True,
)
- span.set_attribute.assert_any_call(
- SemanticConvention.GEN_AI_TOOL_NAME, "noop"
- )
+ span.set_attribute.assert_any_call(SemanticConvention.GEN_AI_TOOL_NAME, "noop")
class TestMergedToolSpanEnrichment:
@@ -505,7 +514,9 @@ class TestMergedToolSpanEnrichment:
SemanticConvention.GEN_AI_TOOL_CALL_ID, "evt_123"
)
- set_calls = {call[0][0]: call[0][1] for call in span.set_attribute.call_args_list}
+ set_calls = {
+ call[0][0]: call[0][1] for call in span.set_attribute.call_args_list
+ }
assert SemanticConvention.GEN_AI_TOOL_CALL_RESULT in set_calls
result = json.loads(set_calls[SemanticConvention.GEN_AI_TOOL_CALL_RESULT])
assert len(result) == 2
@@ -514,8 +525,12 @@ class TestMergedToolSpanEnrichment:
def test_merged_without_capture(self):
span = MagicMock()
- enrich_merged_tool_span(span, "evt_1", MagicMock(), capture_message_content=False)
- set_calls = {call[0][0]: call[0][1] for call in span.set_attribute.call_args_list}
+ enrich_merged_tool_span(
+ span, "evt_1", MagicMock(), capture_message_content=False
+ )
+ set_calls = {
+ call[0][0]: call[0][1] for call in span.set_attribute.call_args_list
+ }
assert SemanticConvention.GEN_AI_TOOL_CALL_RESULT not in set_calls
@@ -552,9 +567,7 @@ class TestMetricsRecording:
"""Tests for record_google_adk_metrics."""
def test_records_duration(self):
- metrics = {
- "genai_client_operation_duration": MagicMock()
- }
+ metrics = {"genai_client_operation_duration": MagicMock()}
record_google_adk_metrics(
metrics,
"invoke_workflow",
@@ -586,34 +599,51 @@ class TestToolNameExtraction:
def test_callable_name_via_dunder(self):
"""Raw Python functions have __name__, not .name."""
+
def get_weather(city: str) -> dict:
"""Get weather for a city."""
return {}
- t_name = getattr(get_weather, "name", None) or getattr(get_weather, "__name__", None) or type(get_weather).__name__
+ t_name = (
+ getattr(get_weather, "name", None)
+ or getattr(get_weather, "__name__", None)
+ or type(get_weather).__name__
+ )
assert t_name == "get_weather"
def test_lambda_falls_back_to_lambda(self):
def _anon(x):
return x
+
_anon.__name__ = "<lambda>"
- t_name = getattr(_anon, "name", None) or getattr(_anon, "__name__", None) or type(_anon).__name__
+ t_name = (
+ getattr(_anon, "name", None)
+ or getattr(_anon, "__name__", None)
+ or type(_anon).__name__
+ )
assert t_name == "<lambda>"
def test_basetool_style_name(self):
"""BaseTool subclasses have .name."""
tool = MagicMock()
tool.name = "my_tool"
- t_name = getattr(tool, "name", None) or getattr(tool, "__name__", None) or type(tool).__name__
+ t_name = (
+ getattr(tool, "name", None)
+ or getattr(tool, "__name__", None)
+ or type(tool).__name__
+ )
assert t_name == "my_tool"
def test_docstring_as_description(self):
"""Raw callables use __doc__ as description fallback."""
+
def calculate(expression: str) -> dict:
"""Evaluate a math expression."""
return {}
- t_desc = getattr(calculate, "description", None) or getattr(calculate, "__doc__", None)
+ t_desc = getattr(calculate, "description", None) or getattr(
+ calculate, "__doc__", None
+ )
assert "Evaluate a math expression" in t_desc
@@ -622,10 +652,12 @@ class TestInstrumentorImport:
def test_import_instrumentor(self):
from openlit.instrumentation.google_adk import GoogleADKInstrumentor
+
assert GoogleADKInstrumentor is not None
def test_import_utils(self):
from openlit.instrumentation.google_adk import utils
+
assert hasattr(utils, "OPERATION_MAP")
assert hasattr(utils, "_PassthroughTracer")
assert hasattr(utils, "enrich_llm_span")
@@ -636,11 +668,13 @@ class TestInstrumentorImport:
async_runner_wrap,
async_agent_wrap,
)
+
assert async_runner_wrap is not None
assert async_agent_wrap is not None
def test_import_sync_wrappers(self):
from openlit.instrumentation.google_adk.google_adk import sync_runner_wrap
+
assert sync_runner_wrap is not None
@@ -648,7 +682,9 @@ class TestServerInfoNonGoogle:
"""Tests for resolve_server_info with non-Google backends."""
def test_anthropic_model_detection(self):
- addr, _, provider = resolve_server_info(model_name="anthropic/claude-sonnet-4-20250514")
+ addr, _, provider = resolve_server_info(
+ model_name="anthropic/claude-sonnet-4-20250514"
+ )
assert addr == "api.anthropic.com"
assert provider == "anthropic"
@@ -751,7 +787,9 @@ class TestInputMessageSchema:
capture_input_messages(span, llm_request, capture_message_content=True)
call_args = span.set_attribute.call_args_list
- msg_call = [c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_INPUT_MESSAGES]
+ msg_call = [
+ c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_INPUT_MESSAGES
+ ]
assert len(msg_call) == 1
messages = json.loads(msg_call[0][0][1])
assert messages[0]["role"] == "user"
@@ -781,7 +819,9 @@ class TestInputMessageSchema:
capture_input_messages(span, llm_request, capture_message_content=True)
call_args = span.set_attribute.call_args_list
- msg_call = [c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_INPUT_MESSAGES]
+ msg_call = [
+ c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_INPUT_MESSAGES
+ ]
messages = json.loads(msg_call[0][0][1])
tool_part = messages[0]["parts"][0]
assert tool_part["type"] == "tool_call"
@@ -809,7 +849,9 @@ class TestInputMessageSchema:
capture_input_messages(span, llm_request, capture_message_content=True)
call_args = span.set_attribute.call_args_list
- msg_call = [c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_INPUT_MESSAGES]
+ msg_call = [
+ c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_INPUT_MESSAGES
+ ]
messages = json.loads(msg_call[0][0][1])
resp_part = messages[0]["parts"][0]
assert resp_part["type"] == "tool_call_response"
@@ -833,7 +875,9 @@ class TestOutputMessageSchema:
capture_output_messages(span, llm_response, True, "stop")
call_args = span.set_attribute.call_args_list
- msg_call = [c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_OUTPUT_MESSAGES]
+ msg_call = [
+ c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_OUTPUT_MESSAGES
+ ]
assert len(msg_call) == 1
messages = json.loads(msg_call[0][0][1])
assert messages[0]["role"] == "assistant"
@@ -860,7 +904,9 @@ class TestOutputMessageSchema:
capture_output_messages(span, llm_response, True, "tool_calls")
call_args = span.set_attribute.call_args_list
- msg_call = [c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_OUTPUT_MESSAGES]
+ msg_call = [
+ c for c in call_args if c[0][0] == SemanticConvention.GEN_AI_OUTPUT_MESSAGES
+ ]
messages = json.loads(msg_call[0][0][1])
assert messages[0]["finish_reason"] == "tool_calls"
assert messages[0]["parts"][0]["type"] == "tool_call"