Skip to content

Conversation

@constantinius
Copy link
Contributor

Description

Integration for Pydantic AI. Support for sync and async (streaming and non-streaming)

Issues

Closes https://linear.app/getsentry/issue/TET-1242/python-sdk-pydantic-ai-integration

Reminders

@linear
Copy link

linear bot commented Oct 9, 2025

@constantinius constantinius requested a review from a team October 9, 2025 16:47
@sentrivana
Copy link
Contributor

Hey, added a commit to run the new test suite in ci c7e7ec2 -- the script updated the test matrix for other test suites too and some of them are now failing; I'll fix that on master and then we can rebase.

@sentrivana sentrivana mentioned this pull request Oct 10, 2025
sentrivana added a commit that referenced this pull request Oct 10, 2025
### Description
Updating tox + reorg the AI group alphabetically.

New openai release doesn't work on 3.8, explicitly testing on 3.9+ from
there

Doing this now to unblock
#4906 (comment)

#### Issues
<!--
* resolves: #1234
* resolves: LIN-1234
-->

#### Reminders
- Please add tests to validate your changes, and lint your code using
`tox -e linters`.
- Add GH Issue ID _&_ Linear ID (if applicable)
- PR title should use [conventional
commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type)
style (`feat:`, `fix:`, `ref:`, `meta:`)
- For external contributors:
[CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md),
[Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord
community](https://discord.gg/Ww9hbqr)
@sentrivana
Copy link
Contributor

Unrelated ci failures fixed.

Do we have test apps/scripts for this? If so would be awesome if you could add them to https://github.com/getsentry/testing-sentry-python like Vjeran did for google-genai in getsentry/testing-sentry-python#1

@constantinius
Copy link
Contributor Author

@sentrivana I've pushed all tests I have here

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.

Some initial comments since I am not sure I'll get to reviewing everything in detail. There should be reviews from other SDK members coming.

Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Had a first look and left some comments. Some general feedback:

  • There's a lot of imports in functions, I've pointed out some but not all. Please go through all non-top-level imports and if it doesn't break anything, move them to the other imports at the top of the file.

  • The code is not easy to follow, mostly because related parts are disconnected.

    • Big part of this is that span management is scattered, e.g. a span is created in a specific function/file, then __enter__ed in a different file, and the corresponding __exit__ is someplace else still. Sometimes, we don't even know if a span is active, like in sentry_sdk/integrations/pydantic_ai/patches/agent_run.py, which shouldn't be the case:
                        # It could be that there is an "invoke agent" span still open
                        current_span = sentry_sdk.get_current_span()
                        if current_span is not None and current_span.timestamp is None:
                            current_span.__exit__(None, None, None)

    It might be that most of this scattered management is simply a byproduct of how Pydantic AI/agentic flows work in general. But if we can do something to improve the flow we should.

    • In general you need to do a lot of jumping around in the code to follow a specific code path. I think a big part of this is the number of new files introduced by this PR. Can we consolidate these a bit? I.e., if functions from a single file are only used in one other file, can we move them there?

This would make the code more reviewable for me, since right now I'm struggling to grasp the bigger picture. 🙂

Co-authored-by: Ivana Kellyer <ivana.kellyer@sentry.io>
cursor[bot]

This comment was marked as outdated.

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Optional Dependency Import Error

The import agents statement is now unconditional and outside the try...except block in both tools.py and utils.py. If the agents package is not installed, this raises an ImportError at module load time instead of gracefully raising DidNotEnable as expected for optional dependencies.

Additional Locations (1)

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Import Failure Causes isinstance TypeError

The isinstance check for FunctionTool can raise a TypeError. If the from agents import FunctionTool import fails, FunctionTool is set to None, which isinstance doesn't accept as a type.

Additional Locations (1)

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: ImportError Handling Regression

The import agents statement was moved outside its try-except block in tools.py and utils.py. If the agents package is not installed, this now raises an unhandled ImportError instead of gracefully disabling the integration or setting related classes to None.

Additional Locations (1)

Fix in Cursor Fix in Web

…s/utils.py sentry_sdk/integrations/openai_agents/spans/execute_tool.py sentry_sdk/integrations/openai_agents/utils.py
@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Pydantic AI Integration Fails Without Installation

The pydantic_ai integration raises DidNotEnable at module import time when pydantic-ai isn't installed. This prevents the integration module from being imported, which breaks the SDK's integration discovery and can cause initialization failures.

Fix in Cursor Fix in Web

Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Thanks for consolidating! Couple more comments, will try it out now.

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Default Mismatch Causes Prompt Omission

The _should_send_prompts function uses getattr with a default of False for include_prompts. This conflicts with the PydanticAIIntegration's default of True for this setting, meaning prompts may not be sent when they should be by default if the attribute is missing.

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Module-Level Handling Causes Integration Load Failure

The pydantic_ai import and DidNotEnable exception are handled at the module level. This prevents the integration from loading if pydantic-ai isn't installed, which goes against the expected pattern of raising DidNotEnable within setup_once().

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Pydantic AI Integration Fails Without Explicit Installation

The pydantic_ai integration raises DidNotEnable at module import time if pydantic_ai isn't installed. This prevents the integration from initializing and can cause Sentry SDK setup to fail, even when the integration isn't explicitly enabled, differing from how other integrations handle optional dependencies.

Fix in Cursor Fix in Web

Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Lemme know if you need help with mypy complaining etc

@constantinius constantinius enabled auto-merge (squash) October 23, 2025 12:14
@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Missing Python Version Constraint Causes Installation Failures

The pydantic_ai integration config in config.py is missing its Python version constraint. The pydantic-ai package requires Python >=3.10 per releases.jsonl, so populate_tox generates test environments for unsupported Python versions, causing installation failures.

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Sentry Instrumentation Missing for Sync Agent Runs

The _patch_agent_run function currently patches Agent.run, Agent.run_stream, and Agent.run_stream_events, but it misses Agent.run_sync. This means synchronous agent invocations won't create Sentry spans, even though the test suite suggests they should be instrumented.

Fix in Cursor Fix in Web

@sentrivana sentrivana disabled auto-merge October 23, 2025 13:11
@sentrivana sentrivana enabled auto-merge (squash) October 23, 2025 13:19
@sentrivana sentrivana merged commit 1b74827 into master Oct 23, 2025
124 checks passed
@sentrivana sentrivana deleted the constantinius/feat/integration/pydantic-ai branch October 23, 2025 13:22
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