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]: Refactor OpenAI OpenTelemetry Monitoring Instrumentation (#785)

Patcherac8da9e5b355
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index b1fa22db..abcfbaef 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.34.21"
+version = "1.34.22"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 5d4a317a..a3c9bcd8 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -402,3 +402,43 @@ def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_ad
metrics["genai_requests"].add(1, attributes)
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_cost"].record(cost, attributes)
+
+def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
+ request_model, response_model, environment, application_name, start_time, end_time, cost):
+ """
+ Record audio-specific metrics for the operation.
+ """
+
+ attributes = create_metrics_attributes(
+ operation=gen_ai_operation,
+ system=gen_ai_system,
+ server_address=server_address,
+ server_port=server_port,
+ request_model=request_model,
+ response_model=response_model,
+ service_name=application_name,
+ deployment_environment=environment,
+ )
+ metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+def record_image_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
+ request_model, response_model, environment, application_name, start_time, end_time, cost):
+ """
+ Record image-specific metrics for the operation.
+ """
+
+ attributes = create_metrics_attributes(
+ operation=gen_ai_operation,
+ system=gen_ai_system,
+ server_address=server_address,
+ server_port=server_port,
+ request_model=request_model,
+ response_model=response_model,
+ service_name=application_name,
+ deployment_environment=environment,
+ )
+ metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_cost"].record(cost, attributes)
diff --git a/sdk/python/src/openlit/instrumentation/openai/__init__.py b/sdk/python/src/openlit/instrumentation/openai/__init__.py
index c4a97274..f71e1319 100644
--- a/sdk/python/src/openlit/instrumentation/openai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/openai/__init__.py
@@ -1,147 +1,142 @@
-# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
"""Initializer of Auto Instrumentation of OpenAI Functions"""
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.openai.openai import chat_completions, embedding, responses, chat_completions_parse
-from openlit.instrumentation.openai.openai import image_generate, image_variatons, audio_create
-from openlit.instrumentation.openai.async_openai import (async_chat_completions, async_embedding,
- async_chat_completions_parse)
-from openlit.instrumentation.openai.async_openai import async_image_generate, async_image_variatons
-from openlit.instrumentation.openai.async_openai import async_audio_create, async_responses
+from openlit.instrumentation.openai.openai import (
+ chat_completions, embedding, responses, chat_completions_parse,
+ image_generate, image_variatons, audio_create
+)
+from openlit.instrumentation.openai.async_openai import (
+ async_chat_completions, async_embedding, async_chat_completions_parse,
+ async_image_generate, async_image_variations, async_audio_create, async_responses
+)
_instruments = ("openai >= 1.92.0",)
class OpenAIInstrumentor(BaseInstrumentor):
- """An instrumentor for OpenAI's client library."""
+ """
+ An instrumentor for OpenAI client library.
+ """
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
def _instrument(self, **kwargs):
- application_name = kwargs.get("application_name")
- environment = kwargs.get("environment")
+ version = importlib.metadata.version("openai")
+ environment = kwargs.get("environment", "default")
+ application_name = kwargs.get("application_name", "default")
tracer = kwargs.get("tracer")
+ pricing_info = kwargs.get("pricing_info", {})
+ capture_message_content = kwargs.get("capture_message_content", False)
metrics = kwargs.get("metrics_dict")
- pricing_info = kwargs.get("pricing_info")
- capture_message_content = kwargs.get("capture_message_content")
disable_metrics = kwargs.get("disable_metrics")
- version = importlib.metadata.version("openai")
+ # 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),
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
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),
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
+ # chat completions parse
wrap_function_wrapper(
- "openai.resources.responses.responses",
- "Responses.create",
- responses(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ "openai.resources.chat.completions",
+ "Completions.parse",
+ chat_completions_parse(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
wrap_function_wrapper(
- "openai.resources.responses.responses",
- "AsyncResponses.create",
- async_responses(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ "openai.resources.chat.completions",
+ "AsyncCompletions.parse",
+ async_chat_completions_parse(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
+ # responses
wrap_function_wrapper(
- "openai.resources.images",
- "Images.generate",
- image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ "openai.resources.responses.responses",
+ "Responses.create",
+ responses(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
wrap_function_wrapper(
- "openai.resources.images",
- "AsyncImages.generate",
- async_image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ "openai.resources.responses.responses",
+ "AsyncResponses.create",
+ async_responses(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
+ # embeddings
wrap_function_wrapper(
"openai.resources.embeddings",
"Embeddings.create",
embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
wrap_function_wrapper(
"openai.resources.embeddings",
"AsyncEmbeddings.create",
async_embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ )
+
+ # 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),
)
+ wrap_function_wrapper(
+ "openai.resources.images",
+ "AsyncImages.generate",
+ async_image_generate(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ )
+
+ # 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),
+ image_variatons(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
wrap_function_wrapper(
"openai.resources.images",
"AsyncImages.create_variation",
- async_image_variatons(version,
- environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ async_image_variations(version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
+ # audio generation
wrap_function_wrapper(
"openai.resources.audio.speech",
"Speech.create",
audio_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics),
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
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),
- )
-
- 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),
- )
-
- 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),
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
- @staticmethod
def _uninstrument(self, **kwargs):
pass
diff --git a/sdk/python/src/openlit/instrumentation/openai/async_openai.py b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
index eaf75c7e..7e07fdfe 100644
--- a/sdk/python/src/openlit/instrumentation/openai/async_openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
@@ -2,78 +2,55 @@
Module for monitoring OpenAI API calls.
"""
-import logging
import time
-from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.trace import SpanKind
from openlit.__helpers import (
- get_chat_model_cost,
- get_embed_model_cost,
- get_audio_model_cost,
- get_image_model_cost,
- general_tokens,
handle_exception,
- extract_and_format_input,
- concatenate_all_contents,
- response_as_dict,
- calculate_ttft,
- calculate_tbt,
- create_metrics_attributes,
set_server_address_and_port
)
+from openlit.instrumentation.openai.utils import (
+ process_chat_chunk,
+ process_response_chunk,
+ process_chat_response,
+ process_streaming_chat_response,
+ process_streaming_response_response,
+ process_response_response,
+ process_embedding_response,
+ process_image_response,
+ process_audio_response,
+)
from openlit.semcov import SemanticConvention
-# Initialize logger for logging potential issues and operations
-logger = logging.getLogger(__name__)
-
-def async_responses(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def async_chat_completions(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for chat completions to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async chat completions.
"""
class TracedAsyncStream:
"""
- Wrapper for streaming responses to collect metrics and trace data.
- Wraps the response to collect message IDs and aggregated response.
-
- This class implements the '__aiter__' and '__anext__' methods that
- handle asynchronous streaming responses.
-
- This class also implements '__aenter__' and '__aexit__' methods that
- handle asynchronous context management protocol.
+ Wrapper for async streaming responses to collect telemetry.
"""
+
def __init__(
self,
wrapped,
span,
+ span_name,
kwargs,
server_address,
server_port,
- **args,
):
self.__wrapped__ = wrapped
self._span = span
- # Placeholder for aggregating streaming response
+ self._span_name = span_name
self._llmresponse = ""
self._response_id = ""
self._response_model = ""
self._finish_reason = ""
- self._input_tokens = ""
- self._output_tokens = ""
-
- self._args = args
+ self._system_fingerprint = ""
+ self._service_tier = "auto"
+ self._tools = None
self._kwargs = kwargs
self._start_time = time.time()
self._end_time = None
@@ -93,390 +70,113 @@ def async_responses(version, environment, application_name,
def __aiter__(self):
return self
- async def __getattr__(self, name):
+ def __getattr__(self, name):
"""Delegate attribute access to the wrapped object."""
- return getattr(await self.__wrapped__, name)
+ return getattr(self.__wrapped__, name)
async def __anext__(self):
try:
chunk = await self.__wrapped__.__anext__()
- end_time = time.time()
- # Record the timestamp for the current chunk
- self._timestamps.append(end_time)
-
- if len(self._timestamps) == 1:
- # Calculate time to first chunk
- self._ttft = calculate_ttft(self._timestamps, self._start_time)
-
- chunked = response_as_dict(chunk)
- # Collect message IDs and aggregated response from events
- if chunked.get('type') == "response.output_text.delta":
- self._llmresponse += chunked.get('delta')
- if chunked.get('type') == "response.completed":
- self._response_id = chunked.get('response').get('id')
- self._response_model = chunked.get('response').get('model')
- self._finish_reason = chunked.get('response').get('status')
- self._input_tokens = chunked.get('response').get('usage').get('input_tokens')
- self._output_tokens = chunked.get('response').get('usage').get('output_tokens')
+ process_chat_chunk(self, chunk)
return chunk
except StopAsyncIteration:
- # Handling exception ensure observability without disrupting operation
try:
- self._end_time = time.time()
- if len(self._timestamps) > 1:
- self._tbt = calculate_tbt(self._timestamps)
-
- try:
- formatted_messages = extract_and_format_input(self._kwargs.get('input', ''))
- prompt = concatenate_all_contents(formatted_messages)
- except:
- prompt = self._kwargs.get('input', '')
-
- request_model = self._kwargs.get("model", "gpt-4o")
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, self._input_tokens,
- self._output_tokens)
-
- # Set Span attributes (OTel Semconv)
- self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- self._span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- self._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- self._kwargs.get("seed", ""))
- self._span.set_attribute(SemanticConvention.SERVER_PORT,
- self._server_port)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- self._kwargs.get("max_output_tokens", -1))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- self._kwargs.get("stop", []))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- self._kwargs.get("temperature", 1.0))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- self._kwargs.get("top_p", 1.0))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [self._finish_reason])
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- self._response_id)
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- self._response_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- self._input_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- self._output_tokens)
- self._span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- self._server_address)
- if isinstance(self._llmresponse, str):
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- else:
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- # Set Span attributes (Extra)
- self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- self._span.set_attribute(SERVICE_NAME,
- application_name)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- self._kwargs.get("user", ""))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- True)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- self._input_tokens + self._output_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT,
- self._tbt)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- self._ttft)
- self._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
- },
- )
- self._span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- request_model=request_model,
- server_address=self._server_address,
- server_port=self._server_port,
- response_model=self._response_model,
+ with self._span:
+ process_streaming_chat_response(
+ self,
+ pricing_info=pricing_info,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version
)
-
- metrics["genai_client_usage_tokens"].record(
- self._input_tokens + self._output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- self._end_time - self._start_time, attributes
- )
- metrics["genai_server_tbt"].record(
- self._tbt, attributes
- )
- metrics["genai_server_ttft"].record(
- self._ttft, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(self._output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(self._input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
except Exception as e:
handle_exception(self._span, e)
- logger.error("Error in trace creation: %s", e)
- finally:
- self._span.end()
raise
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'chat.completions' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions' method.
+ Wraps the OpenAI async chat completions call.
"""
- # Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
- # pylint: disable=no-else-return
if streaming:
- # Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
+ return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
- # Handling for non-streaming responses
else:
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
-
- response_dict = response_as_dict(response)
try:
- try:
- formatted_messages = extract_and_format_input(kwargs.get('input', ''))
- prompt = concatenate_all_contents(formatted_messages)
- except:
- prompt = kwargs.get('input', '')
-
- input_tokens = response_dict.get('usage').get('input_tokens')
- output_tokens = response_dict.get('usage').get('output_tokens')
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- kwargs.get("seed", ""))
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- kwargs.get("max_output_tokens", -1))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- kwargs.get("stop", []))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(response_dict.get("temperature", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(response_dict.get("top_p", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response_dict.get("id"))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- response_dict.get('model'))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- end_time - start_time)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
-
- for i in range(kwargs.get('n',1)):
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [response_dict.get('status')])
- try:
- llm_response = str(response_dict.get('output')[i].get('content')[0].get('text',''))
- except:
- llm_response = ''
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- # pylint: disable=line-too-long
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: llm_response,
- },
- )
- if kwargs.get('tools'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(response_dict.get('tools')))
-
- if isinstance(llm_response, str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- elif llm_response is not None:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_chat_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=response_dict.get('model'),
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_server_ttft"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def async_chat_completions(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def async_responses(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for chat completions to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async responses API.
"""
class TracedAsyncStream:
"""
- Wrapper for streaming responses to collect metrics and trace data.
- Wraps the response to collect message IDs and aggregated response.
-
- This class implements the '__aiter__' and '__anext__' methods that
- handle asynchronous streaming responses.
-
- This class also implements '__aenter__' and '__aexit__' methods that
- handle asynchronous context management protocol.
+ Wrapper for async streaming responses to collect telemetry.
"""
+
def __init__(
self,
wrapped,
span,
+ span_name,
kwargs,
server_address,
server_port,
- **args,
):
self.__wrapped__ = wrapped
self._span = span
- # Placeholder for aggregating streaming response
+ self._span_name = span_name
self._llmresponse = ""
self._response_id = ""
self._response_model = ""
self._finish_reason = ""
- self._openai_response_service_tier = ""
- self._openai_system_fingerprint = ""
-
- self._args = args
+ self._input_tokens = 0
+ self._output_tokens = 0
+ self._reasoning_tokens = 0
+ self._operation_type = "responses"
+ self._service_tier = "default"
+ self._tools = None
+ self._response_tools = None
self._kwargs = kwargs
self._start_time = time.time()
self._end_time = None
@@ -496,585 +196,133 @@ def async_chat_completions(version, environment, application_name,
def __aiter__(self):
return self
- async def __getattr__(self, name):
+ def __getattr__(self, name):
"""Delegate attribute access to the wrapped object."""
- return getattr(await self.__wrapped__, name)
+ return getattr(self.__wrapped__, name)
async def __anext__(self):
try:
chunk = await self.__wrapped__.__anext__()
- end_time = time.time()
- # Record the timestamp for the current chunk
- self._timestamps.append(end_time)
-
- if len(self._timestamps) == 1:
- # Calculate time to first chunk
- self._ttft = calculate_ttft(self._timestamps, self._start_time)
-
- chunked = response_as_dict(chunk)
- # Collect message IDs and aggregated response from events
- if (len(chunked.get('choices')) > 0 and ('delta' in chunked.get('choices')[0] and
- 'content' in chunked.get('choices')[0].get('delta'))):
-
- content = chunked.get('choices')[0].get('delta').get('content')
- if content:
- self._llmresponse += content
- self._response_id = chunked.get('id')
- self._response_model = chunked.get('model')
- try:
- self._finish_reason = chunked.get('choices',[])[0].get('finish_reason')
- except:
- self._finish_reason = "stop"
- self._openai_response_service_tier = chunked.get('service_tier') or 'auto'
- self._openai_system_fingerprint = chunked.get('system_fingerprint')
+ process_response_chunk(self, chunk)
return chunk
except StopAsyncIteration:
- # Handling exception ensure observability without disrupting operation
try:
- self._end_time = time.time()
- if len(self._timestamps) > 1:
- self._tbt = calculate_tbt(self._timestamps)
-
- # Format 'messages' into a single string
- message_prompt = self._kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message["role"]
- content = message["content"]
-
- if isinstance(content, list):
- content_str_list = []
- for item in content:
- if item["type"] == "text":
- content_str_list.append(f'text: {item["text"]}')
- elif (item["type"] == "image_url" and
- not item["image_url"]["url"].startswith("data:")):
- content_str_list.append(f'image_url: {item["image_url"]["url"]}')
- content_str = ", ".join(content_str_list)
- formatted_messages.append(f"{role}: {content_str}")
- else:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- request_model = self._kwargs.get("model", "gpt-4o")
-
- # Calculate tokens using input prompt and aggregated response
- input_tokens = general_tokens(prompt)
- output_tokens = general_tokens(self._llmresponse)
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set Span attributes (OTel Semconv)
- self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- self._span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- self._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- str(self._kwargs.get("seed", "")))
- self._span.set_attribute(SemanticConvention.SERVER_PORT,
- self._server_port)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- str(self._kwargs.get("frequency_penalty", 0.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- self._kwargs.get("max_tokens", -1))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- str(self._kwargs.get("presence_penalty", 0.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- str(self._kwargs.get("stop", [])))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(self._kwargs.get("temperature", 1.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(self._kwargs.get("top_p", 1.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [self._finish_reason])
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- self._response_id)
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- self._response_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- self._span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- self._server_address)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
- str(self._kwargs.get("service_tier", "auto")))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER,
- self._openai_response_service_tier)
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
- self._openai_system_fingerprint)
- if isinstance(self._llmresponse, str):
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- else:
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- # Set Span attributes (Extra)
- self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- self._span.set_attribute(SERVICE_NAME,
- application_name)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- str(self._kwargs.get("user", "")))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- True)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT,
- self._tbt)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- self._ttft)
- self._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- if capture_message_content:
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
- },
- )
- self._span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- request_model=request_model,
- server_address=self._server_address,
- server_port=self._server_port,
- response_model=self._response_model,
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- self._end_time - self._start_time, attributes
- )
- metrics["genai_server_tbt"].record(
- self._tbt, attributes
- )
- metrics["genai_server_ttft"].record(
- self._ttft, attributes
+ with self._span:
+ process_streaming_response_response(
+ self,
+ pricing_info=pricing_info,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version
)
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
except Exception as e:
handle_exception(self._span, e)
- logger.error("Error in trace creation: %s", e)
- finally:
- self._span.end()
raise
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'chat.completions' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions' method.
+ Wraps the OpenAI async responses API call.
"""
- # Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
- # pylint: disable=no-else-return
if streaming:
- # Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
+ return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
- # Handling for non-streaming responses
else:
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()
-
- response_dict = response_as_dict(response)
try:
- # Format 'messages' into a single string
- message_prompt = kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message["role"]
- content = message.get("content", "")
-
- if isinstance(content, list):
- content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
- for item in content
- )
- formatted_messages.append(f"{role}: {content_str}")
- else:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- input_tokens = response_dict.get('usage').get('prompt_tokens')
- output_tokens = response_dict.get('usage').get('completion_tokens')
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- str(kwargs.get("seed", "")))
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- str(kwargs.get("frequency_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- kwargs.get("max_tokens", -1))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- str(kwargs.get("presence_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- str(kwargs.get("stop", [])))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(kwargs.get("temperature", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(kwargs.get("top_p", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response_dict.get("id"))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- response_dict.get('model'))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
- str(kwargs.get("service_tier", "auto")))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER,
- response_dict.get('service_tier', 'auto'))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
- response_dict.get('system_fingerprint', ''))
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- str(kwargs.get("user", "")))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- end_time - start_time)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
-
- for i in range(kwargs.get('n',1)):
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [response_dict.get('choices')[i].get('finish_reason')])
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- # pylint: disable=line-too-long
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(response_dict.get('choices')[i].get('message').get('content')),
- },
- )
- if kwargs.get('tools'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(response_dict.get('choices')[i].get('message').get('tool_calls')))
-
- if isinstance(response_dict.get('choices')[i].get('message').get('content'), str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- elif response_dict.get('choices')[i].get('message').get('content') is not None:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_response_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=response_dict.get('model'),
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_server_ttft"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def async_chat_completions_parse(version, environment, application_name, tracer, pricing_info, capture_message_content,
- metrics, disable_metrics):
+def async_chat_completions_parse(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for chat completions parse to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions parse method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async chat completions parse.
"""
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'chat.completions.parse' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions.parse' method.
+ Wraps the OpenAI async chat completions parse call.
"""
+
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
request_model = kwargs.get("model", "gpt-4o")
+
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
- try:
- # Execute the original 'parse' method
- response = await wrapped(*args, **kwargs)
- end_time = time.time()
-
- response_dict = response_as_dict(response)
-
- # Format 'messages' from kwargs to calculate input tokens
- message_prompt = kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message.get("role")
- content = message.get("content")
- if content:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- input_tokens = response_dict.get('usage').get('prompt_tokens')
- output_tokens = response_dict.get('usage').get('completion_tokens')
-
- # Calculate cost
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, str(kwargs.get("seed", "")))
- span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- str(kwargs.get("frequency_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, str(kwargs.get("max_tokens", -1)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- str(kwargs.get("presence_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, str(kwargs.get("stop", [])))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, str(kwargs.get("temperature", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, str(kwargs.get("top_p", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, response_dict.get("id"))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_dict.get('model'))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
- str(kwargs.get("service_tier", "auto")))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER,
- response_dict.get('service_tier', 'auto'))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
- str(response_dict.get('system_fingerprint', '')))
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
- span.set_attribute(SERVICE_NAME, application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, end_time - start_time)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt},
- )
+ response = await wrapped(*args, **kwargs)
- for i in range(kwargs.get('n', 1)):
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [response_dict.get('choices')[i].get('finish_reason')])
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- # pylint: disable=line-too-long
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(
- response_dict.get('choices')[i].get('message').get('content')),
- },
- )
- if kwargs.get('tools'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(response_dict.get('choices')[i].get('message').get('tool_calls')))
-
- if isinstance(response_dict.get('choices')[i].get('message').get('content'), str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- elif response_dict.get('choices')[i].get('message').get('content') is not None:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- span.set_status(Status(StatusCode.OK))
-
- if not disable_metrics:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ try:
+ response = process_chat_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=response_dict.get('model'),
- )
- metrics["genai_client_usage_tokens"].record(input_tokens + output_tokens, attributes)
- metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
- metrics["genai_server_ttft"].record( end_time - start_time, attributes)
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in 'parse' trace creation: %s", e)
- # Re-raise the exception to not interfere with the application flow
- raise
+
+ return response
return wrapper
-def async_embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def async_embedding(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for embeddings to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the embeddings method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async embeddings.
"""
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'embeddings' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'embeddings' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'embeddings' method.
- kwargs: Keyword arguments for the 'embeddings' method.
-
- Returns:
- The response from the original 'embeddings' method.
+ Wraps the OpenAI async embeddings call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1082,127 +330,44 @@ def async_embedding(version, environment, application_name,
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
- response_dict = response_as_dict(response)
try:
- input_tokens = response_dict.get('usage').get('prompt_tokens')
-
- # Calculate cost of the operation
- cost = get_embed_model_cost(request_model,
- pricing_info, input_tokens)
-
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
- [kwargs.get('encoding_format', 'float')])
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(kwargs.get("input", "")),
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_embedding_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
- metrics["genai_client_usage_tokens"].record(
- input_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def async_image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def async_image_generate(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for image generation to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI image generation.
- capture_message_content: Flag indicating whether to trace the input prompt and generated images.
-
- Returns:
- A function that wraps the image generation method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async image generation.
"""
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'images.generate' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'images.generate' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'images.generate' method.
- kwargs: Keyword arguments for the 'images.generate' method.
-
- Returns:
- The response from the original 'images.generate' method.
+ Wraps the OpenAI async image generation call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1210,146 +375,46 @@ def async_image_generate(version, environment, application_name,
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
- images_count = 0
-
try:
- # Find Image format
- if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
- image = "b64_json"
- else:
- image = "url"
-
- # Calculate cost of the operation
- cost = get_image_model_cost(request_model,
- pricing_info, kwargs.get("size", "1024x1024"),
- kwargs.get("quality", "standard"))
-
- for items in response.data:
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response.created)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "image")
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE,
- kwargs.get("size", "1024x1024"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_QUALITY,
- kwargs.get("quality", "standard"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_STYLE,
- kwargs.get("style", "vivid"))
- span.set_attribute(SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT,
- items.revised_prompt if items.revised_prompt else "")
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("prompt", ""),
- },
- )
- attribute_name = f"{SemanticConvention.GEN_AI_RESPONSE_IMAGE}.{images_count}"
- span.add_event(
- name=attribute_name,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: getattr(items, image),
- },
- )
-
- images_count+=1
-
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- len(response.data) * cost)
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_image_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ 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,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def async_image_variatons(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def async_image_variations(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for creating image variations to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of generating image variations.
- capture_message_content: Flag indicating whether to trace the input image and generated variations.
-
- Returns:
- A function that wraps the image variations creation method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async image variations.
"""
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'images.create.variations' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'images.create.variations' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the method.
- kwargs: Keyword arguments for the method.
-
- Returns:
- The response from the original 'images.create.variations' method.
+ Wraps the OpenAI async image variations call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1357,135 +422,46 @@ def async_image_variatons(version, environment, application_name,
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
- images_count = 0
-
try:
- # Find Image format
- if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
- image = "b64_json"
- else:
- image = "url"
-
- # Calculate cost of the operation
- cost = get_image_model_cost(request_model, pricing_info,
- kwargs.get("size", "1024x1024"), "standard")
-
- for items in response.data:
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response.created)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "image")
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE,
- kwargs.get("size", "1024x1024"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_QUALITY,
- "standard")
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- attribute_name = f"{SemanticConvention.GEN_AI_RESPONSE_IMAGE}.{images_count}"
- span.add_event(
- name=attribute_name,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: getattr(items, image),
- },
- )
-
- images_count+=1
-
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- len(response.data) * cost)
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_image_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ 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,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def async_audio_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def async_audio_create(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for creating speech audio to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of generating speech audio.
- capture_message_content: Flag indicating whether to trace the input text and generated audio.
-
- Returns:
- A function that wraps the speech audio creation method to add telemetry.
+ Generates a telemetry wrapper for OpenAI async audio creation.
"""
async def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'audio.speech.create' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'audio.speech.create' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'audio.speech.create' method.
- kwargs: Keyword arguments for the 'audio.speech.create' method.
-
- Returns:
- The response from the original 'audio.speech.create' method.
+ Wraps the OpenAI async audio creation call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1499,77 +475,27 @@ def async_audio_create(version, environment, application_name,
end_time = time.time()
try:
- # Calculate cost of the operation
- cost = get_audio_model_cost(request_model,
- pricing_info, kwargs.get("input", ""))
-
- # Set Span attributes
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "speech")
-
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE,
- kwargs.get("voice", "alloy"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,
- kwargs.get("response_format", "mp3"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SPEED,
- kwargs.get("speed", 1))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("input", ""),
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_audio_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ 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,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/openai/openai.py b/sdk/python/src/openlit/instrumentation/openai/openai.py
index 85ec01c2..7bfa9c63 100644
--- a/sdk/python/src/openlit/instrumentation/openai/openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/openai.py
@@ -2,78 +2,55 @@
Module for monitoring OpenAI API calls.
"""
-import logging
import time
-from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.trace import SpanKind
from openlit.__helpers import (
- get_chat_model_cost,
- get_embed_model_cost,
- get_audio_model_cost,
- get_image_model_cost,
- general_tokens,
handle_exception,
- extract_and_format_input,
- concatenate_all_contents,
- response_as_dict,
- calculate_ttft,
- calculate_tbt,
- create_metrics_attributes,
set_server_address_and_port
)
+from openlit.instrumentation.openai.utils import (
+ process_chat_chunk,
+ process_response_chunk,
+ process_chat_response,
+ process_streaming_chat_response,
+ process_streaming_response_response,
+ process_response_response,
+ process_embedding_response,
+ process_image_response,
+ process_audio_response,
+)
from openlit.semcov import SemanticConvention
-# Initialize logger for logging potential issues and operations
-logger = logging.getLogger(__name__)
-
-def responses(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def chat_completions(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for chat completions to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions method to add telemetry.
+ Generates a telemetry wrapper for OpenAI chat completions.
"""
class TracedSyncStream:
"""
- Wrapper for streaming responses to collect metrics and trace data.
- Wraps the response to collect message IDs and aggregated response.
-
- This class implements the '__aiter__' and '__anext__' methods that
- handle asynchronous streaming responses.
-
- This class also implements '__aenter__' and '__aexit__' methods that
- handle asynchronous context management protocol.
+ Wrapper for streaming responses to collect telemetry.
"""
+
def __init__(
self,
wrapped,
span,
+ span_name,
kwargs,
server_address,
server_port,
- **args,
):
self.__wrapped__ = wrapped
self._span = span
- # Placeholder for aggregating streaming response
+ self._span_name = span_name
self._llmresponse = ""
self._response_id = ""
self._response_model = ""
self._finish_reason = ""
- self._input_tokens = ""
- self._output_tokens = ""
-
- self._args = args
+ self._system_fingerprint = ""
+ self._service_tier = "auto"
+ self._tools = None
self._kwargs = kwargs
self._start_time = time.time()
self._end_time = None
@@ -100,383 +77,106 @@ def responses(version, environment, application_name,
def __next__(self):
try:
chunk = self.__wrapped__.__next__()
- end_time = time.time()
- # Record the timestamp for the current chunk
- self._timestamps.append(end_time)
-
- if len(self._timestamps) == 1:
- # Calculate time to first chunk
- self._ttft = calculate_ttft(self._timestamps, self._start_time)
-
- chunked = response_as_dict(chunk)
- # Collect message IDs and aggregated response from events
- if chunked.get('type') == "response.output_text.delta":
- self._llmresponse += chunked.get('delta')
- if chunked.get('type') == "response.completed":
- self._response_id = chunked.get('response').get('id')
- self._response_model = chunked.get('response').get('model')
- self._finish_reason = chunked.get('response').get('status')
- self._input_tokens = chunked.get('response').get('usage').get('input_tokens')
- self._output_tokens = chunked.get('response').get('usage').get('output_tokens')
+ process_chat_chunk(self, chunk)
return chunk
except StopIteration:
- # Handling exception ensure observability without disrupting operation
try:
- self._end_time = time.time()
- if len(self._timestamps) > 1:
- self._tbt = calculate_tbt(self._timestamps)
-
- try:
- formatted_messages = extract_and_format_input(self._kwargs.get('input', ''))
- prompt = concatenate_all_contents(formatted_messages)
- except:
- prompt = self._kwargs.get('input', '')
-
- request_model = self._kwargs.get("model", "gpt-4o")
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, self._input_tokens,
- self._output_tokens)
-
- # Set Span attributes (OTel Semconv)
- self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- self._span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- self._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- str(self._kwargs.get("seed", "")))
- self._span.set_attribute(SemanticConvention.SERVER_PORT,
- self._server_port)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- str(self._kwargs.get("max_output_tokens", -1)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- str(self._kwargs.get("stop", [])))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(self._kwargs.get("temperature", 1.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(self._kwargs.get("top_p", 1.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [self._finish_reason])
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- self._response_id)
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- self._response_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- self._input_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- self._output_tokens)
- self._span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- self._server_address)
- if isinstance(self._llmresponse, str):
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- else:
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- # Set Span attributes (Extra)
- self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- self._span.set_attribute(SERVICE_NAME,
- application_name)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- self._kwargs.get("user", ""))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- True)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- self._input_tokens + self._output_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT,
- self._tbt)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- self._ttft)
- self._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
- },
- )
- self._span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- request_model=request_model,
- server_address=self._server_address,
- server_port=self._server_port,
- response_model=self._response_model,
+ with self._span:
+ process_streaming_chat_response(
+ self,
+ pricing_info=pricing_info,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version
)
-
- metrics["genai_client_usage_tokens"].record(
- self._input_tokens + self._output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- self._end_time - self._start_time, attributes
- )
- metrics["genai_server_tbt"].record(
- self._tbt, attributes
- )
- metrics["genai_server_ttft"].record(
- self._ttft, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(self._output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(self._input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
except Exception as e:
handle_exception(self._span, e)
- logger.error("Error in trace creation: %s", e)
- finally:
- self._span.end()
raise
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'chat.completions' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions' method.
+ Wraps the OpenAI chat completions call.
"""
- # Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
- # pylint: disable=no-else-return
if streaming:
- # Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
+ return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
- # Handling for non-streaming responses
else:
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
-
- response_dict = response_as_dict(response)
try:
- try:
- formatted_messages = extract_and_format_input(kwargs.get('input', ''))
- prompt = concatenate_all_contents(formatted_messages)
- except:
- prompt = kwargs.get('input', '')
-
- input_tokens = response_dict.get('usage').get('input_tokens')
- output_tokens = response_dict.get('usage').get('output_tokens')
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- kwargs.get("seed", ""))
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- kwargs.get("max_output_tokens", -1))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- kwargs.get("stop", []))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(response_dict.get("temperature", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(response_dict.get("top_p", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response_dict.get("id"))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- response_dict.get('model'))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- end_time - start_time)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
-
- for i in range(kwargs.get('n',1)):
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [response_dict.get('status')])
- try:
- llm_response = response_dict.get('output')[i].get('content')[0].get('text','')
- except:
- llm_response = ''
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- # pylint: disable=line-too-long
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: llm_response,
- },
- )
- if kwargs.get('tools'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(response_dict.get('tools')))
-
- if isinstance(llm_response, str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- elif llm_response is not None:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_chat_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=response_dict.get('model'),
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_server_ttft"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def chat_completions(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def responses(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics, **kwargs):
"""
- Generates a telemetry wrapper for chat completions to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions method to add telemetry.
+ Generates a telemetry wrapper for OpenAI responses API.
"""
class TracedSyncStream:
"""
- Wrapper for streaming responses to collect metrics and trace data.
- Wraps the response to collect message IDs and aggregated response.
-
- This class implements the '__aiter__' and '__anext__' methods that
- handle asynchronous streaming responses.
-
- This class also implements '__aenter__' and '__aexit__' methods that
- handle asynchronous context management protocol.
+ Wrapper for streaming responses to collect telemetry.
"""
+
def __init__(
self,
wrapped,
span,
+ span_name,
kwargs,
server_address,
server_port,
- **args,
):
self.__wrapped__ = wrapped
self._span = span
- # Placeholder for aggregating streaming response
+ self._span_name = span_name
self._llmresponse = ""
self._response_id = ""
self._response_model = ""
self._finish_reason = ""
- self._openai_response_service_tier = ""
- self._openai_system_fingerprint = ""
-
- self._args = args
+ self._input_tokens = 0
+ self._output_tokens = 0
+ self._reasoning_tokens = 0
+ self._operation_type = "responses"
+ self._service_tier = "default"
+ self._tools = None
+ self._response_tools = None
self._kwargs = kwargs
self._start_time = time.time()
self._end_time = None
@@ -503,578 +203,126 @@ def chat_completions(version, environment, application_name,
def __next__(self):
try:
chunk = self.__wrapped__.__next__()
- end_time = time.time()
- # Record the timestamp for the current chunk
- self._timestamps.append(end_time)
-
- if len(self._timestamps) == 1:
- # Calculate time to first chunk
- self._ttft = calculate_ttft(self._timestamps, self._start_time)
-
- chunked = response_as_dict(chunk)
- # Collect message IDs and aggregated response from events
- if (len(chunked.get('choices')) > 0 and ('delta' in chunked.get('choices')[0] and
- 'content' in chunked.get('choices')[0].get('delta'))):
-
- content = chunked.get('choices')[0].get('delta').get('content')
- if content:
- self._llmresponse += content
- self._response_id = chunked.get('id')
- self._response_model = chunked.get('model')
- try:
- self._finish_reason = chunked.get('choices', [])[0].get('finish_reason')
- except (IndexError, AttributeError, TypeError):
- self._finish_reason = "stop"
- self._openai_response_service_tier = chunked.get('service_tier') or 'auto'
- self._openai_system_fingerprint = chunked.get('system_fingerprint')
+ process_response_chunk(self, chunk)
return chunk
except StopIteration:
- # Handling exception ensure observability without disrupting operation
try:
- self._end_time = time.time()
- if len(self._timestamps) > 1:
- self._tbt = calculate_tbt(self._timestamps)
-
- # Format 'messages' into a single string
- message_prompt = self._kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message["role"]
- content = message["content"]
-
- if isinstance(content, list):
- content_str_list = []
- for item in content:
- if item["type"] == "text":
- content_str_list.append(f'text: {item["text"]}')
- elif (item["type"] == "image_url" and
- not item["image_url"]["url"].startswith("data:")):
- content_str_list.append(f'image_url: {item["image_url"]["url"]}')
- content_str = ", ".join(content_str_list)
- formatted_messages.append(f"{role}: {content_str}")
- else:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- request_model = self._kwargs.get("model", "gpt-4o")
-
- # Calculate tokens using input prompt and aggregated response
- input_tokens = general_tokens(prompt)
- output_tokens = general_tokens(self._llmresponse)
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set Span attributes (OTel Semconv)
- self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- self._span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- self._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- str(self._kwargs.get("seed", "")))
- self._span.set_attribute(SemanticConvention.SERVER_PORT,
- self._server_port)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- str(self._kwargs.get("frequency_penalty", 0.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- self._kwargs.get("max_tokens", -1))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- str(self._kwargs.get("presence_penalty", 0.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- str(self._kwargs.get("stop", [])))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(self._kwargs.get("temperature", 1.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(self._kwargs.get("top_p", 1.0)))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [self._finish_reason])
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- self._response_id)
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- self._response_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- self._span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- self._server_address)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
- str(self._kwargs.get("service_tier", "auto")))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER,
- self._openai_response_service_tier)
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
- self._openai_system_fingerprint)
- if isinstance(self._llmresponse, str):
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- else:
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- # Set Span attributes (Extra)
- self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- self._span.set_attribute(SERVICE_NAME,
- application_name)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- str(self._kwargs.get("user", "")))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- True)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT,
- self._tbt)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- self._ttft)
- self._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- if capture_message_content:
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
+ with self._span:
+ process_streaming_response_response(
+ self,
+ pricing_info=pricing_info,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version
)
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
- },
- )
- self._span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- request_model=request_model,
- server_address=self._server_address,
- server_port=self._server_port,
- response_model=self._response_model,
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- self._end_time - self._start_time, attributes
- )
- metrics["genai_server_tbt"].record(
- self._tbt, attributes
- )
- metrics["genai_server_ttft"].record(
- self._ttft, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
except Exception as e:
handle_exception(self._span, e)
- logger.error("Error in trace creation: %s", e)
- finally:
- self._span.end()
raise
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'chat.completions' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions' method.
+ Wraps the OpenAI responses API call.
"""
- # Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
- # pylint: disable=no-else-return
if streaming:
- # Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
+ return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
- # Handling for non-streaming responses
else:
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
-
- response_dict = response_as_dict(response)
try:
- # Format 'messages' into a single string
- message_prompt = kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message["role"]
- content = message["content"]
-
- if isinstance(content, list):
- content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
- for item in content
- )
- formatted_messages.append(f"{role}: {content_str}")
- else:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- input_tokens = response_dict.get('usage').get('prompt_tokens')
- output_tokens = response_dict.get('usage').get('completion_tokens')
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED,
- str(kwargs.get("seed", "")))
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- str(kwargs.get("frequency_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- str(kwargs.get("max_tokens", -1)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- str(kwargs.get("presence_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
- str(kwargs.get("stop", [])))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- str(kwargs.get("temperature", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- str(kwargs.get("top_p", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response_dict.get("id"))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- response_dict.get('model'))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
- str(kwargs.get("service_tier", "auto")))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER,
- response_dict.get('service_tier', 'auto'))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
- str(response_dict.get('system_fingerprint', '')))
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- end_time - start_time)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
-
- for i in range(kwargs.get('n',1)):
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [response_dict.get('choices')[i].get('finish_reason')])
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- # pylint: disable=line-too-long
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(response_dict.get('choices')[i].get('message').get('content')),
- },
- )
- if kwargs.get('tools'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(response_dict.get('choices')[i].get('message').get('tool_calls')))
-
- if isinstance(response_dict.get('choices')[i].get('message').get('content'), str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- elif response_dict.get('choices')[i].get('message').get('content') is not None:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_response_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=response_dict.get('model'),
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_server_ttft"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def chat_completions_parse(version, environment, application_name, tracer, pricing_info, capture_message_content,
- metrics, disable_metrics):
+def chat_completions_parse(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for chat completions parse to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions parse method to add telemetry.
+ Generates a telemetry wrapper for OpenAI chat completions parse.
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'chat.completions.parse' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions.parse' method.
+ Wraps the OpenAI chat completions parse call.
"""
+
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
request_model = kwargs.get("model", "gpt-4o")
+
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
- try:
- # Execute the original 'parse' method
- response = wrapped(*args, **kwargs)
- end_time = time.time()
-
- response_dict = response_as_dict(response)
-
- # Format 'messages' from kwargs to calculate input tokens
- message_prompt = kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message.get("role")
- content = message.get("content")
- if content:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- input_tokens = response_dict.get('usage').get('prompt_tokens')
- output_tokens = response_dict.get('usage').get('completion_tokens')
-
- # Calculate cost
- cost = get_chat_model_cost(request_model,
- pricing_info, input_tokens,
- output_tokens)
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, str(kwargs.get("seed", "")))
- span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- str(kwargs.get("frequency_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, str(kwargs.get("max_tokens", -1)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- str(kwargs.get("presence_penalty", 0.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, str(kwargs.get("stop", [])))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, str(kwargs.get("temperature", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, str(kwargs.get("top_p", 1.0)))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, response_dict.get("id"))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_dict.get('model'))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
- str(kwargs.get("service_tier", "auto")))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER,
- response_dict.get('service_tier', 'auto'))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
- str(response_dict.get('system_fingerprint', '')))
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
- span.set_attribute(SERVICE_NAME, application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, end_time - start_time)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt},
- )
+ response = wrapped(*args, **kwargs)
- for i in range(kwargs.get('n', 1)):
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON,
- [response_dict.get('choices')[i].get('finish_reason')])
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- # pylint: disable=line-too-long
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(
- response_dict.get('choices')[i].get('message').get('content')),
- },
- )
- if kwargs.get('tools'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(response_dict.get('choices')[i].get('message').get('tool_calls')))
-
- if isinstance(response_dict.get('choices')[i].get('message').get('content'), str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- elif response_dict.get('choices')[i].get('message').get('content') is not None:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- span.set_status(Status(StatusCode.OK))
-
- if not disable_metrics:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ try:
+ response = process_chat_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=response_dict.get('model'),
- )
- metrics["genai_client_usage_tokens"].record(input_tokens + output_tokens, attributes)
- metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
- metrics["genai_server_ttft"].record( end_time - start_time, attributes)
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in 'parse' trace creation: %s", e)
- # Re-raise the exception to not interfere with the application flow
- raise
+
+ return response
return wrapper
-def embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def embedding(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics, **kwargs):
"""
- Generates a telemetry wrapper for embeddings to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the embeddings method to add telemetry.
+ Generates a telemetry wrapper for OpenAI embeddings.
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'embeddings' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'embeddings' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'embeddings' method.
- kwargs: Keyword arguments for the 'embeddings' method.
-
- Returns:
- The response from the original 'embeddings' method.
+ Wraps the OpenAI embeddings call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1082,127 +330,44 @@ def embedding(version, environment, application_name,
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
- response_dict = response_as_dict(response)
try:
- input_tokens = response_dict.get('usage').get('prompt_tokens')
-
- # Calculate cost of the operation
- cost = get_embed_model_cost(request_model,
- pricing_info, input_tokens)
-
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
- [kwargs.get('encoding_format', 'float')])
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(kwargs.get("input", "")),
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_embedding_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
- metrics["genai_client_usage_tokens"].record(
- input_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def image_generate(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics, **kwargs):
"""
- Generates a telemetry wrapper for image generation to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of OpenAI image generation.
- capture_message_content: Flag indicating whether to trace the input prompt and generated images.
-
- Returns:
- A function that wraps the image generation method to add telemetry.
+ Generates a telemetry wrapper for OpenAI image generation.
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'images.generate' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'images.generate' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'images.generate' method.
- kwargs: Keyword arguments for the 'images.generate' method.
-
- Returns:
- The response from the original 'images.generate' method.
+ Wraps the OpenAI image generation call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1210,146 +375,46 @@ def image_generate(version, environment, application_name,
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
- images_count = 0
-
try:
- # Find Image format
- if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
- image = "b64_json"
- else:
- image = "url"
-
- # Calculate cost of the operation
- cost = get_image_model_cost(request_model,
- pricing_info, kwargs.get("size", "1024x1024"),
- kwargs.get("quality", "standard"))
-
- for items in response.data:
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response.created)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "image")
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE,
- kwargs.get("size", "1024x1024"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_QUALITY,
- kwargs.get("quality", "standard"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_STYLE,
- kwargs.get("style", "vivid"))
- span.set_attribute(SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT,
- items.revised_prompt if items.revised_prompt else "")
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("prompt", ""),
- },
- )
- attribute_name = f"{SemanticConvention.GEN_AI_RESPONSE_IMAGE}.{images_count}"
- span.add_event(
- name=attribute_name,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: getattr(items, image),
- },
- )
-
- images_count+=1
-
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- len(response.data) * cost)
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_image_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ 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,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def image_variatons(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def image_variatons(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for creating image variations to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of generating image variations.
- capture_message_content: Flag indicating whether to trace the input image and generated variations.
-
- Returns:
- A function that wraps the image variations creation method to add telemetry.
+ Generates a telemetry wrapper for OpenAI image variations.
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'images.create.variations' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'images.create.variations' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the method.
- kwargs: Keyword arguments for the method.
-
- Returns:
- The response from the original 'images.create.variations' method.
+ Wraps the OpenAI image variations call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1357,135 +422,46 @@ def image_variatons(version, environment, application_name,
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ 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()
- images_count = 0
-
try:
- # Find Image format
- if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
- image = "b64_json"
- else:
- image = "url"
-
- # Calculate cost of the operation
- cost = get_image_model_cost(request_model, pricing_info,
- kwargs.get("size", "1024x1024"), "standard")
-
- for items in response.data:
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID,
- response.created)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "image")
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE,
- kwargs.get("size", "1024x1024"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_QUALITY,
- "standard")
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- attribute_name = f"{SemanticConvention.GEN_AI_RESPONSE_IMAGE}.{images_count}"
- span.add_event(
- name=attribute_name,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: getattr(items, image),
- },
- )
-
- images_count+=1
-
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- len(response.data) * cost)
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_image_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ 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,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
-def audio_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def audio_create(version, environment, application_name, tracer, pricing_info,
+ capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for creating speech audio to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the OpenAI API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating the cost of generating speech audio.
- capture_message_content: Flag indicating whether to trace the input text and generated audio.
-
- Returns:
- A function that wraps the speech audio creation method to add telemetry.
+ Generates a telemetry wrapper for OpenAI audio creation.
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'audio.speech.create' API call to add telemetry.
-
- This collects metrics such as execution time, cost, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'audio.speech.create' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'audio.speech.create' method.
- kwargs: Keyword arguments for the 'audio.speech.create' method.
-
- Returns:
- The response from the original 'audio.speech.create' method.
+ Wraps the OpenAI audio creation call.
"""
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
@@ -1499,77 +475,27 @@ def audio_create(version, environment, application_name,
end_time = time.time()
try:
- # Calculate cost of the operation
- cost = get_audio_model_cost(request_model,
- pricing_info, kwargs.get("input", ""))
-
- # Set Span attributes
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "speech")
-
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE,
- kwargs.get("voice", "alloy"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,
- kwargs.get("response_format", "mp3"))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SPEED,
- kwargs.get("speed", 1))
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("input", ""),
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
- system=SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ response = process_audio_response(
+ response=response,
request_model=request_model,
- server_address=server_address,
+ pricing_info=pricing_info,
server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_cost"].record(cost, attributes)
-
- # Return original response
- return response
+ 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,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.py
new file mode 100644
index 00000000..44a3ad83
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py
@@ -0,0 +1,794 @@
+"""
+OpenAI OpenTelemetry instrumentation utility functions
+"""
+import time
+
+from opentelemetry.trace import Status, StatusCode
+
+from openlit.__helpers import (
+ calculate_ttft,
+ response_as_dict,
+ calculate_tbt,
+ get_chat_model_cost,
+ get_embed_model_cost,
+ get_audio_model_cost,
+ get_image_model_cost,
+ general_tokens,
+ record_completion_metrics,
+ record_embedding_metrics,
+ record_audio_metrics,
+ record_image_metrics,
+ common_span_attributes,
+)
+from openlit.semcov import SemanticConvention
+
+def format_content(messages):
+ """
+ Format the messages into a string for span events.
+ Handles both chat completions format and responses API input format.
+ """
+
+ if not messages:
+ return ""
+
+ # Handle string input (simple case)
+ if isinstance(messages, str):
+ return messages
+
+ # Handle list of messages
+ formatted_messages = []
+ for message in messages:
+ role = message.get("role", "user")
+ content = message.get("content", "")
+
+ if isinstance(content, list):
+ content_str_list = []
+ for item in content:
+ # Chat completions format
+ if item.get("type") == "text":
+ content_str_list.append(f'text: {item.get("text", "")}')
+ elif (item.get("type") == "image_url" and
+ not item.get("image_url", {}).get("url", "").startswith("data:")):
+ content_str_list.append(f'image_url: {item["image_url"]["url"]}')
+
+ # Responses API format
+ elif item.get("type") == "input_text":
+ content_str_list.append(f'text: {item.get("text", "")}')
+ elif item.get("type") == "input_image":
+ image_url = item.get("image_url", "")
+ if image_url and not image_url.startswith("data:"):
+ content_str_list.append(f'image_url: {image_url}')
+
+ content_str = ", ".join(content_str_list)
+ formatted_messages.append(f"{role}: {content_str}")
+ else:
+ formatted_messages.append(f"{role}: {content}")
+
+ return "\n".join(formatted_messages)
+
+def process_chat_chunk(scope, chunk):
+ """
+ Process a chunk of chat response data and update state.
+ """
+
+ end_time = time.time()
+ scope._timestamps.append(end_time)
+
+ if len(scope._timestamps) == 1:
+ scope._ttft = calculate_ttft(scope._timestamps, scope._start_time)
+
+ chunked = response_as_dict(chunk)
+
+ # Extract content from chat completions
+ if (len(chunked.get("choices", [])) > 0 and
+ "delta" in chunked.get("choices")[0]):
+
+ delta = chunked.get("choices")[0]["delta"]
+ content = delta.get("content")
+ if content:
+ scope._llmresponse += content
+
+ # Handle tool calls in streaming - optimized
+ delta_tools = delta.get("tool_calls")
+ if delta_tools:
+ scope._tools = scope._tools or []
+
+ for tool in delta_tools:
+ idx = tool.get("index", 0)
+
+ # Extend list if needed
+ scope._tools.extend([{}] * (idx + 1 - len(scope._tools)))
+
+ if tool.get("id"): # New tool (id exists)
+ func = tool.get("function", {})
+ scope._tools[idx] = {
+ "id": tool["id"],
+ "function": {"name": func.get("name", ""), "arguments": func.get("arguments", "")},
+ "type": tool.get("type", "function")
+ }
+ elif scope._tools[idx] and "function" in tool: # Append args (id is None)
+ scope._tools[idx]["function"]["arguments"] += tool["function"].get("arguments", "")
+
+ # Extract metadata
+ scope._response_id = chunked.get("id") or scope._response_id
+ scope._response_model = chunked.get("model") or scope._response_model
+
+ try:
+ scope._finish_reason = chunked.get("choices", [])[0].get("finish_reason") or scope._finish_reason
+ except (IndexError, AttributeError, TypeError):
+ scope._finish_reason = "stop"
+
+ scope._system_fingerprint = chunked.get("system_fingerprint") or scope._system_fingerprint
+ scope._service_tier = chunked.get("service_tier") or scope._service_tier
+
+def process_response_chunk(scope, chunk):
+ """
+ Process a chunk of response API data and update state.
+ """
+
+ end_time = time.time()
+ scope._timestamps.append(end_time)
+
+ if len(scope._timestamps) == 1:
+ scope._ttft = calculate_ttft(scope._timestamps, scope._start_time)
+
+ chunked = response_as_dict(chunk)
+
+ # Extract content from responses API
+ if chunked.get("type") == "response.output_text.delta":
+ scope._llmresponse += chunked.get("delta", "")
+
+ # Handle tool calls in streaming for responses API
+ elif chunked.get("type") == "response.output_item.added":
+ # New tool call item added
+ if not hasattr(scope, "_response_tools") or scope._response_tools is None:
+ scope._response_tools = []
+
+ item = chunked.get("item", {})
+ if item.get("type") == "function_call":
+ scope._response_tools.append({
+ "id": item.get("id", ""),
+ "call_id": item.get("call_id", ""),
+ "name": item.get("name", ""),
+ "type": item.get("type", "function_call"),
+ "arguments": item.get("arguments", ""),
+ "status": item.get("status", "in_progress")
+ })
+
+ elif chunked.get("type") == "response.function_call_arguments.delta":
+ # Tool arguments being streamed
+ if hasattr(scope, "_response_tools") and scope._response_tools:
+ item_id = chunked.get("item_id", "")
+ delta = chunked.get("delta", "")
+
+ # Find the tool by item_id and append arguments
+ for tool in scope._response_tools:
+ if tool.get("id") == item_id:
+ tool["arguments"] += delta
+ break
+
+ elif chunked.get("type") == "response.function_call_arguments.done":
+ # Tool arguments complete
+ if hasattr(scope, "_response_tools") and scope._response_tools:
+ item_id = chunked.get("item_id", "")
+ final_arguments = chunked.get("arguments", "")
+
+ # Update the tool with final arguments
+ for tool in scope._response_tools:
+ if tool.get("id") == item_id:
+ tool["arguments"] = final_arguments
+ break
+
+ elif chunked.get("type") == "response.output_item.done":
+ # Tool call item complete
+ if hasattr(scope, "_response_tools") and scope._response_tools:
+ item = chunked.get("item", {})
+ item_id = item.get("id", "")
+
+ # Update the tool with final status and data
+ for tool in scope._response_tools:
+ if tool.get("id") == item_id:
+ tool.update({
+ "call_id": item.get("call_id", tool.get("call_id", "")),
+ "name": item.get("name", tool.get("name", "")),
+ "arguments": item.get("arguments", tool.get("arguments", "")),
+ "status": item.get("status", "completed")
+ })
+ break
+
+ elif chunked.get("type") == "response.completed":
+ response_data = chunked.get("response", {})
+ scope._response_id = response_data.get("id")
+ scope._response_model = response_data.get("model")
+ scope._finish_reason = response_data.get("status")
+
+ usage = response_data.get("usage", {})
+ scope._input_tokens = usage.get("input_tokens", 0)
+ scope._output_tokens = usage.get("output_tokens", 0)
+
+ # Handle reasoning tokens
+ output_tokens_details = usage.get("output_tokens_details", {})
+ scope._reasoning_tokens = output_tokens_details.get("reasoning_tokens", 0)
+
+def common_response_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream):
+ """
+ Process responses API request and generate Telemetry
+ """
+
+ scope._end_time = time.time()
+ if len(scope._timestamps) > 1:
+ scope._tbt = calculate_tbt(scope._timestamps)
+
+ # For responses API, format input using the same function as chat completions
+ input_data = scope._kwargs.get("input", "")
+ prompt = format_content(input_data)
+ request_model = scope._kwargs.get("model", "gpt-4o")
+
+ # Calculate tokens and cost
+ if hasattr(scope, "_input_tokens") and scope._input_tokens:
+ input_tokens = scope._input_tokens
+ output_tokens = scope._output_tokens
+ else:
+ input_tokens = general_tokens(prompt)
+ output_tokens = general_tokens(scope._llmresponse)
+
+ cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, scope._response_model,
+ environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+
+ # Span Attributes for Request parameters specific to responses API
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_output_tokens", -1))
+
+ # Reasoning parameters
+ reasoning = scope._kwargs.get("reasoning", {})
+ if reasoning:
+ if reasoning.get("effort"):
+ scope._span.set_attribute("gen_ai.request.reasoning_effort", reasoning.get("effort"))
+
+ # Responses API specific attributes
+ if hasattr(scope, "_service_tier"):
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._service_tier)
+
+ # Span Attributes for Response parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text")
+
+ # Span Attributes for Tools (responses API structure) - optimized
+ if hasattr(scope, "_response_tools") and scope._response_tools:
+ tools = scope._response_tools if isinstance(scope._response_tools, list) else [scope._response_tools]
+
+ names, ids, args = zip(*[
+ (t.get("name", ""),
+ str(t.get("call_id", "")), # Use call_id for responses API
+ str(t.get("arguments", "")))
+ for t in tools if isinstance(t, dict) and t
+ ]) if tools else ([], [], [])
+
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+
+ # Span Attributes for Cost and Tokens
+ 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_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Reasoning tokens
+ if hasattr(scope, "_reasoning_tokens") and scope._reasoning_tokens > 0:
+ scope._span.set_attribute("gen_ai.usage.reasoning_tokens", scope._reasoning_tokens)
+
+ # Span Attributes for Content
+ if capture_message_content:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+
+ # To be removed once the change to span_attributes (from span events) is complete
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: scope._llmresponse,
+ },
+ )
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics
+ if not disable_metrics:
+ record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, scope._response_model, environment,
+ application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
+ cost, scope._tbt, scope._ttft)
+
+def process_streaming_response_response(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content=False, disable_metrics=False, version=""):
+ """
+ Process streaming responses API response and generate telemetry.
+ """
+
+ common_response_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream=True)
+
+def process_response_response(response, request_model, pricing_info, server_port, server_address,
+ environment, application_name, metrics, start_time, span, capture_message_content=False,
+ disable_metrics=False, version="1.0.0", **kwargs):
+ """
+ Process non-streaming responses API response and generate telemetry.
+ """
+
+ scope = type("GenericScope", (), {})()
+ response_dict = response_as_dict(response)
+
+ scope._start_time = start_time
+ scope._end_time = time.time()
+ scope._span = span
+
+ # Extract content from responses API structure with reasoning support
+ output = response_dict.get("output", [])
+ scope._llmresponse = ""
+ scope._response_tools = None
+
+ if output:
+ # Find the message item in the output array (might not be first if reasoning is present)
+ message_item = None
+ for item in output:
+ if item.get("type") == "message":
+ message_item = item
+ break
+ if item.get("type") == "function_call":
+ # Handle tool call
+ scope._response_tools = [{
+ "id": item.get("id", ""),
+ "call_id": item.get("call_id", ""),
+ "name": item.get("name", ""),
+ "type": item.get("type", "function_call"),
+ "arguments": item.get("arguments", ""),
+ "status": item.get("status", "")
+ }]
+
+ # Extract content from message item if found
+ if message_item:
+ content = message_item.get("content", [])
+ if content and len(content) > 0:
+ scope._llmresponse = content[0].get("text", "")
+
+ scope._response_id = response_dict.get("id")
+ scope._response_model = response_dict.get("model")
+
+ # Handle token usage including reasoning tokens
+ usage = response_dict.get("usage", {})
+ scope._input_tokens = usage.get("input_tokens", 0)
+ scope._output_tokens = usage.get("output_tokens", 0)
+
+ output_tokens_details = usage.get("output_tokens_details", {})
+ scope._reasoning_tokens = output_tokens_details.get("reasoning_tokens", 0)
+
+ scope._timestamps = []
+ scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+ scope._service_tier = response_dict.get("service_tier", "default")
+ scope._finish_reason = response_dict.get("status", "completed")
+
+ common_response_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream=False)
+
+ return response
+
+def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream):
+ """
+ Process chat request and generate Telemetry
+ """
+
+ scope._end_time = time.time()
+ if len(scope._timestamps) > 1:
+ scope._tbt = calculate_tbt(scope._timestamps)
+
+ # Format messages for chat operations
+ if hasattr(scope, "_operation_type") and scope._operation_type == "responses":
+ # Handle responses API input format using format_content
+ input_data = scope._kwargs.get("input", "")
+ prompt = format_content(input_data)
+ else:
+ # Handle standard chat format
+ prompt = format_content(scope._kwargs.get("messages", []))
+
+ request_model = scope._kwargs.get("model", "gpt-4o")
+
+ # Calculate tokens and cost
+ if hasattr(scope, "_input_tokens") and scope._input_tokens:
+ input_tokens = scope._input_tokens
+ output_tokens = scope._output_tokens
+ else:
+ input_tokens = general_tokens(prompt)
+ output_tokens = general_tokens(scope._llmresponse)
+
+ cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, scope._response_model,
+ environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+
+ # Span Attributes for Request parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, str(scope._kwargs.get("seed", "")))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
+
+ # Span Attributes for Response parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+
+ # OpenAI-specific attributes
+ if hasattr(scope, "_system_fingerprint"):
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, scope._system_fingerprint)
+ if hasattr(scope, "_service_tier"):
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._service_tier)
+
+ # Span Attributes for Tools - optimized
+ if hasattr(scope, "_tools") and scope._tools:
+ tools = scope._tools if isinstance(scope._tools, list) else [scope._tools]
+
+ names, ids, args = zip(*[
+ (t.get("function", {}).get("name", ""),
+ str(t.get("id", "")),
+ str(t.get("function", {}).get("arguments", "")))
+ for t in tools if isinstance(t, dict) and t
+ ]) if tools else ([], [], [])
+
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+
+ # Span Attributes for Cost and Tokens
+ 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_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Span Attributes for Content
+ if capture_message_content:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+
+ # To be removed once the change to span_attributes (from span events) is complete
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: scope._llmresponse,
+ },
+ )
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics
+ if not disable_metrics:
+ record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, scope._response_model, environment,
+ application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
+ cost, scope._tbt, scope._ttft)
+
+def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content=False, disable_metrics=False, version=""):
+ """
+ Process streaming chat response and generate telemetry.
+ """
+
+ common_chat_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream=True)
+
+def process_chat_response(response, request_model, pricing_info, server_port, server_address,
+ environment, application_name, metrics, start_time, span, capture_message_content=False,
+ disable_metrics=False, version="1.0.0", **kwargs):
+ """
+ Process non-streaming chat response and generate telemetry.
+ """
+
+ scope = type("GenericScope", (), {})()
+ response_dict = response_as_dict(response)
+
+ scope._start_time = start_time
+ scope._end_time = time.time()
+ scope._span = span
+ scope._llmresponse = " ".join(
+ (choice.get("message", {}).get("content") or "")
+ for choice in response_dict.get("choices", [])
+ )
+ scope._response_id = response_dict.get("id")
+ scope._response_model = response_dict.get("model")
+ scope._input_tokens = response_dict.get("usage", {}).get("prompt_tokens", 0)
+ scope._output_tokens = response_dict.get("usage", {}).get("completion_tokens", 0)
+ scope._timestamps = []
+ scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+ scope._system_fingerprint = response_dict.get("system_fingerprint", "")
+ scope._service_tier = response_dict.get("service_tier", "auto")
+ scope._finish_reason = str(response_dict.get("choices", [])[0].get("finish_reason", "")) if response_dict.get("choices") else ""
+
+ # Handle operation type for responses API
+ if kwargs.get("_operation_type") == "responses":
+ scope._operation_type = "responses"
+
+ # Handle tool calls
+ if kwargs.get("tools"):
+ scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ else:
+ scope._tools = None
+
+ common_chat_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream=False)
+
+ return response
+
+def common_embedding_logic(scope, request_model, pricing_info, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version):
+ """
+ Common logic for processing embedding operations.
+ """
+
+ # Calculate cost
+ cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, request_model,
+ environment, application_name, False, scope._tbt, scope._ttft, version)
+
+ # Span Attributes for Request parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [scope._kwargs.get("encoding_format", "float")])
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
+
+ # Span Attributes for Cost and Tokens
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Span Attributes for Content
+ if capture_message_content:
+ input_data = scope._kwargs.get("input", "")
+ formatted_content = format_content(input_data) if isinstance(input_data, (list, dict)) else str(input_data)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_content)
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics
+ if not disable_metrics:
+ record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, request_model, environment,
+ application_name, scope._start_time, scope._end_time, scope._input_tokens, cost)
+
+def common_image_logic(scope, request_model, pricing_info, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version):
+ """
+ Common logic for processing image operations.
+ """
+
+ # Calculate cost
+ cost = get_image_model_cost(request_model, pricing_info,
+ scope._kwargs.get("size", "1024x1024"),
+ scope._kwargs.get("quality", "standard"))
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, request_model,
+ environment, application_name, False, scope._tbt, scope._ttft, version)
+
+ # Span Attributes for Request parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE, scope._kwargs.get("size", "1024x1024"))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_QUALITY, scope._kwargs.get("quality", "standard"))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
+
+ # Extract response data
+ response_dict = scope._response_dict
+ images_data = response_dict.get("data", [])
+ response_created = response_dict.get("created")
+ response_size = response_dict.get("size")
+ response_quality = response_dict.get("quality")
+ response_output_format = response_dict.get("output_format")
+
+ # Span Attributes for Response
+ if response_created:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, str(response_created))
+
+ # Process image data and collect URLs/base64 content
+ if images_data:
+ # Collect image URLs or base64 content
+ image_contents = []
+
+ for image in images_data:
+ # Collect image content (URL or base64)
+ if image.get("url"):
+ image_contents.append(image["url"])
+ elif image.get("b64_json"):
+ # For base64, we typically dont want to store the full content in spans
+ # Just indicate its base64 format
+ image_contents.append("[base64_image_data]")
+
+ # Set image response data using semantic conventions
+ if image_contents:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_IMAGE, image_contents)
+
+ # Response-level attributes if different from request
+ if response_size:
+ scope._span.set_attribute("gen_ai.response.image_size", response_size)
+ if response_quality:
+ scope._span.set_attribute("gen_ai.response.image_quality", response_quality)
+ if response_output_format:
+ scope._span.set_attribute("gen_ai.response.output_format", response_output_format)
+
+ # Span Attributes for Cost
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Span Attributes for Content
+ if capture_message_content:
+ # Always collect the original prompt
+ prompt = scope._kwargs.get("prompt", "")
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
+
+ # Collect and set revised prompts if available
+ if images_data:
+ revised_prompts = []
+ for image in images_data:
+ if image.get("revised_prompt"):
+ revised_prompts.append(image["revised_prompt"])
+
+ # Set revised prompts as span attribute if any were found
+ if revised_prompts:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT, revised_prompts)
+
+ # Add revised prompt events for detailed tracking
+ for i, image in enumerate(images_data):
+ if image.get("revised_prompt"):
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT: image["revised_prompt"],
+ "image_index": i,
+ },
+ )
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics
+ if not disable_metrics:
+ record_image_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE, 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 common_audio_logic(scope, request_model, pricing_info, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version):
+ """
+ Common logic for processing audio operations.
+ """
+
+ # Calculate cost
+ input_text = scope._kwargs.get("input", "")
+ cost = get_audio_model_cost(request_model, pricing_info, input_text)
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address, scope._server_port, request_model, request_model,
+ environment, application_name, False, scope._tbt, scope._ttft, version)
+
+ # Span Attributes for Request parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE, scope._kwargs.get("voice", "alloy"))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT, scope._kwargs.get("response_format", "mp3"))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SPEED, scope._kwargs.get("speed", 1.0))
+
+ # Span Attributes for Cost
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Span Attributes for Content
+ if capture_message_content:
+ input_text = scope._kwargs.get("input", "")
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, input_text)
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics
+ if not disable_metrics:
+ record_audio_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, 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_audio_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", **kwargs):
+ """
+ Process audio generation response and generate telemetry.
+ """
+
+ scope = type("GenericScope", (), {})()
+
+ scope._start_time = start_time
+ scope._end_time = end_time
+ scope._span = span
+ scope._timestamps = []
+ scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+
+ common_audio_logic(scope, request_model, pricing_info, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version)
+
+ return response
+
+def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
+ environment, application_name, metrics, start_time, span, capture_message_content=False,
+ disable_metrics=False, version="1.0.0", **kwargs):
+ """
+ Process embedding response and generate telemetry.
+ """
+
+ scope = type("GenericScope", (), {})()
+ response_dict = response_as_dict(response)
+
+ scope._start_time = start_time
+ scope._end_time = time.time()
+ scope._span = span
+ scope._input_tokens = response_dict.get("usage", {}).get("prompt_tokens", 0)
+ scope._timestamps = []
+ scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+
+ common_embedding_logic(scope, request_model, pricing_info, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version)
+
+ return response
+
+def process_image_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", **kwargs):
+ """
+ Process image generation 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 = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+ scope._response_dict = response_dict
+
+ common_image_logic(scope, request_model, pricing_info, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version)
+
+ return response
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py b/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py
index 9c2e4892..7758d258 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py
@@ -136,6 +136,6 @@ def async_send_message(version, environment, application_name, tracer,
except Exception as e:
handle_exception(span, e)
- return response
+ return response
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py b/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py
index 905ca68d..87702ee6 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py
@@ -136,6 +136,6 @@ def send_message(version, environment, application_name, tracer,
except Exception as e:
handle_exception(span, e)
- return response
+ return response
return wrapper