-
Notifications
You must be signed in to change notification settings - Fork 567
feat(integrations): implement context management for invoke_agent spans #5089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat(integrations): implement context management for invoke_agent spans #5089
Conversation
- Introduced context variables to manage nested invoke_agent spans safely. - Added functions to push and pop spans from the context stack. - Updated existing code to utilize context variables instead of Sentry scope for agent management. - Enhanced execute_tool_span to support parent-child relationships between spans. This change improves the handling of agent spans during nested calls, ensuring better traceability and isolation of spans in asynchronous contexts.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #5089 +/- ##
==========================================
- Coverage 83.94% 83.94% -0.01%
==========================================
Files 179 179
Lines 17948 17970 +22
Branches 3193 3198 +5
==========================================
+ Hits 15067 15085 +18
- Misses 1909 1911 +2
- Partials 972 974 +2
|
| self._span.__enter__() | ||
|
|
||
| # Push span and agent to contextvar stack | ||
| push_invoke_agent_span(self._span, self.agent, self.is_streaming) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Orphaned Context Entries Corrupt Stack
In _StreamingContextManagerWrapper.__aenter__, if an exception occurs after calling push_invoke_agent_span on line 51 but before the method completes (e.g., when entering the original context manager on line 54), the __aexit__ method will not be called, leaving an orphaned entry on the contextvar stack that never gets popped. This causes stack corruption for subsequent operations in the same context.
| finally: | ||
| sentry_sdk.get_current_scope().remove_context("pydantic_ai_agent") | ||
| # Pop span from contextvar stack | ||
| pop_invoke_agent_span() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Span Context: Premature Exit
The pop_invoke_agent_span() call in the finally block executes before the with statement's implicit span.__exit__() call. This means the span is removed from the contextvar stack while it's still active and before its cleanup runs. The correct approach would be to pop the span after the with block completes, not inside its finally clause.
This change improves the handling of agent spans during nested calls, ensuring better traceability and isolation of spans in asynchronous contexts.
Description
Issues
Reminders
tox -e linters.feat:,fix:,ref:,meta:)