Skip to content

Conversation

@constantinius
Copy link
Contributor

Description

Adds instrumentation for Runner.run_streamed

Issues

Closes https://linear.app/getsentry/issue/TET-1705/openai-agents-streaming-support

@linear
Copy link

linear bot commented Jan 12, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Jan 13, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • feat(anthropic): Set system instruction attribute by alexander-alderman-webb in #5353
  • feat(google-genai): Set system instruction attribute by alexander-alderman-webb in #5354
  • feat(integrations): openai-agents streaming support by constantinius in #5291
  • feat(langchain): Set system instruction attribute by alexander-alderman-webb in #5357
  • feat(openai-agents): Set system instruction attribute by alexander-alderman-webb in #5355
  • feat(pydantic-ai): Set system instruction attribute by alexander-alderman-webb in #5356

Internal Changes 🔧

  • ci(release): Fix changelog-preview permissions by BYK in #5368
  • ci: Fix path in AI integration tests by alexander-alderman-webb in #5347

🤖 This preview updates automatically when you update the PR.

@constantinius constantinius marked this pull request as ready for review January 13, 2026 11:33
@constantinius constantinius requested a review from a team as a code owner January 13, 2026 11:33
cursor[bot]

This comment was marked as outdated.

… streaming events

- Introduced a `_close_workflow_span` function to ensure proper closure of workflow spans.
- Wrapped `stream_events` and `cancel` methods in `run_result` to manage workflow spans during their execution.
- This change improves resource management and error handling in the OpenAI agent integration.
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

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

looks good overall, only a few comments

Comment on lines +139 to +141
except Exception:
_close_streaming_workflow_span(agent)
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to close the workflow span here?
my understanding is that a new agent takes over with a handoff, so terminating the agent in finally would make sense?
let me know if I am missing anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I can tell this is the correct handling. The failed handoff will cause the workflow to stop, but a successful handoff should not terminate the workflow span

Copy link
Contributor

Choose a reason for hiding this comment

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

okay I understand, I was confusing workflow and agent invocation spans 😢

can you check if it is possible to to use the context manager in a with statement in run_streamed as well? I know we catch exceptions at various levels to close the workflow span, but these exceptions should bubble up to the top-level run_streamed if I have the correct mental model. Something like

with agent_workflow_span(agent):
	...

instead of all the manual management 🙏 .

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 5 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

# If run_streamed itself fails (not the background task), clean up immediately
workflow_span.__exit__(*sys.exc_info())
_capture_exception(exc)
raise exc from None
Copy link

Choose a reason for hiding this comment

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

Exception handling missing capture_internal_exceptions wrapper

Medium Severity

The _capture_exception(exc) call isn't wrapped in capture_internal_exceptions(), unlike the consistent pattern used elsewhere in the codebase (lines 43, 61). If _capture_exception raises an exception, it will replace the user's original exception, potentially hiding the root cause of the failure.

Fix in Cursor Fix in Web

# If run_streamed itself fails (not the background task), clean up immediately
workflow_span.__exit__(*sys.exc_info())
_capture_exception(exc)
raise exc from None
Copy link

Choose a reason for hiding this comment

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

Inconsistent exception reraising loses traceback information

Low Severity

Using raise exc from None suppresses the exception chain and doesn't preserve the full traceback, while the non-streaming version uses reraise(*exc_info) to maintain traceback integrity. This inconsistency makes debugging streaming failures more difficult compared to non-streaming failures.

Fix in Cursor Fix in Web


except Exception:
_close_streaming_workflow_span(agent)
raise
Copy link

Choose a reason for hiding this comment

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

Missing exception safety wrapper in handoff error handling

High Severity

The _close_streaming_workflow_span(agent) call in the except block isn't wrapped in capture_internal_exceptions(), unlike the identical pattern at line 226. If closing the workflow span fails, it will replace the original handoff exception, hiding the root cause. The PR discussion explicitly flagged this issue and marked it as "Fixed", but the fix wasn't applied here.

Fix in Cursor Fix in Web

if agent and context_wrapper and _has_active_agent_span(context_wrapper):
end_invoke_agent_span(context_wrapper, agent, final_output)
# For streaming, close the workflow span (non-streaming uses context manager in _create_run_wrapper)
_close_streaming_workflow_span(agent)
Copy link

Choose a reason for hiding this comment

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

Missing exception safety in final output cleanup

Medium Severity

The _close_streaming_workflow_span(agent) call in the finally block isn't wrapped in capture_internal_exceptions(). If span cleanup fails during exception handling (e.g., when original_execute_final_output at line 164 raises), the cleanup exception will replace the original exception, hiding the actual failure cause.

Fix in Cursor Fix in Web

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.

3 participants