Skip to content

Commit d7e581b

Browse files
feat(python): enable stream_gen_ai_spans in Python test templates (#151)
* feat(python): enable stream_gen_ai_spans in Python test templates Pass the existing streamGenAiSpans flag through to the Python base template as stream_gen_ai_spans in sentry_sdk.init(). This enables span streaming for gen_ai.* spans in Python SDK 2.60.0+, matching the behavior already in place for the JS SDK. The span collector already handles v2 span envelopes, so no changes are needed there — only the template and documentation comments are updated to reflect cross-platform support. Refs TET-2360 * feat: add stream_gen_ai_spans configuration to Sentry SDK initialization in templates
1 parent 72032b5 commit d7e581b

8 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ Options:
4343
--sentry-javascript <path> Use local Sentry JavaScript SDK (link)
4444
--sentry-php <path> Use local Sentry PHP SDK (core sentry/sentry-php)
4545
--sentry-laravel <path> Use local Sentry Laravel SDK (composer path repository)
46-
--stream-gen-ai-spans Enable streamGenAiSpans in JS Sentry.init() (default: on)
47-
--not-stream-gen-ai-spans Disable streamGenAiSpans in JS Sentry.init()
46+
--stream-gen-ai-spans Enable streamGenAiSpans / stream_gen_ai_spans in Sentry.init() (default: on)
47+
--not-stream-gen-ai-spans Disable streamGenAiSpans / stream_gen_ai_spans in Sentry.init()
4848
--help, -h Show this help message
4949
5050
Examples:

src/runner/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export class Runner {
271271
...(testDefinition.agent && { agent: testDefinition.agent }),
272272
...(testDefinition.mcpServer && { mcpServer: testDefinition.mcpServer }),
273273
inputs: processedInputs,
274-
streamGenAiSpans: context.streamGenAiSpans !== false, // JS Sentry.init() flag, default true
274+
streamGenAiSpans: context.streamGenAiSpans !== false, // Sentry.init() flag (JS + Python), default true
275275
};
276276

277277
// PHP/Laravel: add computed class names and command signature for templates

src/runner/templates/base.python.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ sentry_sdk.init(
2323
dsn=os.environ.get("SENTRY_DSN"),
2424
traces_sample_rate=1.0,
2525
send_default_pii=True,
26+
stream_gen_ai_spans={{ "True" if streamGenAiSpans else "False" }},
2627
)
2728
{% endblock %}
2829

src/runner/templates/embeddings/python/litellm/template.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sentry_sdk.init(
1212
dsn=os.environ.get("SENTRY_DSN"),
1313
traces_sample_rate=1.0,
1414
send_default_pii=True,
15+
stream_gen_ai_spans={{ "True" if streamGenAiSpans else "False" }},
1516
integrations=[LiteLLMIntegration(include_prompts=True)],
1617
disabled_integrations=[OpenAIIntegration()],
1718
)

src/runner/templates/llm/python/litellm/template.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ sentry_sdk.init(
8686
dsn=os.environ.get("SENTRY_DSN"),
8787
traces_sample_rate=1.0,
8888
send_default_pii=True,
89+
stream_gen_ai_spans={{ "True" if streamGenAiSpans else "False" }},
8990
integrations=[LiteLLMIntegration(include_prompts=True)],
9091
disabled_integrations=[OpenAIIntegration()],
9192
)

src/runner/templates/mcp/python/mcp/template.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ sentry_sdk.init(
4848
dsn=os.environ.get("SENTRY_DSN"),
4949
traces_sample_rate=1.0,
5050
send_default_pii=True,
51+
stream_gen_ai_spans={{ "True" if streamGenAiSpans else "False" }},
5152
integrations=[MCPIntegration()],
5253
)
5354
{% endblock %}

src/span-collector/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ export class SpanCollector {
152152

153153
// v2 span envelope: application/vnd.sentry.items.span.v2+json
154154
// Body shape: { version: 2, items: [<v2span>, ...] }
155-
// Used by Sentry JS SDK 10.53.0+ when streamGenAiSpans is enabled —
156-
// gen_ai spans are stripped from the transaction and shipped here.
155+
// Used by Sentry JS SDK 10.53.0+ and Python SDK 2.60.0+ when
156+
// streamGenAiSpans / stream_gen_ai_spans is enabled — gen_ai spans
157+
// are stripped from the transaction and shipped here.
157158
// Spec: https://develop.sentry.dev/sdk/telemetry/spans/span-protocol
158159
if (
159160
itemHeader.type === 'span' &&

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,6 @@ export interface RunnerContext {
311311
timeoutMs: number;
312312
// Controls whether to print verbose console output (default: true)
313313
verbose?: boolean;
314-
// JS only: value to pass for `streamGenAiSpans` in Sentry.init() (default: true)
314+
// Value to pass for `streamGenAiSpans` (JS) / `stream_gen_ai_spans` (Python) in Sentry.init() (default: true)
315315
streamGenAiSpans?: boolean;
316316
}

0 commit comments

Comments
 (0)