Skip to content

Commit a457184

Browse files
authored
Use an iterator instead of a shared global index in openai agent tests (#991)
1 parent d863f5c commit a457184

File tree

1 file changed

+6
-26
lines changed

1 file changed

+6
-26
lines changed

tests/contrib/openai_agents/test_openai.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,16 @@
7272
from tests.helpers import new_worker
7373
from tests.helpers.nexus import create_nexus_endpoint, make_nexus_endpoint_name
7474

75-
response_index: int = 0
76-
7775

7876
class StaticTestModel(TestModel):
7977
__test__ = False
8078
responses: list[ModelResponse] = []
8179

82-
def response(self):
83-
global response_index
84-
response = self.responses[response_index]
85-
response_index += 1
86-
return response
87-
8880
def __init__(
8981
self,
9082
) -> None:
91-
global response_index
92-
response_index = 0
93-
super().__init__(self.response)
83+
self._responses = iter(self.responses)
84+
super().__init__(lambda: next(self._responses))
9485

9586

9687
class TestHelloModel(StaticTestModel):
@@ -1342,9 +1333,6 @@ async def test_customer_service_workflow(client: Client, use_local_model: bool):
13421333
)
13431334

13441335

1345-
guardrail_response_index: int = 0
1346-
1347-
13481336
class InputGuardrailModel(OpenAIResponsesModel):
13491337
__test__ = False
13501338
responses: list[ModelResponse] = [
@@ -1433,11 +1421,9 @@ def __init__(
14331421
model: str,
14341422
openai_client: AsyncOpenAI,
14351423
) -> None:
1436-
global response_index
1437-
response_index = 0
1438-
global guardrail_response_index
1439-
guardrail_response_index = 0
14401424
super().__init__(model, openai_client)
1425+
self._responses = iter(self.responses)
1426+
self._guardrail_responses = iter(self.guardrail_responses)
14411427

14421428
async def get_response(
14431429
self,
@@ -1455,15 +1441,9 @@ async def get_response(
14551441
system_instructions
14561442
== "Check if the user is asking you to do their math homework."
14571443
):
1458-
global guardrail_response_index
1459-
response = self.guardrail_responses[guardrail_response_index]
1460-
guardrail_response_index += 1
1461-
return response
1444+
return next(self._guardrail_responses)
14621445
else:
1463-
global response_index
1464-
response = self.responses[response_index]
1465-
response_index += 1
1466-
return response
1446+
return next(self._responses)
14671447

14681448

14691449
### 1. An agent-based guardrail that is triggered if the user is asking to do math homework

0 commit comments

Comments
 (0)