From d9cffce7fecf1ed2348691feb1de3a30fa01eef5 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 24 Jul 2025 12:15:22 -0400 Subject: [PATCH 1/2] Use an iterator instead of a shared global index --- tests/contrib/openai_agents/test_openai.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/contrib/openai_agents/test_openai.py b/tests/contrib/openai_agents/test_openai.py index bb7ed38a3..aa53132ed 100644 --- a/tests/contrib/openai_agents/test_openai.py +++ b/tests/contrib/openai_agents/test_openai.py @@ -72,25 +72,16 @@ from tests.helpers import new_worker from tests.helpers.nexus import create_nexus_endpoint, make_nexus_endpoint_name -response_index: int = 0 - class StaticTestModel(TestModel): __test__ = False responses: list[ModelResponse] = [] - def response(self): - global response_index - response = self.responses[response_index] - response_index += 1 - return response - def __init__( self, ) -> None: - global response_index - response_index = 0 - super().__init__(self.response) + self._responses = iter(self.responses) + super().__init__(lambda: next(self._responses)) class TestHelloModel(StaticTestModel): From 9659c9b06b3c57c827d4c634e7ccfc86814cdd5d Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 24 Jul 2025 12:23:05 -0400 Subject: [PATCH 2/2] Apply the same cleanup to another test --- tests/contrib/openai_agents/test_openai.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/tests/contrib/openai_agents/test_openai.py b/tests/contrib/openai_agents/test_openai.py index aa53132ed..31cb5eda9 100644 --- a/tests/contrib/openai_agents/test_openai.py +++ b/tests/contrib/openai_agents/test_openai.py @@ -1333,9 +1333,6 @@ async def test_customer_service_workflow(client: Client, use_local_model: bool): ) -guardrail_response_index: int = 0 - - class InputGuardrailModel(OpenAIResponsesModel): __test__ = False responses: list[ModelResponse] = [ @@ -1424,11 +1421,9 @@ def __init__( model: str, openai_client: AsyncOpenAI, ) -> None: - global response_index - response_index = 0 - global guardrail_response_index - guardrail_response_index = 0 super().__init__(model, openai_client) + self._responses = iter(self.responses) + self._guardrail_responses = iter(self.guardrail_responses) async def get_response( self, @@ -1446,15 +1441,9 @@ async def get_response( system_instructions == "Check if the user is asking you to do their math homework." ): - global guardrail_response_index - response = self.guardrail_responses[guardrail_response_index] - guardrail_response_index += 1 - return response + return next(self._guardrail_responses) else: - global response_index - response = self.responses[response_index] - response_index += 1 - return response + return next(self._responses) ### 1. An agent-based guardrail that is triggered if the user is asking to do math homework