Skip to content

Commit abedda8

Browse files
committed
Merge remote-tracking branch 'origin/master' into potel-base
2 parents c7d10a2 + 57a3405 commit abedda8

File tree

5 files changed

+75
-19
lines changed

5 files changed

+75
-19
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ for your feedback. How was the migration? Is everything working as expected? Is
6363
*nothing* working as expected? Something in between? Please let us know
6464
[on GitHub](https://github.com/getsentry/sentry-python/discussions/3936) or
6565
[on Discord](https://discord.com/invite/Ww9hbqr).
66+
67+
## 2.35.1
68+
69+
### Various fixes & improvements
70+
71+
- OpenAI Agents: Isolate agent run (#4720) by @sentrivana
72+
- Tracing: Do not attach stacktrace to transaction (#4713) by @Zylphrex
73+
6674
## 2.35.0
6775

6876
### Various fixes & improvements

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
from functools import wraps
44

55
from sentry_sdk.integrations import DidNotEnable
6-
76
from ..spans import invoke_agent_span, update_invoke_agent_span, handoff_span
87

98
from typing import TYPE_CHECKING
109

1110
if TYPE_CHECKING:
1211
from typing import Any, Optional
1312

14-
1513
try:
1614
import agents
1715
except ImportError:

sentry_sdk/integrations/openai_agents/patches/runner.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ def _create_run_wrapper(original_func: Callable[..., Any]) -> Callable[..., Any]
2323

2424
@wraps(original_func)
2525
async def wrapper(*args: Any, **kwargs: Any) -> Any:
26-
agent = args[0]
27-
with agent_workflow_span(agent):
28-
result = None
29-
try:
30-
result = await original_func(*args, **kwargs)
31-
return result
32-
except Exception as exc:
33-
_capture_exception(exc)
34-
35-
# It could be that there is a "invoke agent" span still open
36-
span = sentry_sdk.get_current_span()
37-
if span is not None and span.timestamp is None:
38-
span.__exit__(None, None, None)
39-
40-
raise exc from None
26+
# Isolate each workflow so that when agents are run in asyncio tasks they
27+
# don't touch each other's scopes
28+
with sentry_sdk.isolation_scope():
29+
agent = args[0]
30+
with agent_workflow_span(agent):
31+
result = None
32+
try:
33+
result = await original_func(*args, **kwargs)
34+
return result
35+
except Exception as exc:
36+
_capture_exception(exc)
37+
38+
# It could be that there is a "invoke agent" span still open
39+
current_span = sentry_sdk.get_current_span()
40+
if current_span is not None and current_span.timestamp is None:
41+
current_span.__exit__(None, None, None)
42+
43+
raise exc from None
4144

4245
return wrapper

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import re
23
import pytest
34
from unittest.mock import MagicMock, patch
@@ -86,7 +87,7 @@ def test_agent_custom_model() -> Agent:
8687
name="test_agent_custom_model",
8788
instructions="You are a helpful test assistant.",
8889
# the model could be agents.OpenAIChatCompletionsModel()
89-
model=MagicMock(spec=agents.Model, model="my-custom-model"),
90+
model="my-custom-model",
9091
model_settings=ModelSettings(
9192
max_tokens=100,
9293
temperature=0.7,
@@ -650,3 +651,45 @@ async def test_error_handling(sentry_init, capture_events, test_agent):
650651
assert ai_client_span["description"] == "chat gpt-4"
651652
assert ai_client_span["origin"] == "auto.ai.openai_agents"
652653
assert ai_client_span["status"] == "internal_error"
654+
655+
656+
@pytest.mark.asyncio
657+
async def test_multiple_agents_asyncio(
658+
sentry_init, capture_events, test_agent, mock_model_response
659+
):
660+
"""
661+
Test that multiple agents can be run at the same time in asyncio tasks
662+
without interfering with each other.
663+
"""
664+
665+
with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}):
666+
with patch(
667+
"agents.models.openai_responses.OpenAIResponsesModel.get_response"
668+
) as mock_get_response:
669+
mock_get_response.return_value = mock_model_response
670+
671+
sentry_init(
672+
integrations=[OpenAIAgentsIntegration()],
673+
traces_sample_rate=1.0,
674+
)
675+
676+
events = capture_events()
677+
678+
async def run():
679+
await agents.Runner.run(
680+
starting_agent=test_agent,
681+
input="Test input",
682+
run_config=test_run_config,
683+
)
684+
685+
await asyncio.gather(*[run() for _ in range(3)])
686+
687+
assert len(events) == 3
688+
txn1, txn2, txn3 = events
689+
690+
assert txn1["type"] == "transaction"
691+
assert txn1["transaction"] == "test_agent workflow"
692+
assert txn2["type"] == "transaction"
693+
assert txn2["transaction"] == "test_agent workflow"
694+
assert txn3["type"] == "transaction"
695+
assert txn3["transaction"] == "test_agent workflow"

tox.ini

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# The file (and all resulting CI YAMLs) then need to be regenerated via
1111
# "scripts/generate-test-files.sh".
1212
#
13-
# Last generated: 2025-08-25T14:58:55.582712+00:00
13+
# Last generated: 2025-08-26T08:59:42.512502+00:00
1414

1515
[tox]
1616
requires =
@@ -159,6 +159,10 @@ envlist =
159159
{py3.7,py3.10,py3.11}-pymongo-v3.13.0
160160
{py3.7,py3.9,py3.10}-pymongo-v4.0.2
161161
{py3.9,py3.12,py3.13}-pymongo-v4.14.1
162+
{py3.6}-pymongo-v3.5.1
163+
{py3.6,py3.10,py3.11}-pymongo-v3.13.0
164+
{py3.6,py3.9,py3.10}-pymongo-v4.0.2
165+
{py3.9,py3.12,py3.13}-pymongo-v4.14.0
162166

163167
{py3.7}-redis_py_cluster_legacy-v2.0.0
164168
{py3.7,py3.8}-redis_py_cluster_legacy-v2.1.3

0 commit comments

Comments
 (0)