Skip to content

Commit 749f737

Browse files
fix(openai-agents): restore Speech/Transcription/SpeechGroup handlers, remove AgentSpanData misfeature
- _hooks.py: remove misplaced catch-all 'elif span_data:' that was shadowing SpeechSpanData, TranscriptionSpanData, SpeechGroupSpanData, and AgentSpanData branches - _hooks.py: remove AgentSpanData handler that incorrectly propagated model settings to agent spans (test spec: agent spans must NOT carry gen_ai.request.* params) - _hooks.py: replace hardcoded "openai.agent.model.frequency_penalty" with GenAIAttributes.GEN_AI_REQUEST_FREQUENCY_PENALTY constant - tests: fix dead "llm.usage.*" prefix check, fix vestigial "gen_ai.prompt" scan, fix hardcoded frequency_penalty string, fix long line in test_realtime_session.py Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d10ff26 commit 749f737

File tree

4 files changed

+7
-43
lines changed

4 files changed

+7
-43
lines changed

packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -624,16 +624,6 @@ def on_span_end(self, span):
624624
model_settings = _extract_response_attributes(otel_span, response, trace_content)
625625
self._last_model_settings = model_settings
626626

627-
# Legacy fallback for other span types
628-
elif span_data:
629-
input_data = getattr(span_data, "input", [])
630-
_extract_prompt_attributes(otel_span, input_data, trace_content)
631-
632-
response = getattr(span_data, "response", None)
633-
if response:
634-
model_settings = _extract_response_attributes(otel_span, response, trace_content)
635-
self._last_model_settings = model_settings
636-
637627
elif (
638628
_has_realtime_spans
639629
and SpeechSpanData
@@ -687,33 +677,6 @@ def on_span_end(self, span):
687677
json.dumps([{"role": "user", "content": input_text}]),
688678
)
689679

690-
elif span_data and type(span_data).__name__ == "AgentSpanData":
691-
# For agent spans, add the model settings we stored from the response span
692-
if hasattr(self, "_last_model_settings") and self._last_model_settings:
693-
for key, value in self._last_model_settings.items():
694-
if key == "temperature":
695-
otel_span.set_attribute(
696-
GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE, value
697-
)
698-
elif key == "max_tokens":
699-
otel_span.set_attribute(
700-
GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS, value
701-
)
702-
elif key == "top_p":
703-
otel_span.set_attribute(
704-
GenAIAttributes.GEN_AI_REQUEST_TOP_P, value
705-
)
706-
elif key == "model":
707-
otel_span.set_attribute(
708-
GenAIAttributes.GEN_AI_REQUEST_MODEL, value
709-
)
710-
elif key == "frequency_penalty":
711-
otel_span.set_attribute(
712-
"openai.agent.model.frequency_penalty", value
713-
)
714-
# Note: prompt_attributes, completion_attributes, and usage tokens are now
715-
# on response spans only
716-
717680
if hasattr(span, "error") and span.error:
718681
otel_span.set_status(Status(StatusCode.ERROR, str(span.error)))
719682
else:

packages/opentelemetry-instrumentation-openai-agents/tests/test_openai_agents.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def test_dict_content_serialization(exporter):
5050

5151
spans = exporter.get_finished_spans()
5252

53-
# Look for any spans with prompt/content attributes
53+
# Look for any spans with message content attributes
5454
for span in spans:
5555
for attr_name, attr_value in span.attributes.items():
56-
prompt_content_check = ("prompt" in attr_name and "content" in attr_name) or (
57-
"gen_ai.prompt" in attr_name and "content" in attr_name
56+
prompt_content_check = (
57+
attr_name in ("gen_ai.input.messages", "gen_ai.output.messages")
5858
)
5959
if prompt_content_check:
6060
# All content attributes should be strings, not dicts
@@ -98,7 +98,7 @@ def test_agent_spans(exporter, test_agent):
9898
assert GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE not in agent_span.attributes
9999
assert GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS not in agent_span.attributes
100100
assert GenAIAttributes.GEN_AI_REQUEST_TOP_P not in agent_span.attributes
101-
assert "openai.agent.model.frequency_penalty" not in agent_span.attributes
101+
assert GenAIAttributes.GEN_AI_REQUEST_FREQUENCY_PENALTY not in agent_span.attributes
102102

103103
# Find the response span (openai.response) - this should contain prompts/completions/usage
104104
response_spans = [s for s in spans if s.name == "openai.response"]

packages/opentelemetry-instrumentation-openai-agents/tests/test_realtime_session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,5 +697,6 @@ def test_response_done_without_usage_still_captures_completion(self, tracer, tra
697697
spans = exporter.get_finished_spans()
698698
llm_spans = [s for s in spans if s.name == "openai.realtime"]
699699
assert len(llm_spans) == 1
700-
assert json.loads(llm_spans[0].attributes.get("gen_ai.output.messages"))[0]["content"] == "Why did the chicken cross the road?"
700+
output = json.loads(llm_spans[0].attributes.get("gen_ai.output.messages"))
701+
assert output[0]["content"] == "Why did the chicken cross the road?"
701702
assert llm_spans[0].attributes.get("gen_ai.usage.input_tokens") is None

packages/opentelemetry-instrumentation-openai-agents/tests/test_recipe_agents_hierarchy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ async def test_recipe_agents_hierarchy(exporter, recipe_agents):
286286

287287
# Check for usage
288288
has_usage = any(
289-
key.startswith("gen_ai.usage.") or key.startswith("llm.usage.") for key in response_span.attributes.keys()
289+
key.startswith("gen_ai.usage.") for key in response_span.attributes.keys()
290290
)
291291
assert has_usage, (
292292
f"Response span {i} should have usage attributes, attributes: {dict(response_span.attributes)}"

0 commit comments

Comments
 (0)