Skip to content

Conversation

@constantinius
Copy link
Contributor

  • 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.

Description

Issues

Reminders

- 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.
@constantinius constantinius requested a review from a team as a code owner November 10, 2025 16:13
@linear
Copy link

linear bot commented Nov 10, 2025

@codecov
Copy link

codecov bot commented Nov 10, 2025

Codecov Report

❌ Patch coverage is 91.11111% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.94%. Comparing base (2f966c6) to head (c7b7ae7).
⚠️ Report is 6 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/pydantic_ai/utils.py 89.28% 1 Missing and 2 partials ⚠️
...ntry_sdk/integrations/pydantic_ai/patches/tools.py 80.00% 0 Missing and 1 partial ⚠️
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     
Files with missing lines Coverage Δ
..._sdk/integrations/pydantic_ai/patches/agent_run.py 88.88% <100.00%> (-0.27%) ⬇️
...ry_sdk/integrations/pydantic_ai/spans/ai_client.py 76.11% <100.00%> (-0.14%) ⬇️
...sdk/integrations/pydantic_ai/spans/execute_tool.py 100.00% <100.00%> (ø)
...sdk/integrations/pydantic_ai/spans/invoke_agent.py 82.69% <100.00%> (+0.33%) ⬆️
...ntry_sdk/integrations/pydantic_ai/patches/tools.py 78.37% <80.00%> (-0.57%) ⬇️
sentry_sdk/integrations/pydantic_ai/utils.py 88.88% <89.28%> (-0.40%) ⬇️

... and 5 files with indirect coverage changes

self._span.__enter__()

# Push span and agent to contextvar stack
push_invoke_agent_span(self._span, self.agent, self.is_streaming)
Copy link

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.

Fix in Cursor Fix in Web

finally:
sentry_sdk.get_current_scope().remove_context("pydantic_ai_agent")
# Pop span from contextvar stack
pop_invoke_agent_span()
Copy link

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.

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.

2 participants