Skip to content

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

fix(openai): suppress responses spans in framework context (#1571)

LZYfdb0cd4cc006
diff --git a/sdk/python/src/openlit/instrumentation/crewai/__init__.py b/sdk/python/src/openlit/instrumentation/crewai/__init__.py
index 895bfe22..31305424 100644
--- a/sdk/python/src/openlit/instrumentation/crewai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/crewai/__init__.py
@@ -14,6 +14,10 @@ from wrapt import wrap_function_wrapper
from openlit._config import OpenlitConfig
from openlit.instrumentation.crewai.crewai import general_wrap
from openlit.instrumentation.crewai.async_crewai import async_general_wrap
+from openlit.instrumentation.crewai.openai_llm import (
+ async_openai_completion_call,
+ openai_completion_call,
+)
_instruments = ("crewai >= 1.10.0",)
@@ -138,6 +142,25 @@ class CrewAIInstrumentor(BaseInstrumentor):
except Exception:
pass
+ # Native OpenAI provider. CrewAI calls responses.create and
+ # chat.completions.create on its own client, not through LiteLLM, so
+ # nothing else sets the framework-LLM flag. Own the LLM span here and
+ # hold the flag for the whole call. Older CrewAI builds without this
+ # class are skipped.
+ try:
+ wrap_function_wrapper(
+ "crewai.llms.providers.openai.completion",
+ "OpenAICompletion.call",
+ openai_completion_call(*wrap_args),
+ )
+ wrap_function_wrapper(
+ "crewai.llms.providers.openai.completion",
+ "OpenAICompletion.acall",
+ async_openai_completion_call(*wrap_args),
+ )
+ except Exception:
+ pass
+
# -- thread context propagation --
self._install_context_propagation()
diff --git a/sdk/python/src/openlit/instrumentation/crewai/openai_llm.py b/sdk/python/src/openlit/instrumentation/crewai/openai_llm.py
new file mode 100644
index 00000000..60fa3cd9
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/crewai/openai_llm.py
@@ -0,0 +1,580 @@
+"""LLM span for CrewAI's native OpenAI provider.
+
+CrewAI 1.15 calls ``responses.create`` and ``chat.completions.create`` on its
+own OpenAI client. LiteLLM, LangChain, and Strands are the only other setters
+of ``openlit_framework_llm_span_active``, and none of them run on this path.
+Without a span owned here, the provider wrappers either emit a second LLM span
+or, once guarded, emit nothing and the prompt disappears from the crew trace.
+
+The flag stays set for the entire ``call`` / ``acall``. CrewAI consumes a
+Responses stream inside that method, so clearing the flag before it returns
+would let the provider wrapper open a span again during iteration.
+"""
+
+import json
+import logging
+import time
+from urllib.parse import urlparse
+
+from opentelemetry.trace import SpanKind, Status, StatusCode
+
+from openlit.__helpers import (
+ common_span_attributes,
+ format_input_message,
+ format_output_message,
+ get_chat_model_cost,
+ get_server_address_for_provider,
+ handle_exception,
+ is_framework_llm_active,
+ record_completion_metrics,
+ reset_framework_llm_active,
+ set_framework_llm_active,
+)
+from openlit.instrumentation.crewai.utils import _infer_provider_from_model
+from openlit.semcov import SemanticConvention
+
+logger = logging.getLogger(__name__)
+
+_USAGE_KEYS = (
+ "prompt_tokens",
+ "completion_tokens",
+ "reasoning_tokens",
+ "cached_prompt_tokens",
+ "cache_creation_tokens",
+)
+
+
+def openai_completion_call(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """Wrap ``OpenAICompletion.call`` with one framework LLM span."""
+
+ def wrapper(wrapped, instance, args, kwargs):
+ if is_framework_llm_active():
+ return wrapped(*args, **kwargs)
+
+ return _run_sync(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ )
+
+ return wrapper
+
+
+def async_openai_completion_call(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """Wrap ``OpenAICompletion.acall`` with one framework LLM span."""
+
+ async def wrapper(wrapped, instance, args, kwargs):
+ if is_framework_llm_active():
+ return await wrapped(*args, **kwargs)
+
+ return await _run_async(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ )
+
+ return wrapper
+
+
+def _run_sync(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """Start the span, hold the flag around the sync call, then record it."""
+ request_model = _request_model(instance)
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ before = _usage_snapshot(instance)
+ messages = _call_messages(args, kwargs)
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ fw_token = set_framework_llm_active()
+ try:
+ response = wrapped(*args, **kwargs)
+ except Exception as exc:
+ handle_exception(span, exc)
+ _record_error_metrics(
+ metrics,
+ disable_metrics,
+ request_model,
+ instance,
+ environment,
+ application_name,
+ start_time,
+ before,
+ pricing_info,
+ exc,
+ )
+ raise
+ finally:
+ reset_framework_llm_active(fw_token)
+
+ _finish_span(
+ span,
+ instance,
+ response,
+ messages,
+ request_model,
+ before,
+ start_time,
+ version,
+ environment,
+ application_name,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ )
+ return response
+
+
+async def _run_async(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """Start the span, hold the flag around the async call, then record it."""
+ request_model = _request_model(instance)
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ before = _usage_snapshot(instance)
+ messages = _call_messages(args, kwargs)
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+ fw_token = set_framework_llm_active()
+ try:
+ response = await wrapped(*args, **kwargs)
+ except Exception as exc:
+ handle_exception(span, exc)
+ _record_error_metrics(
+ metrics,
+ disable_metrics,
+ request_model,
+ instance,
+ environment,
+ application_name,
+ start_time,
+ before,
+ pricing_info,
+ exc,
+ )
+ raise
+ finally:
+ reset_framework_llm_active(fw_token)
+
+ _finish_span(
+ span,
+ instance,
+ response,
+ messages,
+ request_model,
+ before,
+ start_time,
+ version,
+ environment,
+ application_name,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ )
+ return response
+
+
+def _finish_span(
+ span,
+ instance,
+ response,
+ messages,
+ request_model,
+ before,
+ start_time,
+ version,
+ environment,
+ application_name,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """Record the call onto the span. Telemetry must not fail the LLM call."""
+ try:
+ usage = _usage_delta(before, _usage_snapshot(instance))
+ server_address, server_port = _server_for_llm(instance)
+ scope = type("GenericScope", (), {"_span": span})()
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ server_address,
+ server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ _is_stream(instance),
+ 0,
+ 0,
+ version,
+ )
+ _set_api_type(span, instance)
+ _set_messages(span, messages, response, capture_message_content)
+ _set_usage(span, usage)
+ _record_metrics(
+ metrics,
+ disable_metrics,
+ request_model,
+ instance,
+ environment,
+ application_name,
+ start_time,
+ before,
+ pricing_info,
+ usage=usage,
+ )
+ span.set_status(Status(StatusCode.OK))
+ except Exception:
+ logger.debug("Failed to record CrewAI OpenAI LLM span", exc_info=True)
+
+
+def _record_error_metrics(
+ metrics,
+ disable_metrics,
+ request_model,
+ instance,
+ environment,
+ application_name,
+ start_time,
+ before,
+ pricing_info,
+ exc,
+):
+ """Record the failed call without hiding the original exception."""
+ try:
+ _record_metrics(
+ metrics,
+ disable_metrics,
+ request_model,
+ instance,
+ environment,
+ application_name,
+ start_time,
+ before,
+ pricing_info,
+ error_type=type(exc).__name__ or "_OTHER",
+ )
+ except Exception:
+ logger.debug("Failed to record CrewAI OpenAI error metrics", exc_info=True)
+
+
+def _record_metrics(
+ metrics,
+ disable_metrics,
+ request_model,
+ instance,
+ environment,
+ application_name,
+ start_time,
+ before,
+ pricing_info,
+ usage=None,
+ error_type=None,
+):
+ """Record one completion metric for this call, or nothing when disabled."""
+ if disable_metrics or not metrics:
+ return
+ if usage is None:
+ usage = _usage_delta(before, _usage_snapshot(instance))
+ input_tokens = usage[0] if usage else 0
+ output_tokens = usage[1] if usage else 0
+ cache_read = usage[3] if usage else 0
+ cache_creation = usage[4] if usage else 0
+ server_address, server_port = _server_for_llm(instance)
+ cost = 0
+ if pricing_info:
+ cost = get_chat_model_cost(
+ request_model,
+ pricing_info,
+ input_tokens,
+ output_tokens,
+ cache_read_tokens=cache_read,
+ cache_creation_tokens=cache_creation,
+ prompt_tokens_include_cache=True,
+ )
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ server_address,
+ server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ start_time,
+ time.time(),
+ input_tokens,
+ output_tokens,
+ cost,
+ None,
+ None,
+ error_type=error_type,
+ is_stream=_is_stream(instance),
+ )
+
+
+def _set_usage(span, usage):
+ """Write the per-call token delta. Lifetime counters are not the call."""
+ if usage is None:
+ return
+ input_tokens, output_tokens, reasoning, cache_read, cache_creation = usage
+ 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.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
+ if cache_read:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read
+ )
+ if cache_creation:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
+ cache_creation,
+ )
+ if reasoning:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS_REPORTED, True
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, reasoning
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, reasoning)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_DERIVED_COMPLETED_OUTPUT_TOKENS,
+ max(output_tokens - reasoning, 0),
+ )
+
+
+def _set_messages(span, messages, response, capture_message_content):
+ """Store prompt and output only when content capture is on."""
+ if not capture_message_content:
+ return
+ incoming = _input_messages(messages)
+ outgoing = _output_messages(response)
+ if incoming is not None:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_INPUT_MESSAGES, json.dumps(incoming)
+ )
+ if outgoing is not None:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES, json.dumps(outgoing)
+ )
+ finish = outgoing[0].get("finish_reason")
+ if finish:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [finish]
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT,
+ )
+
+
+def _set_api_type(span, instance):
+ """Mark which OpenAI API this call used, when the provider says so."""
+ api = _effective_api(instance)
+ if api == "responses":
+ span.set_attribute(SemanticConvention.OPENAI_API_TYPE, "responses")
+ elif api == "completions":
+ span.set_attribute(SemanticConvention.OPENAI_API_TYPE, "chat_completions")
+
+
+def _effective_api(instance):
+ """Return CrewAI's resolved API name, or None when it cannot be read."""
+ effective = getattr(instance, "_effective_api", None)
+ if callable(effective):
+ try:
+ return effective()
+ except Exception:
+ return getattr(instance, "api", None)
+ return getattr(instance, "api", None)
+
+
+def _call_messages(args, kwargs):
+ """Return the messages argument from either position or keyword."""
+ if args:
+ return args[0]
+ return kwargs.get("messages")
+
+
+def _request_model(instance):
+ """Return the model id CrewAI will send, or a stable fallback."""
+ model = getattr(instance, "model", None)
+ if model:
+ return str(model)
+ return "openai"
+
+
+def _is_stream(instance):
+ """Return the call-scoped stream flag when CrewAI exposes one."""
+ effective = getattr(instance, "_effective_stream", None)
+ if callable(effective):
+ try:
+ return bool(effective())
+ except Exception:
+ return bool(getattr(instance, "stream", False))
+ return bool(getattr(instance, "stream", False))
+
+
+def _server_for_llm(instance):
+ """Resolve the endpoint from the LLM instance itself.
+
+ ``set_server_address_and_port`` looks up ``instance.llm``. Here the
+ instance is the LLM, so that lookup would miss ``api_base`` / ``base_url``.
+ """
+ api_base = getattr(instance, "api_base", None) or getattr(instance, "base_url", None)
+ if api_base:
+ parsed = urlparse(str(api_base))
+ if parsed.hostname:
+ port = parsed.port
+ if port is None:
+ port = 443 if parsed.scheme == "https" else 80
+ return parsed.hostname, port
+ provider = _infer_provider_from_model(_request_model(instance)) or "openai"
+ return get_server_address_for_provider(provider)
+
+
+def _usage_snapshot(instance):
+ """Copy CrewAI's cumulative counters. The dict is mutated in place."""
+ usage = getattr(instance, "_token_usage", None)
+ if not isinstance(usage, dict):
+ return None
+ try:
+ return {key: int(usage.get(key) or 0) for key in _USAGE_KEYS}
+ except (TypeError, ValueError):
+ return None
+
+
+def _usage_delta(before, after):
+ """Return per-call tokens, or None when the instance does not track them.
+
+ ``_token_usage`` sums every call on the LLM object. Attributing the raw
+ counter would put the lifetime total on each span.
+ """
+ if before is None or after is None:
+ return None
+ return tuple(max(after[key] - before[key], 0) for key in _USAGE_KEYS)
+
+
+def _input_messages(messages):
+ """Normalize a string or a list of role/content messages."""
+ if messages is None:
+ return None
+ if isinstance(messages, str):
+ return [format_input_message("user", messages)]
+ if not isinstance(messages, list):
+ return [format_input_message("user", messages)]
+ formatted = []
+ for message in messages:
+ if isinstance(message, dict):
+ role = str(message.get("role") or "user")
+ content = message.get("content")
+ if content is None:
+ content = ""
+ formatted.append(format_input_message(role, content))
+ continue
+ role = str(getattr(message, "role", None) or "user")
+ content = getattr(message, "content", None)
+ if content is None:
+ content = str(message)
+ formatted.append(format_input_message(role, content))
+ return formatted or None
+
+
+def _output_messages(response):
+ """Normalize text, tool-call lists, and ResponsesAPIResult-like objects."""
+ if isinstance(response, str):
+ return [format_output_message(response, "stop")]
+ if isinstance(response, list):
+ parts = []
+ for item in response:
+ if not isinstance(item, dict) or not item.get("name"):
+ continue
+ arguments = item.get("arguments")
+ if arguments is None:
+ arguments = {}
+ parts.append(
+ {
+ "type": "tool_call",
+ "id": str(item.get("id") or item.get("call_id") or ""),
+ "name": str(item.get("name")),
+ "arguments": arguments,
+ }
+ )
+ if parts:
+ return [
+ {"role": "assistant", "parts": parts, "finish_reason": "tool_call"}
+ ]
+ return None
+ text = getattr(response, "text", None)
+ if isinstance(text, str):
+ return [format_output_message(text, "stop")]
+ if response is None:
+ return None
+ return [format_output_message(response, "stop")]
diff --git a/sdk/python/src/openlit/instrumentation/openai/async_openai.py b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
index 0c2841c3..3647586a 100644
--- a/sdk/python/src/openlit/instrumentation/openai/async_openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
@@ -381,6 +381,12 @@ def async_responses(
Wraps the OpenAI async responses API call.
"""
+ # Same guard as async chat completions. The flag is held for the
+ # whole framework call, including stream iteration that happens
+ # inside that call.
+ if is_framework_llm_active():
+ return await wrapped(*args, **kwargs)
+
streaming = kwargs.get("stream", False)
server_address, server_port = set_server_address_and_port(
instance, "api.openai.com", 443
diff --git a/sdk/python/src/openlit/instrumentation/openai/openai.py b/sdk/python/src/openlit/instrumentation/openai/openai.py
index de5d4e6b..922b1459 100644
--- a/sdk/python/src/openlit/instrumentation/openai/openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/openai.py
@@ -383,6 +383,12 @@ def responses(
Wraps the OpenAI responses API call.
"""
+ # Framework instrumentors (CrewAI, LiteLLM, LangChain, Strands) own
+ # the LLM span and set this flag for the duration of the call. Skip
+ # the provider span so it cannot export ahead of that parent.
+ if is_framework_llm_active():
+ return wrapped(*args, **kwargs)
+
streaming = kwargs.get("stream", False)
server_address, server_port = set_server_address_and_port(
instance, "api.openai.com", 443
diff --git a/sdk/python/tests/test_crewai_openai_llm_span.py b/sdk/python/tests/test_crewai_openai_llm_span.py
new file mode 100644
index 00000000..c964a330
--- /dev/null
+++ b/sdk/python/tests/test_crewai_openai_llm_span.py
@@ -0,0 +1,261 @@
+"""CrewAI native OpenAI calls own the LLM span and suppress the provider span."""
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, Mock
+
+import pytest
+from opentelemetry.sdk.trace import TracerProvider
+from opentelemetry.sdk.trace.export import SimpleSpanProcessor
+from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
+from opentelemetry.trace import StatusCode
+
+from openlit._config import OpenlitConfig
+from openlit.__helpers import (
+ is_framework_llm_active,
+ reset_framework_llm_active,
+ set_framework_llm_active,
+)
+from openlit.instrumentation.crewai.openai_llm import (
+ async_openai_completion_call,
+ openai_completion_call,
+)
+from openlit.instrumentation.openai.async_openai import async_responses
+from openlit.instrumentation.openai.openai import chat_completions, responses
+from openlit.semcov import SemanticConvention
+
+
+def _tracer():
+ """Return a tracer and the exporter that receives its ended spans."""
+ OpenlitConfig.reset_to_defaults()
+ exporter = InMemorySpanExporter()
+ provider = TracerProvider()
+ provider.add_span_processor(SimpleSpanProcessor(exporter))
+ return provider.get_tracer("test-crewai-openai-llm"), exporter
+
+
+def _config(tracer, capture=False):
+ """Return the wrapper arguments the CrewAI instrumentor would pass."""
+ return {
+ "version": "test-version",
+ "environment": "test-env",
+ "application_name": "test-app",
+ "tracer": tracer,
+ "pricing_info": {},
+ "capture_message_content": capture,
+ "metrics": None,
+ "disable_metrics": True,
+ }
+
+
+def _llm():
+ """Return a stand-in for OpenAICompletion with cumulative token counters."""
+ return SimpleNamespace(
+ model="openai/gpt-4o",
+ stream=False,
+ api="responses",
+ _token_usage={
+ "prompt_tokens": 4,
+ "completion_tokens": 1,
+ "reasoning_tokens": 0,
+ "cached_prompt_tokens": 0,
+ "cache_creation_tokens": 0,
+ },
+ )
+
+
+def _provider_wrapper(factory, tracer):
+ """Build an OpenAI provider wrapper with the same tracer."""
+ return factory(**_config(tracer))
+
+
+def test_call_parents_llm_span_and_skips_responses_span():
+ """The Responses wrapper must not open a span while the crew call owns it."""
+ tracer, exporter = _tracer()
+ crew = openai_completion_call(**_config(tracer, capture=True))
+ provider = _provider_wrapper(responses, tracer)
+ instance = _llm()
+
+ def wrapped(*_args, **_kwargs):
+ assert is_framework_llm_active()
+ instance._token_usage["prompt_tokens"] += 11
+ instance._token_usage["completion_tokens"] += 7
+ instance._token_usage["reasoning_tokens"] += 2
+ sentinel = object()
+ result = provider(
+ Mock(return_value=sentinel),
+ object(),
+ (),
+ {"model": "gpt-4o", "stream": True},
+ )
+ assert result is sentinel
+ return "from the model"
+
+ with tracer.start_as_current_span("invoke_agent researcher") as task:
+ result = crew(wrapped, instance, ("hello crew",), {})
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ llm = spans[0]
+ assert llm.parent.span_id == task.get_span_context().span_id
+
+ assert result == "from the model"
+ assert is_framework_llm_active() is False
+ assert llm.name == "chat openai/gpt-4o"
+ assert llm.attributes[SemanticConvention.GEN_AI_REQUEST_MODEL] == "openai/gpt-4o"
+ assert llm.attributes[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 11
+ assert llm.attributes[SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS] == 7
+ assert llm.attributes[SemanticConvention.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] == 2
+ assert llm.attributes[SemanticConvention.OPENAI_API_TYPE] == "responses"
+ assert llm.attributes[SemanticConvention.SERVER_ADDRESS] == "api.openai.com"
+ assert "hello crew" in llm.attributes[SemanticConvention.GEN_AI_INPUT_MESSAGES]
+ assert "from the model" in llm.attributes[SemanticConvention.GEN_AI_OUTPUT_MESSAGES]
+
+
+def test_call_skips_chat_completions_span_too():
+ """Chat completions on the same native client follow the same flag."""
+ tracer, exporter = _tracer()
+ crew = openai_completion_call(**_config(tracer))
+ provider = _provider_wrapper(chat_completions, tracer)
+ instance = _llm()
+ instance.api = "completions"
+
+ def wrapped(*_args, **_kwargs):
+ sentinel = object()
+ result = provider(
+ Mock(return_value=sentinel),
+ object(),
+ (),
+ {"model": "gpt-4o", "stream": False},
+ )
+ assert result is sentinel
+ return "chat text"
+
+ crew(wrapped, instance, ("hi",), {})
+
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ assert spans[0].name == "chat openai/gpt-4o"
+ assert spans[0].attributes[SemanticConvention.OPENAI_API_TYPE] == "chat_completions"
+ assert SemanticConvention.GEN_AI_INPUT_MESSAGES not in spans[0].attributes
+
+
+def test_usage_delta_ignores_earlier_calls_on_the_same_instance():
+ """Lifetime counters on the LLM must not be copied onto the next span."""
+ tracer, exporter = _tracer()
+ crew = openai_completion_call(**_config(tracer))
+ instance = _llm()
+
+ def wrapped(*_args, **_kwargs):
+ instance._token_usage["prompt_tokens"] += 2
+ instance._token_usage["completion_tokens"] += 3
+ return "ok"
+
+ crew(wrapped, instance, ("first",), {})
+ crew(wrapped, instance, ("second",), {})
+
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 2
+ for span in spans:
+ assert span.attributes[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 2
+ assert span.attributes[SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
+
+
+def test_call_resets_flag_when_the_provider_raises():
+ """A failed call must not leave the next request suppressed."""
+ tracer, exporter = _tracer()
+ crew = openai_completion_call(**_config(tracer))
+
+ def wrapped(*_args, **_kwargs):
+ raise RuntimeError("provider down")
+
+ with pytest.raises(RuntimeError, match="provider down"):
+ crew(wrapped, _llm(), ("hi",), {})
+
+ assert is_framework_llm_active() is False
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ assert spans[0].status.status_code == StatusCode.ERROR
+
+
+def test_call_passes_through_when_a_framework_span_is_already_active():
+ """A nested entry must not open a second LLM span or reset the outer flag."""
+ tracer, exporter = _tracer()
+ crew = openai_completion_call(**_config(tracer))
+ token = set_framework_llm_active()
+ try:
+ result = crew(lambda *_args, **_kwargs: "outer", _llm(), ("hi",), {})
+ assert is_framework_llm_active() is True
+ finally:
+ reset_framework_llm_active(token)
+
+ assert result == "outer"
+ assert is_framework_llm_active() is False
+ assert not exporter.get_finished_spans()
+
+
+def test_tool_call_list_is_recorded_as_output():
+ """Function-call returns stay on the crew span as tool-call parts."""
+ tracer, exporter = _tracer()
+ crew = openai_completion_call(**_config(tracer, capture=True))
+ tool_calls = [{"id": "call_1", "name": "multiply", "arguments": {"a": 2}}]
+
+ crew(lambda *_args, **_kwargs: tool_calls, _llm(), ("compute",), {})
+
+ output = exporter.get_finished_spans()[0].attributes[
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES
+ ]
+ assert "multiply" in output
+ assert "tool_call" in output
+
+
+async def test_acall_parents_llm_span_and_skips_async_responses_span():
+ """Async crew calls hold the flag across the awaited provider call."""
+ tracer, exporter = _tracer()
+ crew = async_openai_completion_call(**_config(tracer, capture=True))
+ provider = _provider_wrapper(async_responses, tracer)
+ instance = _llm()
+
+ async def wrapped(*_args, **_kwargs):
+ assert is_framework_llm_active()
+ instance._token_usage["prompt_tokens"] += 5
+ instance._token_usage["completion_tokens"] += 6
+ sentinel = object()
+ result = await provider(
+ AsyncMock(return_value=sentinel),
+ object(),
+ (),
+ {"model": "gpt-4o", "stream": False},
+ )
+ assert result is sentinel
+ return "async answer"
+
+ with tracer.start_as_current_span("invoke_agent researcher") as task:
+ result = await crew(wrapped, instance, ("hello async",), {})
+ llm = exporter.get_finished_spans()[-1]
+ assert llm.parent.span_id == task.get_span_context().span_id
+
+ assert result == "async answer"
+ assert is_framework_llm_active() is False
+ chat_spans = [
+ span
+ for span in exporter.get_finished_spans()
+ if span.name.startswith("chat ")
+ ]
+ assert len(chat_spans) == 1
+ assert "hello async" in llm.attributes[SemanticConvention.GEN_AI_INPUT_MESSAGES]
+ assert llm.attributes[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] == 5
+
+
+async def test_acall_resets_flag_when_the_provider_raises():
+ """A failed async call must not leave the flag set."""
+ tracer, _exporter = _tracer()
+ crew = async_openai_completion_call(**_config(tracer))
+
+ async def wrapped(*_args, **_kwargs):
+ raise RuntimeError("async down")
+
+ with pytest.raises(RuntimeError, match="async down"):
+ await crew(wrapped, _llm(), ("hi",), {})
+
+ assert is_framework_llm_active() is False
diff --git a/sdk/python/tests/test_openai_framework_guard.py b/sdk/python/tests/test_openai_framework_guard.py
new file mode 100644
index 00000000..b1246ea5
--- /dev/null
+++ b/sdk/python/tests/test_openai_framework_guard.py
@@ -0,0 +1,131 @@
+"""Tests for suppressing OpenAI Responses spans inside framework instrumentation."""
+
+from unittest.mock import AsyncMock, Mock
+
+import pytest
+from opentelemetry.sdk.trace import TracerProvider
+from opentelemetry.sdk.trace.export import SimpleSpanProcessor
+from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
+
+from openlit._config import OpenlitConfig
+from openlit.__helpers import (
+ reset_framework_llm_active,
+ set_framework_llm_active,
+)
+from openlit.instrumentation.openai.async_openai import async_responses
+from openlit.instrumentation.openai.openai import responses
+
+
+def _wrapper_kwargs(tracer):
+ """Return common wrapper configuration."""
+ return {
+ "version": "test-version",
+ "environment": "test-env",
+ "application_name": "test-app",
+ "tracer": tracer,
+ "pricing_info": {},
+ "capture_message_content": False,
+ "metrics": None,
+ "disable_metrics": True,
+ }
+
+
+def _real_tracer():
+ """Return a tracer whose finished spans can be inspected."""
+ OpenlitConfig.reset_to_defaults()
+ exporter = InMemorySpanExporter()
+ provider = TracerProvider()
+ provider.add_span_processor(SimpleSpanProcessor(exporter))
+ return provider.get_tracer("test-openai-framework-guard"), exporter
+
+
[email protected]("streaming", [False, True])
+def test_responses_skips_span_when_framework_llm_is_active(streaming):
+ """Sync Responses calls should pass through without creating a span."""
+ tracer = Mock()
+ wrapper = responses(**_wrapper_kwargs(tracer))
+ sentinel = object()
+ wrapped = Mock(return_value=sentinel)
+ kwargs = {"model": "gpt-4o", "stream": streaming}
+
+ token = set_framework_llm_active()
+ try:
+ result = wrapper(wrapped, object(), (), kwargs)
+ finally:
+ reset_framework_llm_active(token)
+
+ assert result is sentinel
+ wrapped.assert_called_once_with(model="gpt-4o", stream=streaming)
+ tracer.start_span.assert_not_called()
+ tracer.start_as_current_span.assert_not_called()
+
+
[email protected]("streaming", [False, True])
+async def test_async_responses_skips_span_when_framework_llm_is_active(streaming):
+ """Async Responses calls should pass through without creating a span."""
+ tracer = Mock()
+ wrapper = async_responses(**_wrapper_kwargs(tracer))
+ sentinel = object()
+ wrapped = AsyncMock(return_value=sentinel)
+ kwargs = {"model": "gpt-4o", "stream": streaming}
+
+ token = set_framework_llm_active()
+ try:
+ result = await wrapper(wrapped, object(), (), kwargs)
+ finally:
+ reset_framework_llm_active(token)
+
+ assert result is sentinel
+ wrapped.assert_awaited_once_with(model="gpt-4o", stream=streaming)
+ tracer.start_span.assert_not_called()
+ tracer.start_as_current_span.assert_not_called()
+
+
+def test_responses_starts_span_when_framework_llm_is_inactive():
+ """A direct Responses call still gets its own span."""
+ tracer, exporter = _real_tracer()
+ wrapper = responses(**_wrapper_kwargs(tracer))
+ wrapped = Mock(return_value={"output": [], "model": "gpt-4o"})
+
+ result = wrapper(wrapped, object(), (), {"model": "gpt-4o", "stream": False})
+
+ assert result["model"] == "gpt-4o"
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ assert spans[0].name == "chat gpt-4o"
+ assert spans[0].attributes.get("gen_ai.request.model") == "gpt-4o"
+
+
+def test_responses_streaming_starts_span_when_framework_llm_is_inactive():
+ """A direct streaming Responses call opens a span before any iteration."""
+ tracer, exporter = _real_tracer()
+ wrapper = responses(**_wrapper_kwargs(tracer))
+ wrapped = Mock()
+ wrapped.return_value.close = Mock()
+
+ result = wrapper(wrapped, object(), (), {"model": "gpt-4o", "stream": True})
+
+ assert result is not wrapped.return_value
+ assert not exporter.get_finished_spans()
+ result.close()
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ assert spans[0].name == "chat gpt-4o"
+
+
+async def test_async_responses_starts_span_when_framework_llm_is_inactive():
+ """A direct async Responses call still gets its own span."""
+ tracer, exporter = _real_tracer()
+ wrapper = async_responses(**_wrapper_kwargs(tracer))
+ wrapped = AsyncMock(return_value={"output": [], "model": "gpt-4o"})
+
+ result = await wrapper(
+ wrapped, object(), (), {"model": "gpt-4o", "stream": False}
+ )
+
+ assert result["model"] == "gpt-4o"
+ spans = exporter.get_finished_spans()
+ assert len(spans) == 1
+ assert spans[0].name == "chat gpt-4o"