|
| 1 | +import pytest |
| 2 | +from openai.types.chat.chat_completion import ChatCompletion, Choice |
| 3 | +from openai.types.chat.chat_completion_message import ChatCompletionMessage |
| 4 | + |
| 5 | +from agents import ModelSettings, ModelTracing, OpenAIChatCompletionsModel, OpenAIResponsesModel |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.allow_call_model_methods |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_extra_headers_passed_to_openai_responses_model(): |
| 11 | + """ |
| 12 | + Ensure extra_headers in ModelSettings is passed to the OpenAIResponsesModel client. |
| 13 | + """ |
| 14 | + called_kwargs = {} |
| 15 | + |
| 16 | + class DummyResponses: |
| 17 | + async def create(self, **kwargs): |
| 18 | + nonlocal called_kwargs |
| 19 | + called_kwargs = kwargs |
| 20 | + class DummyResponse: |
| 21 | + id = "dummy" |
| 22 | + output = [] |
| 23 | + usage = type( |
| 24 | + "Usage", (), {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0} |
| 25 | + )() |
| 26 | + return DummyResponse() |
| 27 | + |
| 28 | + class DummyClient: |
| 29 | + def __init__(self): |
| 30 | + self.responses = DummyResponses() |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + model = OpenAIResponsesModel(model="gpt-4", openai_client=DummyClient()) # type: ignore |
| 35 | + extra_headers = {"X-Test-Header": "test-value"} |
| 36 | + await model.get_response( |
| 37 | + system_instructions=None, |
| 38 | + input="hi", |
| 39 | + model_settings=ModelSettings(extra_headers=extra_headers), |
| 40 | + tools=[], |
| 41 | + output_schema=None, |
| 42 | + handoffs=[], |
| 43 | + tracing=ModelTracing.DISABLED, |
| 44 | + previous_response_id=None, |
| 45 | + ) |
| 46 | + assert "extra_headers" in called_kwargs |
| 47 | + assert called_kwargs["extra_headers"]["X-Test-Header"] == "test-value" |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.allow_call_model_methods |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_extra_headers_passed_to_openai_client(): |
| 54 | + """ |
| 55 | + Ensure extra_headers in ModelSettings is passed to the OpenAI client. |
| 56 | + """ |
| 57 | + called_kwargs = {} |
| 58 | + |
| 59 | + class DummyCompletions: |
| 60 | + async def create(self, **kwargs): |
| 61 | + nonlocal called_kwargs |
| 62 | + called_kwargs = kwargs |
| 63 | + msg = ChatCompletionMessage(role="assistant", content="Hello") |
| 64 | + choice = Choice(index=0, finish_reason="stop", message=msg) |
| 65 | + return ChatCompletion( |
| 66 | + id="resp-id", |
| 67 | + created=0, |
| 68 | + model="fake", |
| 69 | + object="chat.completion", |
| 70 | + choices=[choice], |
| 71 | + usage=None, |
| 72 | + ) |
| 73 | + |
| 74 | + class DummyClient: |
| 75 | + def __init__(self): |
| 76 | + self.chat = type("_Chat", (), {"completions": DummyCompletions()})() |
| 77 | + self.base_url = "https://api.openai.com" |
| 78 | + |
| 79 | + model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=DummyClient()) # type: ignore |
| 80 | + extra_headers = {"X-Test-Header": "test-value"} |
| 81 | + await model.get_response( |
| 82 | + system_instructions=None, |
| 83 | + input="hi", |
| 84 | + model_settings=ModelSettings(extra_headers=extra_headers), |
| 85 | + tools=[], |
| 86 | + output_schema=None, |
| 87 | + handoffs=[], |
| 88 | + tracing=ModelTracing.DISABLED, |
| 89 | + previous_response_id=None, |
| 90 | + ) |
| 91 | + assert "extra_headers" in called_kwargs |
| 92 | + assert called_kwargs["extra_headers"]["X-Test-Header"] == "test-value" |
0 commit comments