Skip to content

fix(openai-agents): resolve missing response field execution for Gene…#4320

Open
Toshika-Kamble wants to merge 2 commits into
traceloop:mainfrom
Toshika-Kamble:fix/generation-span-response
Open

fix(openai-agents): resolve missing response field execution for Gene…#4320
Toshika-Kamble wants to merge 2 commits into
traceloop:mainfrom
Toshika-Kamble:fix/generation-span-response

Conversation

@Toshika-Kamble

@Toshika-Kamble Toshika-Kamble commented Jun 25, 2026

Copy link
Copy Markdown

Description

This PR addresses an issue where GenerationSpanData was missing a standalone response field, preventing _extract_response_attributes from executing during on_span_end.

Changes

  • Configured a fallback to assign response_source = span_data when handling GenerationSpanData if a standalone response attribute is absent.

Verification Results

  • Ran the entire test suite locally within the package folder: 396/396 tests passed successfully.

Summary by CodeRabbit

  • Bug Fixes
    • Improved tracing reliability when finishing generation spans, including better handling of response data.
    • Prevented span-closing errors from interrupting normal operation if tracing metadata extraction fails.

@CLAassistant

CLAassistant commented Jun 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

_end_generation_span now derives response attributes from GenerationSpanData when response is absent, and it ignores errors from response attribute extraction while ending the span.

Changes

OpenAI Agents generation span end handling

Layer / File(s) Summary
Response fallback and guarded extraction
packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py
_end_generation_span selects a fallback response source for GenerationSpanData and wraps _extract_response_attributes in try/except during span closure.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐇 I hopped through spans both swift and light,
Found missing fields and made them right.
If attributes trip on closing time,
I’ll keep the trace in rhythm and rhyme.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the core fix: handling missing response on GenerationSpanData in openai-agents instrumentation.
Linked Issues check ✅ Passed The change adds a fallback for GenerationSpanData and preserves response attribute extraction on span end, matching #4157.
Out of Scope Changes check ✅ Passed The diff stays focused on the openai-agents span-end bug fix and adds only defensive handling around response extraction.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py`:
- Around line 983-989: Surface response-extraction failures in the span closure
path instead of swallowing them in `_extract_response_attributes()` handling:
keep the try/except around the `response_source` block in `_hooks.py`, but
replace the silent `pass` with recording or logging the caught exception so the
`GenerationSpanData` fallback loss of `gen_ai.output.*` attributes is visible
while preserving non-throwing behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 539f0ad5-5e66-495b-a467-853055d5055b

📥 Commits

Reviewing files that changed from the base of the PR and between fb292d0 and c6d2a10.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py

Comment on lines +983 to +989
# Wrap this tightly so an internal attribute error won't abort the entire span closure
if response_source is not None:
try:
_extract_response_attributes(otel_span, response_source, trace_content)
except Exception as e:
# This keeps the span alive and lets us see if other tests pass
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant file and nearby symbols first.
ast-grep outline packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py --view expanded || true

# Show the target region with line numbers.
sed -n '940,1010p' packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py

# Find tests touching GenerationSpanData / response extraction / response_source.
rg -n "GenerationSpanData|response_source|_extract_response_attributes|record_exception|gen_ai\.output|no response must not raise" packages/opentelemetry-instrumentation-openai-agents -g '*test*' -g '*.py'

Repository: traceloop/openllmetry

Length of output: 23296


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show any tests around the suspected behavior in the package.
fd -a . packages/opentelemetry-instrumentation-openai-agents | rg 'test|tests'

Repository: traceloop/openllmetry

Length of output: 4062


🏁 Script executed:

#!/bin/bash
set -euo pipefail

ast-grep outline packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py --view expanded || true

printf '\n--- FILE REGION ---\n'
sed -n '940,1010p' packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py

printf '\n--- MATCHES ---\n'
rg -n "GenerationSpanData|response_source|_extract_response_attributes|record_exception|gen_ai\.output|no response must not raise" packages/opentelemetry-instrumentation-openai-agents -g '*test*' -g '*.py'

Repository: traceloop/openllmetry

Length of output: 23334


🏁 Script executed:

#!/bin/bash
set -euo pipefail

fd -a . packages/opentelemetry-instrumentation-openai-agents | rg 'test|tests'

Repository: traceloop/openllmetry

Length of output: 4062


🏁 Script executed:

#!/bin/bash
set -euo pipefail

ast-grep outline packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py --view expanded || true
printf '\n--- FILE REGION ---\n'
sed -n '940,1010p' packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py
printf '\n--- MATCHES ---\n'
rg -n "GenerationSpanData|response_source|_extract_response_attributes|record_exception|gen_ai\.output|no response must not raise" packages/opentelemetry-instrumentation-openai-agents -g '*test*' -g '*.py'

Repository: traceloop/openllmetry

Length of output: 23334


🏁 Script executed:

#!/bin/bash
set -euo pipefail

ast-grep outline packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py --view expanded || true
printf '\n--- FILE REGION ---\n'
sed -n '940,1010p' packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py
printf '\n--- MATCHES ---\n'
rg -n "GenerationSpanData|response_source|_extract_response_attributes|record_exception|gen_ai\.output|no response must not raise" packages/opentelemetry-instrumentation-openai-agents -g '*test*' -g '*.py'

Repository: traceloop/openllmetry

Length of output: 23334


Surface response-extraction failures instead of swallowing them. _extract_response_attributes() can still fail here, and except Exception: pass will drop the GenerationSpanData fallback’s gen_ai.output.* attrs silently. Keep the non-throwing behavior, but record or log the exception instead.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py`
around lines 983 - 989, Surface response-extraction failures in the span closure
path instead of swallowing them in `_extract_response_attributes()` handling:
keep the try/except around the `response_source` block in `_hooks.py`, but
replace the silent `pass` with recording or logging the caught exception so the
`GenerationSpanData` fallback loss of `gen_ai.output.*` attributes is visible
while preserving non-throwing behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants