Skip to content

feat(google-genai): Support span streaming#6313

Open
alexander-alderman-webb wants to merge 7 commits into
masterfrom
webb/google-genai/span-first
Open

feat(google-genai): Support span streaming#6313
alexander-alderman-webb wants to merge 7 commits into
masterfrom
webb/google-genai/span-first

Conversation

@alexander-alderman-webb
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb commented May 21, 2026

Note: Depends on #6314

Description

Use sentry_sdk.traces.start_span, replace Span.set_data() with StreamedSpan.set_attribute() and Span.set_status(SPANSTATUS.INTERNAL_ERROR) with StreamedSpan.status = SpanStatus.ERROR when in span streaming mode.

Parametrize tests on the trace lifecycle option.

Issues

Closes #6024

Reminders

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 21, 2026

Codecov Results 📊

76 passed | Total: 76 | Pass Rate: 100% | Execution Time: 8.66s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +69
Passed Tests 📈 +69
Failed Tests
Skipped Tests

All tests are passing successfully.

❌ Patch coverage is 1.48%. Project has 16563 uncovered lines.
❌ Project coverage is 26.21%. Comparing base (base) to head (head).

Files with missing lines (4)
File Patch % Lines
utils.py 0.00% ⚠️ 529 Missing
tracing_utils.py 50.66% ⚠️ 372 Missing and 66 partials
__init__.py 5.02% ⚠️ 246 Missing
streaming.py 0.00% ⚠️ 66 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    26.35%    26.21%    -0.14%
==========================================
  Files          190       190         —
  Lines        22483     22445       -38
  Branches      7652      7997      +345
==========================================
+ Hits          5924      5882       -42
- Misses       16559     16563        +4
- Partials       561       548       -13

Generated by Codecov Action

@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review May 21, 2026 09:07
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner May 21, 2026 09:07
Copy link
Copy Markdown

@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 2 potential issues.

Fix All in Cursor

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

Reviewed by Cursor Bugbot for commit bdf8ded. Configure here.

Comment thread sentry_sdk/tracing_utils.py
Comment thread sentry_sdk/integrations/google_genai/__init__.py
Comment thread sentry_sdk/integrations/google_genai/utils.py
Comment thread sentry_sdk/integrations/google_genai/__init__.py
Copy link
Copy Markdown
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.

lgtm, some suggestions

Comment on lines +98 to +102
set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
set_on_span(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
set_span_data_for_request(chat_span, integration, model_name, contents, kwargs)
chat_span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
set_on_span(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could put these attrs into the attributes dict of the streaming start_span alongside sentry.op and sentry.origin. That way they'd be accessible in people's traces_samplers (since sampling happens right on start_span).

Basically, as long as the attrs are known on start_span, it's good to set them on the span from the get go.

set_on_span = chat_span.set_data
chat_span.__enter__()

set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same comment here

Comment on lines +229 to +236
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
},
) as chat_span:
chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

...and here

Suggested change
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
},
) as chat_span:
chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
},
) as chat_span:

Comment on lines +293 to +300
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
},
) as chat_span:
chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
},
) as chat_span:
chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name
},
) as chat_span:

Comment on lines +353 to +360
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
},
) as span:
span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings")
span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
},
) as span:
span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings")
span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "embeddings",
SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name
},
) as span:

Comment on lines +413 to +420
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
},
) as span:
span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings")
span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
},
) as span:
span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings")
span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM)
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "embeddings",
SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
},
) as span:

Comment on lines +678 to +687
span = sentry_sdk.traces.start_span(
name=f"execute_tool {tool_name}",
attributes={
"sentry.op": OP.GEN_AI_EXECUTE_TOOL,
"sentry.origin": ORIGIN,
},
)
span.set_attribute(SPANDATA.GEN_AI_TOOL_NAME, tool_name)
if tool_doc:
span.set_attribute(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_doc)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
span = sentry_sdk.traces.start_span(
name=f"execute_tool {tool_name}",
attributes={
"sentry.op": OP.GEN_AI_EXECUTE_TOOL,
"sentry.origin": ORIGIN,
},
)
span.set_attribute(SPANDATA.GEN_AI_TOOL_NAME, tool_name)
if tool_doc:
span.set_attribute(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_doc)
attributes = {
"sentry.op": OP.GEN_AI_EXECUTE_TOOL,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_TOOL_NAME: tool_name,
}
if tool_doc:
attributes[SPANDATA.GEN_AI_TOOL_DESCRIPTION] = tool_doc
span = sentry_sdk.traces.start_span(
name=f"execute_tool {tool_name}",
attributes=attributes,
)

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.

Migrate google_genai to span first

2 participants