Skip to content

Commit 45df059

Browse files
committed
fix: correct the propagation of the "is_streaming" flag
1 parent df06590 commit 45df059

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

sentry_sdk/integrations/pydantic_ai/patches/agent_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async def __aenter__(self):
4848
self._span.__enter__()
4949

5050
# Push span and agent to contextvar stack
51-
push_invoke_agent_span(self._span, self.agent)
51+
push_invoke_agent_span(self._span, self.agent, self.is_streaming)
5252

5353
# Enter the original context manager
5454
result = await self.original_ctx_manager.__aenter__()
@@ -105,7 +105,7 @@ async def wrapper(self, *args, **kwargs):
105105
# Create invoke_agent span
106106
with invoke_agent_span(user_prompt, self, model, model_settings) as span:
107107
# Push span and agent to contextvar stack
108-
push_invoke_agent_span(span, self)
108+
push_invoke_agent_span(span, self, is_streaming)
109109
try:
110110
result = await original_func(self, *args, **kwargs)
111111

sentry_sdk/integrations/pydantic_ai/spans/ai_client.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
_set_model_data,
1111
_should_send_prompts,
1212
_get_model_name,
13+
get_current_agent,
14+
get_is_streaming,
1315
)
1416

1517
from typing import TYPE_CHECKING
@@ -216,20 +218,11 @@ def ai_client_span(messages, agent, model, model_settings):
216218
_set_agent_data(span, agent)
217219
_set_model_data(span, model, model_settings)
218220

219-
# Set streaming flag
220-
agent_data = sentry_sdk.get_current_scope()._contexts.get("pydantic_ai_agent") or {}
221-
is_streaming = agent_data.get("_streaming", False)
222-
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, is_streaming)
221+
# Set streaming flag from contextvar
222+
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, get_is_streaming())
223223

224224
# Add available tools if agent is available
225-
agent_obj = agent
226-
if not agent_obj:
227-
# Try to get from Sentry scope
228-
agent_data = (
229-
sentry_sdk.get_current_scope()._contexts.get("pydantic_ai_agent") or {}
230-
)
231-
agent_obj = agent_data.get("_agent")
232-
225+
agent_obj = agent or get_current_agent()
233226
_set_available_tools(span, agent_obj)
234227

235228
# Set input messages (full conversation history)

sentry_sdk/integrations/pydantic_ai/utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
# type: ContextVar[list[dict[str, Any]]]
1818

1919

20-
def push_invoke_agent_span(span, agent):
21-
# type: (sentry_sdk.tracing.Span, Any) -> None
22-
"""Push an invoke_agent span onto the stack along with its agent."""
20+
def push_invoke_agent_span(span, agent, is_streaming=False):
21+
# type: (sentry_sdk.tracing.Span, Any, bool) -> None
22+
"""Push an invoke_agent span onto the stack along with its agent and streaming flag."""
2323
stack = _invoke_agent_span_stack.get().copy()
24-
stack.append({"span": span, "agent": agent})
24+
stack.append({"span": span, "agent": agent, "is_streaming": is_streaming})
2525
_invoke_agent_span_stack.set(stack)
2626

2727

@@ -52,6 +52,15 @@ def get_current_agent():
5252
return None
5353

5454

55+
def get_is_streaming():
56+
# type: () -> bool
57+
"""Get the streaming flag from the contextvar stack."""
58+
stack = _invoke_agent_span_stack.get()
59+
if stack:
60+
return stack[-1].get("is_streaming", False)
61+
return False
62+
63+
5564
def _should_send_prompts():
5665
# type: () -> bool
5766
"""

0 commit comments

Comments
 (0)