Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def _patch_graph_nodes() -> None:

@wraps(original_model_request_run)
async def wrapped_model_request_run(self: "Any", ctx: "Any") -> "Any":
# Avoid creating a duplicate span if run() is invoked after stream().
# This fails here: https://github.com/pydantic/pydantic-ai/blob/916fc83e8929470679db5ac1b3065bda5d5f4253/pydantic_ai_slim/pydantic_ai/_agent_graph.py#L1119
did_stream = getattr(self, "_did_stream", False)
# Do not create a duplicate span when a cached result is served.
cached_result = getattr(self, "_result", None)
if did_stream or cached_result is not None:
return await original_model_request_run(self, ctx)

messages, model, model_settings = _extract_span_data(self, ctx)

with ai_client_span(messages, None, model, model_settings) as span:
Expand All @@ -85,6 +93,14 @@ def create_wrapped_stream(
@asynccontextmanager
@wraps(original_stream_method)
async def wrapped_model_request_stream(self: "Any", ctx: "Any") -> "Any":
# Avoid creating a duplicate span if the function is invoked twice.
# This fails here: https://github.com/pydantic/pydantic-ai/blob/916fc83e8929470679db5ac1b3065bda5d5f4253/pydantic_ai_slim/pydantic_ai/_agent_graph.py#L1128
did_stream = getattr(self, "_did_stream", False)
if did_stream:
Comment thread
alexander-alderman-webb marked this conversation as resolved.
async with original_stream_method(self, ctx) as stream:
yield stream
return

messages, model, model_settings = _extract_span_data(self, ctx)

# Create chat span for streaming request
Expand Down
Loading
Loading