Skip to content

feat(pydantic-ai): Add tool description to execute_tool spans#5596

Merged
ericapisani merged 27 commits intomasterfrom
ep/add-tool-description-to-pydantic-ai-spans
Mar 9, 2026
Merged

feat(pydantic-ai): Add tool description to execute_tool spans#5596
ericapisani merged 27 commits intomasterfrom
ep/add-tool-description-to-pydantic-ai-spans

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented Mar 5, 2026

Add the tool's description to gen_ai.execute_tool spans via the gen_ai.tool.description span data attribute.

When a pydantic-ai tool is defined with a docstring, that docstring becomes the tool's description in its ToolDefinition. This PR surfaces that description in the corresponding Sentry span, making it easier to understand what each tool does when inspecting traces.

The description is sourced from tool.tool_def.description on the internal tool object. If no description is available (e.g. the tool has no docstring), the attribute value is None.

Include the tool's description (from its docstring) in gen_ai.execute_tool
spans via the gen_ai.tool.description span data attribute. The description
is sourced from the tool's ToolDefinition, which is derived from the
tool's docstring.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 5, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (pydantic-ai) Add tool description to execute_tool spans by ericapisani in #5596

Bug Fixes 🐛

  • (celery) Propagate user-set headers by sentrivana in #5581
  • (utils) Avoid double serialization of strings in safe_serialize by ericapisani in #5587

Documentation 📚

  • (openai-agents) Remove inapplicable comment by alexander-alderman-webb in #5495
  • Add AGENTS.md by sentrivana in #5579
  • Add set_attribute example to changelog by sentrivana in #5578

Internal Changes 🔧

Openai Agents

  • Replace mocks with httpx in MCP tool tests by alexander-alderman-webb in #5605
  • Replace mocks with httpx in handoff tests by alexander-alderman-webb in #5604
  • Replace mocks with httpx in API error test by alexander-alderman-webb in #5601
  • Replace mocks with httpx in non-error single-response tests by alexander-alderman-webb in #5600
  • Remove test for unreachable state by alexander-alderman-webb in #5584
  • Expect namespace tool field for new openai versions by alexander-alderman-webb in #5599

Other

  • Do not run actions on potel-base by sentrivana in #5614

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 5, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.66s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 13870 uncovered lines.
✅ Project coverage is 30.72%. Comparing base (base) to head (head).

Files with missing lines (2)
File Patch % Lines
tools.py 0.00% ⚠️ 90 Missing
execute_tool.py 0.00% ⚠️ 22 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    25.63%    30.72%    +5.09%
==========================================
  Files          189       189         —
  Lines        20016     20020        +4
  Branches      6476      6478        +2
==========================================
+ Hits          5131      6150     +1019
- Misses       14885     13870     -1015
- Partials       421       468       +47

Generated by Codecov Action

ericapisani and others added 4 commits March 6, 2026 07:42
Move Optional inside quotes for consistency with type annotation
style conventions.

Co-Authored-By: Claude Haiku 4.5 <[email protected]>
….com:getsentry/sentry-python into ep/add-tool-description-to-pydantic-ai-spans
When a tool has no description, the tool_description field should be
present in the span data but have a None value. Update the assertion
to check for field presence rather than field absence.

Also use `is None` instead of `== None` for proper Python style.

Co-Authored-By: Claude Haiku 4.5 <[email protected]>
Comment thread tests/integrations/pydantic_ai/test_pydantic_ai.py Outdated
@ericapisani ericapisani marked this pull request as ready for review March 6, 2026 14:31
@ericapisani ericapisani requested a review from a team as a code owner March 6, 2026 14:31
Comment thread sentry_sdk/integrations/pydantic_ai/spans/execute_tool.py Outdated
Use getattr with a None default when accessing tool_definition.description
to prevent AttributeError if the attribute is missing.

Co-Authored-By: Claude Haiku 4.5 <[email protected]>
Copy link
Copy Markdown
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.

Very nice work! See the two comments I've left.

I know there are plenty of existing pydantic-ai tests calling execute_tool_span() as if it's part of the public API we offer. I don't believe it's intended for users to call these functions directly however. As such, they are considered private.

Comment thread tests/integrations/pydantic_ai/test_pydantic_ai.py Outdated
Comment thread sentry_sdk/integrations/pydantic_ai/spans/execute_tool.py Outdated