Skip to content

Conversation

@constantinius
Copy link
Contributor

Description

chat and execute tool spans under an invoke agent span should have the agent name property set

Issues

Closes https://linear.app/getsentry/issue/TET-1293/make-sure-that-agent-name-is-set-on-all-of-its-gen-ai-children

@constantinius constantinius requested a review from a team as a code owner October 24, 2025 13:44
@linear
Copy link

linear bot commented Oct 24, 2025

@constantinius constantinius requested a review from a team October 24, 2025 13:45
cursor[bot]

This comment was marked as outdated.

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

❌ Patch coverage is 36.36364% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.90%. Comparing base (76cc416) to head (5043e91).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/langchain.py 36.36% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5030      +/-   ##
==========================================
- Coverage   83.93%   83.90%   -0.03%     
==========================================
  Files         179      179              
  Lines       17890    17901      +11     
  Branches     3181     3183       +2     
==========================================
+ Hits        15016    15020       +4     
- Misses       1909     1914       +5     
- Partials      965      967       +2     
Files with missing lines Coverage Δ
sentry_sdk/integrations/langchain.py 67.93% <36.36%> (-0.80%) ⬇️

sentrivana added a commit that referenced this pull request Oct 29, 2025
### Description
Cherry-picked off of
#5030

#### Issues
Ref
https://linear.app/getsentry/issue/TET-1293/make-sure-that-agent-name-is-set-on-all-of-its-gen-ai-children

#### 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)

---------

Co-authored-by: Fabian Schindler <fabian.schindler@sentry.io>
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.

I took the google-genai part out of this PR and merged it separately since it was straightforward: #5038

Regarding the Langchain part, we talked about this and I also thought about it a bit more. Documenting my reasoning and the proposed path forward for this sort of pattern here.

The problem: We need access to some package-internal object (in this case the agent) to be able to extract something from it in different places in the integration.

In this PR, we're opting for storing it on the current scope in the _contexts. I'm unhappy about this since we're misusing a field whose purpose is something entirely different (populating contexts on events) to store something integration specific.

Other solutions that have been proposed:

  • Store it on something coming from Langchain directly that gets passed around and is accessible everywhere we need it. This is the usual way of doing things, but we couldn't find a good candidate here.
  • Store it on the parent transaction and access it from there. The problem here is there might be different agents in a transaction.
  • Store it on a parent span and access it from here. But there is no good way of traversing the span tree.

TL;DR: recommended way forward:

My recommended solution would be to store this in a ContextVar on the top level of the integration. That way, we have the same execution context isolation that we have with scopes (they're also context vars), but we're not misusing _contexts, and we're keeping the change contained to the integration.

Alternatively, can this be filled in in relay? I.e., we set the agent name on the topmost span that makes sense, and relay copies it to all child spans where it makes sense.

shellmayr pushed a commit that referenced this pull request Oct 30, 2025
### Description
Cherry-picked off of
#5030

#### Issues
Ref
https://linear.app/getsentry/issue/TET-1293/make-sure-that-agent-name-is-set-on-all-of-its-gen-ai-children

#### 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)

---------

Co-authored-by: Fabian Schindler <fabian.schindler@sentry.io>
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