Skip to content

Fix Pydantic AI docs in integration class docstring (Fixes #5293)#5729

Open
harryautomazione wants to merge 4 commits intogetsentry:masterfrom
harryautomazione:fix-issue-5293
Open

Fix Pydantic AI docs in integration class docstring (Fixes #5293)#5729
harryautomazione wants to merge 4 commits intogetsentry:masterfrom
harryautomazione:fix-issue-5293

Conversation

@harryautomazione
Copy link

What

  • Update PydanticAIIntegration docstring in sentry_sdk/integrations/pydantic_ai/__init__.py.
  • Correct installation example to:
    • pip install sentry-sdk[pydantic_ai]
  • Correct Pydantic AI usage example:
    • change result_type=SupportResponse to output_type=SupportResponse
    • change result.data to result.output

Why

  • Issue Fix Pydantic AI docs #5293 reports doc mismatch between Sentry console and docs for Pydantic AI.
  • The existing snippet uses obsolete parameter/attribute names and can mislead users.
  • Align SDK docstring with current Pydantic AI API behavior.

How

  • Edit PydanticAIIntegration class docstring to include:
    • coherent install command with extra ([pydantic_ai])
    • usage snippet using output_type and result.output
  • Keep changes local to docstring only (behavior unchanged).

Testing

  • Run relevant tests:
    • pytest tests/integrations/pydantic_ai/test_pydantic_ai.py
  • Validate that integration path still passes with all pydantic_ai tests.
  • Manual check: docstring example compiles and is consistent with unit tests.

Risks / Impact

  • Low risk: documentation-only change.
  • No runtime behavior changes in SDK.
  • Improves developer experience and avoids API confusion.

Checklist

  • Linked issue: Fixes #5293
  • Problem and impact described
  • Solution described
  • Tests referenced
  • No secrets introduced

…mand, parameter name, and attribute

- Change install command to include [pydantic_ai] extra
- Change result_type to output_type in example
- Change result.data to result.output in example

This ensures the docstring matches the current Pydantic AI API and correct installation instructions.

Fixes getsentry#5293
@harryautomazione harryautomazione requested a review from a team as a code owner March 20, 2026 22:26
@github-actions
Copy link
Contributor

github-actions bot commented Mar 20, 2026

Semver Impact of This PR

None (no version bump detected)

📋 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 ✨

  • (asgi) Add option to disable suppressing chained exceptions by alexander-alderman-webb in #5714
  • (logging) Separate ignore lists for events/breadcrumbs and sentry logs by sl0thentr0py in #5698

Bug Fixes 🐛

Anthropic

  • Set exception info on streaming span when applicable by alexander-alderman-webb in #5683
  • Patch AsyncStream.close() and AsyncMessageStream.close() to finish spans by alexander-alderman-webb in #5675
  • Patch Stream.close() and MessageStream.close() to finish spans by alexander-alderman-webb in #5674

Documentation 📚

  • Add note on AI PRs to CONTRIBUTING.md by sentrivana in #5696

Internal Changes 🔧

  • Add -latest alias for each integration test suite by sentrivana in #5706
  • Use date-based branch names for toxgen PRs by sentrivana in #5704
  • 🤖 Update test matrix with new releases (03/19) by github-actions in #5703
  • Add client report tests for span streaming by sentrivana in #5677

Other

  • Update CHANGELOG.md by sentrivana in #5685

🤖 This preview updates automatically when you update the PR.

…gation

- Add CURRENT_LANGCHAIN_AGENT_NAME contextvar to track agent name across spans
- Set agent name in agent executor wrappers (invoke/stream)
- Propagate agent name to all child spans via _create_span
- Add test to verify agent name is set on all spans
finally:
# Ensure cleanup happens even if iterator is abandoned or fails
_pop_agent()
span.__exit__(*exc_info)
Copy link

Choose a reason for hiding this comment

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

Bug: Calling CURRENT_LANGCHAIN_AGENT_NAME.reset(token) in the new_iterator_async generator will raise a ValueError because the token was created in a different, synchronous context.
Severity: HIGH

Suggested Fix

The ContextVar token should be set and reset within the same context. Move the CURRENT_LANGCHAIN_AGENT_NAME.set(agent_name) call inside the new_iterator_async generator, so that both setting the variable and resetting it happen within the async generator's context. This will ensure the token is valid when reset() is called in the finally block.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: sentry_sdk/integrations/langchain.py#L1103

Potential issue: The `token` for the `CURRENT_LANGCHAIN_AGENT_NAME` context variable is
created in the synchronous function `new_stream`. However, the `new_iterator_async`
generator, which runs in a separate, copied context, attempts to reset this token in its
`finally` block. According to Python's context variable specification (PEP 567), calling
`reset()` with a token from a different context raises a `ValueError`. This will cause
any async streaming agent calls to crash during cleanup when the generator is exhausted
or closed. The synchronous version is unaffected as it operates within the same context.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.



def _push_agent(agent_name: "Optional[str]") -> None:
"""Push an agent name onto the stack."""
Copy link

Choose a reason for hiding this comment

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

Stale comment now misleadingly annotates unrelated function

Low Severity

The comment # Contextvar to track agent names in a stack for re-entrant agent support originally documented the _agent_stack ContextVar that was removed in this diff. The comment was left behind and now sits directly above _get_system_instructions, making it look like it describes that unrelated function. This is misleading for anyone reading the code.

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.

1 participant