Skip to content

Commit b2b0c34

Browse files
mypy2
1 parent c4a5366 commit b2b0c34

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414

1515
if TYPE_CHECKING:
16-
from typing import Any, Awaitable, Callable, Optional
16+
from typing import Any, Awaitable, Callable, Optional, Union
1717

1818
from agents.run_internal.run_steps import SingleStepResult
1919

@@ -51,7 +51,7 @@ def _maybe_start_agent_span(
5151
should_run_agent_start_hooks: bool,
5252
span_kwargs: "dict[str, Any]",
5353
is_streaming: bool = False,
54-
) -> "Optional[Span]":
54+
) -> "Optional[Union[Span, StreamedSpan]]":
5555
"""
5656
Start an agent invocation span if conditions are met.
5757
Handles ending any existing span for a different agent.
@@ -79,7 +79,12 @@ def _maybe_start_agent_span(
7979
context_wrapper._sentry_agent_span = span
8080
agent._sentry_agent_span = span
8181

82-
if is_streaming:
82+
if not is_streaming:
83+
return span
84+
85+
if isinstance(span, StreamedSpan):
86+
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
87+
else:
8388
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
8489

8590
return span

sentry_sdk/integrations/openai_agents/patches/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ..spans import ai_client_span, update_ai_client_span
1818

1919
if TYPE_CHECKING:
20-
from typing import Any, Callable, Optional
20+
from typing import Any, Callable, Optional, Union
2121

2222
from sentry_sdk.tracing import Span
2323

@@ -42,7 +42,7 @@ def _set_response_model_on_agent_span(
4242

4343

4444
def _inject_trace_propagation_headers(
45-
hosted_tool: "HostedMCPTool", span: "Span"
45+
hosted_tool: "HostedMCPTool", span: "Union[Span, StreamedSpan]"
4646
) -> None:
4747
headers = hosted_tool.tool_config.get("headers")
4848
if headers is None:

sentry_sdk/integrations/openai_agents/patches/runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ def wrapper(*args: "Any", **kwargs: "Any") -> "Any":
142142

143143
# Set conversation ID on workflow span early so it's captured even on errors
144144
if conversation_id:
145-
workflow_span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id)
145+
if isinstance(workflow_span, StreamedSpan):
146+
workflow_span.set_attribute(
147+
SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id
148+
)
149+
else:
150+
workflow_span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id)
146151

147152
# Store span on agent for cleanup
148153
agent._sentry_workflow_span = workflow_span

0 commit comments

Comments
 (0)