Skip to content

🐛 Bug Report: GenerationSpanData missing response field causes _extract_response_attributes to never execute #4157

@heyanlong

Description

@heyanlong

Which component is this bug for?

OpenAI Instrumentation

📜 Description

GenerationSpanData missing response field causes _extract_response_attributes to never execute

@dont_throw
    def on_span_end(self, span):
        """Called when a span ends - finish OpenTelemetry span."""
        from agents import GenerationSpanData

        if not span or not hasattr(span, "span_data"):
            return

        if span in self._otel_spans:
            otel_span = self._otel_spans[span]
            span_data = getattr(span, "span_data", None)
            trace_content = should_send_prompts()
            if span_data and (
                type(span_data).__name__ == "ResponseSpanData"
                or isinstance(span_data, GenerationSpanData)
            ):
                # Extract prompt data from input
                input_data = getattr(span_data, "input", [])
                _extract_prompt_attributes(otel_span, input_data, trace_content)

                # Add function/tool specifications to the request using OpenAI semantic conventions
                response = getattr(span_data, "response", None) # if span_data is GenerationSpanData, so _extract_response_attributes to never execute
                if response:
                    model_settings = _extract_response_attributes(otel_span, response, trace_content)
                    self._last_model_settings = model_settings

👟 Reproduction steps

from opentelemetry.instrumentation.openai_agents import OpenAIAgentsInstrumentor

trace.set_tracer_provider(trace_provider)

OpenAIAgentsInstrumentor(
    replace_existing_processors=True
).instrument(
    agent_name="TestAgent",
    tracer_provider=trace_provider,
)

agent = Agent(
            name=name,
            instructions=f"{RECOMMENDED_PROMPT_PREFIX}\n\n{mark_instructions}",
            model=LitellmModel(model=model),
            model_settings=ModelSettings(
                include_usage=True
            ))
Runner.run_streamed(agent, messages, context=context, **kwargs)

👍 Expected behavior

There should be gen_ai.output.xxx

👎 Actual Behavior with Screenshots

Image

🤖 Python Version

No response

📃 Provide any additional context for the Bug.

No response

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

Are you willing to submit PR?

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions