Skip to content

ref(pydantic-ai): Resolve version divergence in _compat and restructure modules - #7196

Draft
ericapisani wants to merge 1 commit into
ep/pydantic-ai-refactor-3-run-contextfrom
ep/pydantic-ai-refactor-4-compat-layout
Draft

ref(pydantic-ai): Resolve version divergence in _compat and restructure modules#7196
ericapisani wants to merge 1 commit into
ep/pydantic-ai-refactor-3-run-contextfrom
ep/pydantic-ai-refactor-4-compat-layout

Conversation

@ericapisani

Copy link
Copy Markdown
Member

Description

Issues

Reminders


Stack created with GitHub Stacks CLIGive Feedback 💬

…re modules

Introduce _compat.py, which resolves the installed pydantic-ai version and every version-dependent decision once at import time: the hooks-vs-graph-nodes model backend, the ToolManager tool-call method name, and the message part classes (now resolved individually so one upstream rename degrades only the paths needing that class). The using_request_hooks class flag and the circular-import workarounds are gone; setup_once is three composed calls with deferred imports, and importing the package loads nothing but __init__.

Restructure to the target layout: _spans.py absorbs utils.py and the spans/ package, _wrap_agent.py and _wrap_model.py (both model backends behind one install_model_backend()) and _wrap_tools.py (the two duplicated tool wrappers unified into one) replace patches/.

Review-driven fixes folded in: unknown ToolManager method names and a missing _agent_graph module now degrade gracefully instead of crashing sentry_sdk.init(), agent_run_scope removes exactly its own run from the stack so non-LIFO streaming exits cannot corrupt it, the streaming wrapper propagates exception suppression from the wrapped context manager, the after_model_request span close is guarded, and chat spans extract model info once instead of twice.
@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

112394 passed | ❌ 10 failed | ⏭️ 6755 skipped | Total: 119159 | Pass Rate: 94.32% | Execution Time: 405m 58s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -63
Passed Tests 📉 -69
Failed Tests 📈 +10
Skipped Tests 📉 -4

➕ New Tests (10)

View new tests
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_span_streaming_quart_auth_user_id_data_collection[dc_user_info_true]
    • File: tests.integrations.quart.test_quart
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing
  • test_shadowed_modules_when_importing_integrations[pydantic_ai]
    • File: tests.test_shadowed_module
    • Status: ❌ Failing

❌ Failed Tests

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.10-shadowed_module
Error: Failed: DID NOT RAISE DidNotEnable

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
    ???
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:688: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:883: in exec_module
    ???
<frozen importlib._bootstrap>:241: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:103: in test_shadowed_modules_when_importing_integrations
    with pytest.raises(integrations.DidNotEnable):
E   Failed: DID NOT RAISE DidNotEnable

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.11-shadowed_module
Error: Failed: DID NOT RAISE DidNotEnable

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
    ^^^^^^^^^^^^^^^^^^
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.11.15-linux-x86_64-gnu/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:940: in exec_module
    ???
<frozen importlib._bootstrap>:241: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:103: in test_shadowed_modules_when_importing_integrations
    with pytest.raises(integrations.DidNotEnable):
E   Failed: DID NOT RAISE DidNotEnable

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.12-shadowed_module
Error: Failed: DID NOT RAISE DidNotEnable

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
    ^^^^^^^^^^^^^^^^^^
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.12.13-linux-x86_64-gnu/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<frozen importlib._bootstrap>:1387: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:999: in exec_module
    ???
<frozen importlib._bootstrap>:488: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:103: in test_shadowed_modules_when_importing_integrations
    with pytest.raises(integrations.DidNotEnable):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   Failed: DID NOT RAISE DidNotEnable

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.13-shadowed_module
Error: Failed: DID NOT RAISE DidNotEnable

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
    ^^^^^^^^^^^^^^^^^^
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.13.14-linux-x86_64-gnu/lib/python3.13/importlib/__init__.py:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<frozen importlib._bootstrap>:1395: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:1023: in exec_module
    ???
<frozen importlib._bootstrap>:488: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:103: in test_shadowed_modules_when_importing_integrations
    with pytest.raises(integrations.DidNotEnable):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   Failed: DID NOT RAISE DidNotEnable

test_span_streaming_quart_auth_user_id_data_collection[dc_user_info_true]

File: tests.integrations.quart.test_quart
Suite: py3.14-quart-v0.21.0
Error: KeyError: 'user.id'

Stack Trace
tests/integrations/quart/test_quart.py:1223: in test_span_streaming_quart_auth_user_id_data_collection
    assert segment["attributes"]["user.id"] == "42"
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   KeyError: 'user.id'

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.14-shadowed_module
Error: Failed: DID NOT RAISE DidNotEnable

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
    ^^^^^^^^^^^^^^^^^^
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.14.7-linux-x86_64-gnu/lib/python3.14/importlib/__init__.py:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<frozen importlib._bootstrap>:1406: in _gcd_import
    ???
<frozen importlib._bootstrap>:1371: in _find_and_load
    ???
<frozen importlib._bootstrap>:1342: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:938: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:759: in exec_module
    ???
<frozen importlib._bootstrap>:491: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:103: in test_shadowed_modules_when_importing_integrations
    with pytest.raises(integrations.DidNotEnable):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   Failed: DID NOT RAISE DidNotEnable

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.14t-shadowed_module
Error: Failed: DID NOT RAISE DidNotEnable

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
    ^^^^^^^^^^^^^^^^^^
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.14.6+freethreaded-linux-x86_64-gnu/lib/python3.14t/importlib/__init__.py:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<frozen importlib._bootstrap>:1406: in _gcd_import
    ???
<frozen importlib._bootstrap>:1371: in _find_and_load
    ???
<frozen importlib._bootstrap>:1342: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:938: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:759: in exec_module
    ???
<frozen importlib._bootstrap>:491: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:103: in test_shadowed_modules_when_importing_integrations
    with pytest.raises(integrations.DidNotEnable):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   Failed: DID NOT RAISE DidNotEnable

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.7-shadowed_module
Error: Failed: DID NOT RAISE <class 'sentry_sdk.integrations.DidNotEnable'>

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
/usr/local/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1006: in _gcd_import
    ???
<frozen importlib._bootstrap>:983: in _find_and_load
    ???
<frozen importlib._bootstrap>:967: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:677: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:728: in exec_module
    ???
<frozen importlib._bootstrap>:219: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:104: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
E   Failed: DID NOT RAISE <class 'sentry_sdk.integrations.DidNotEnable'>

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.8-shadowed_module
Error: Failed: DID NOT RAISE <class 'sentry_sdk.integrations.DidNotEnable'>

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.8-linux-x86_64-gnu/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1014: in _gcd_import
    ???
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:843: in exec_module
    ???
<frozen importlib._bootstrap>:219: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:104: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
E   Failed: DID NOT RAISE <class 'sentry_sdk.integrations.DidNotEnable'>

test_shadowed_modules_when_importing_integrations[pydantic_ai]

File: tests.test_shadowed_module
Suite: py3.9-shadowed_module
Error: Failed: DID NOT RAISE <class 'sentry_sdk.integrations.DidNotEnable'>

Stack Trace
sentry_sdk/integrations/pydantic_ai/__init__.py:4: in <module>
    import pydantic_ai  # noqa: F401
E   ModuleNotFoundError: No module named 'pydantic_ai'

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:86: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
../../_temp/uv-python-dir/cpython-3.9-linux-x86_64-gnu/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1030: in _gcd_import
    ???
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:850: in exec_module
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
sentry_sdk/integrations/pydantic_ai/__init__.py:6: in <module>
    raise DidNotEnable("pydantic-ai not installed")
E   sentry_sdk.integrations.DidNotEnable: pydantic-ai not installed

During handling of the above exception, another exception occurred:
tests/test_shadowed_module.py:104: in test_shadowed_modules_when_importing_integrations
    importlib.import_module(module_path)
E   Failed: DID NOT RAISE <class 'sentry_sdk.integrations.DidNotEnable'>

✅ Patch coverage is 92.37%. Project has 2462 uncovered lines.
✅ Project coverage is 90.28%. Comparing base (base) to head (head).

Files with missing lines (6)
File Patch % Lines
sentry_sdk/integrations/pydantic_ai/_wrap_model.py 89.62% ⚠️ 11 Missing and 12 partials
sentry_sdk/integrations/pydantic_ai/_spans.py 94.48% ⚠️ 8 Missing and 8 partials
sentry_sdk/integrations/pydantic_ai/_compat.py 89.47% ⚠️ 4 Missing and 2 partials
sentry_sdk/integrations/pydantic_ai/_wrap_tools.py 92.86% ⚠️ 3 Missing and 3 partials
sentry_sdk/integrations/pydantic_ai/_wrap_agent.py 92.31% ⚠️ 1 Missing and 1 partials
sentry_sdk/integrations/pydantic_ai/_run_context.py 100.00% ⚠️ 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    90.23%    90.28%    +0.05%
==========================================
  Files          193       190        -3
  Lines        25361     25323       -38
  Branches      9302      9280       -22
==========================================
+ Hits         22883     22861       -22
- Misses        2478      2462       -16
- Partials      1432      1425        -7

Generated by Codecov Action

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant