Skip to content

Use an iterator instead of a shared global index #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2025
Merged
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
32 changes: 6 additions & 26 deletions tests/contrib/openai_agents/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1342,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] = [
Expand Down Expand Up @@ -1433,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,
Expand All @@ -1455,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
Expand Down