Skip to content

Conversation

@sentrivana
Copy link
Contributor

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

@linear
Copy link

linearbot commented Oct 29, 2025

@sentrivanasentrivana marked this pull request as ready for review October 29, 2025 09:09
@sentrivanasentrivana requested a review from a team as a code ownerOctober 29, 2025 09:09
Copy link

@cursorcursorbot left a comment

Choose a reason for hiding this comment

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

Bug: Agent Name Missing in Async Non-Streaming Wrapper

The _wrap_async_generate_content function is missing the line that sets SPANDATA.GEN_AI_AGENT_NAME on the chat_span. This line was added to the other three wrapper functions (_wrap_generate_content_stream, _wrap_async_generate_content_stream, and _wrap_generate_content), but was not added here. This causes the async non-streaming variant to be inconsistent with the other implementations and fails to meet the PR's goal of ensuring agent name is set on all gen_ai children.

sentry_sdk/integrations/google_genai/__init__.py#L257-L301

def_wrap_async_generate_content(f):
# type: (Callable[..., Any]) -> Callable[..., Any]
@wraps(f)
asyncdefnew_async_generate_content(self, *args, **kwargs):
# type: (Any, Any, Any) -> Any
integration=sentry_sdk.get_client().get_integration(GoogleGenAIIntegration)
ifintegrationisNone:
returnawaitf(self, *args, **kwargs)
model, contents, model_name=prepare_generate_content_args(args, kwargs)
withget_start_span_function()(
op=OP.GEN_AI_INVOKE_AGENT,
name="invoke_agent",
origin=ORIGIN,
) asspan:
span.set_data(SPANDATA.GEN_AI_AGENT_NAME, model_name)
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent")
set_span_data_for_request(span, integration, model_name, contents, kwargs)
withsentry_sdk.start_span(
op=OP.GEN_AI_CHAT,
name=f"chat {model_name}",
origin=ORIGIN,
) aschat_span:
chat_span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
chat_span.set_data(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
chat_span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
set_span_data_for_request(
chat_span, integration, model_name, contents, kwargs
)
try:
response=awaitf(self, *args, **kwargs)
exceptExceptionasexc:
_capture_exception(exc)
chat_span.set_status(SPANSTATUS.ERROR)
raise
set_span_data_for_response(chat_span, integration, response)
set_span_data_for_response(span, integration, response)
returnresponse
returnnew_async_generate_content

Fix in CursorFix in Web


Comment on lines 235 to 241
chat_span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
chat_span.set_data(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
chat_span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
chat_span.set_data(SPANDATA.GEN_AI_AGENT_NAME, model_name)
set_span_data_for_request(
chat_span, integration, model_name, contents, kwargs
)
Copy link

Choose a reason for hiding this comment

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

Bug:_wrap_async_generate_content omits setting SPANDATA.GEN_AI_AGENT_NAME on the chat span.
Severity: HIGH | Confidence: 1.00

🔍 Detailed Analysis

The _wrap_async_generate_content function fails to set the SPANDATA.GEN_AI_AGENT_NAME on the chat span. This omission occurs when handling async non-streaming generate_content calls, leading to inconsistent span data compared to other wrapper functions which correctly set this attribute. Consequently, monitoring attributes for these specific calls will be incomplete.

💡 Suggested Fix

Add chat_span.set_data(SPANDATA.GEN_AI_AGENT_NAME, model_name) within the _wrap_async_generate_content function, specifically after setting SPANDATA.GEN_AI_REQUEST_MODEL.

🤖 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/google_genai/__init__.py#L235-L241 Potential issue: The `_wrap_async_generate_content` function fails to set the `SPANDATA.GEN_AI_AGENT_NAME` on the chat span. This omission occurs when handling async non-streaming `generate_content` calls, leading to inconsistent span data compared to other wrapper functions which correctly set this attribute. Consequently, monitoring attributes for these specific calls will be incomplete. 

Did we get this right? 👍 / 👎 to inform future reviews.

@codecov
Copy link

codecovbot commented Oct 29, 2025

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.96%. Comparing base (b3b2eb6) to head (0e23407).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing linesPatch %Lines
sentry_sdk/integrations/google_genai/__init__.py66.66%1 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## master #5038 +/- ## ========================================== + Coverage 83.93% 83.96% +0.03%  ========================================== Files 179 179 Lines 17887 17890 +3 Branches 3181 3181 ========================================== + Hits 15014 15022 +8 + Misses 1908 1902 -6 - Partials 965 966 +1 
Files with missing linesCoverage Δ
sentry_sdk/integrations/google_genai/__init__.py52.94% <66.66%> (+0.24%)⬆️

... and 3 files with indirect coverage changes

@sentrivanasentrivana merged commit 76cc416 into masterOct 29, 2025
123 of 124 checks passed
@sentrivanasentrivana deleted the ivana/genai-agent-name branch October 29, 2025 09:31
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 freeto 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.

4 participants

@sentrivana@alexander-alderman-webb@constantinius