ref(pydantic-ai): Consolidate run-scoped state into one context manager - #7195
Draft
ericapisani wants to merge 1 commit into
Draft
ref(pydantic-ai): Consolidate run-scoped state into one context manager#7195ericapisani wants to merge 1 commit into
ericapisani wants to merge 1 commit into
Conversation
Replace the hand-rolled push/pop contextvar stack with _run_context.py: one AgentRun dataclass on a token-reset contextvar, managed by a single agent_run_scope() context manager so pairing no longer relies on manual try/finally in three places. The streaming wrapper's manual scope/span/stack bookkeeping collapses into one ExitStack, which also stops leaking the isolation scope and invoke_agent span when entering the wrapped context manager fails, and the token reset tolerates streaming runs that exit in a different asyncio task. The request hooks deliberately keep pairing chat spans through RunContext.metadata: contextvar pairing can mispair overlapping run_stream context managers in the same task, while the metadata dict is correct per run by construction. Review-driven hardening folded in: hook bodies are wrapped in capture_internal_exceptions so span-data failures cannot abort the user's model request, invoke_agent spans now receive model data from the agent argument instead of the not-yet-pushed contextvar, the model-settings mapping has a single source of truth in _extract.py, and the usage extractor passes record_token_usage kwargs directly.
Contributor
Codecov Results 📊✅ 112337 passed | ❌ 1 failed | ⏭️ 6755 skipped | Total: 119093 | Pass Rate: 94.33% | Execution Time: 412m 58s 📊 Comparison with Base Branch
➕ New Tests (1)View new tests
❌ Failed Tests
|
| File | Patch % | Lines |
|---|---|---|
| sentry_sdk/integrations/pydantic_ai/patches/agent_run.py | 100.00% | |
| sentry_sdk/integrations/pydantic_ai/init.py | 85.71% | |
| sentry_sdk/integrations/pydantic_ai/_run_context.py | 92.59% |
Coverage diff
@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 90.23% 90.23% —%
==========================================
Files 193 195 +2
Lines 25361 25382 +21
Branches 9302 9278 -24
==========================================
+ Hits 22883 22901 +18
- Misses 2478 2481 +3
- Partials 1432 1422 -10Generated by Codecov Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Issues
Reminders
uv run ruff.feat:,fix:,ref:,meta:)Stack created with GitHub Stacks CLI • Give Feedback 💬