Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions src/agents/models/openai_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,15 @@ async def _prepare_websocket_request(

def _merge_websocket_headers(self, extra_headers: Mapping[str, Any]) -> dict[str, str]:
headers: dict[str, str] = {}
for key, value in self._client.default_headers.items():
if _is_openai_omitted_value(value):
continue
headers[key] = str(value)
auth_headers = getattr(self._client, "auth_headers", {})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid inherited bearer auth for Azure clients

When an AsyncAzureOpenAI client is passed here, openai-python 2.36.0 does not override auth_headers; it inherits AsyncOpenAI.auth_headers, which formats the Azure API key as Authorization: Bearer ... instead of the api-key header that Azure's _auth_headers / _prepare_options path uses. This makes the Responses WebSocket handshake send bogus auth for Azure API-key clients, and can also add that bogus bearer alongside a caller's default_headers={"api-key": ...} workaround, so please derive auth through the client's request auth path or special-case Azure instead of reading this property directly.

Useful? React with 👍 / 👎.

for source in (auth_headers, self._client.default_headers):
for key, value in source.items():
header_key = str(key)
for existing_key in list(headers):
if existing_key.lower() == header_key.lower():
del headers[existing_key]
if not _is_openai_omitted_value(value):
headers[header_key] = str(value)

for key, value in extra_headers.items():
if isinstance(value, NotGiven):
Expand Down
39 changes: 39 additions & 0 deletions tests/models/test_openai_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3204,6 +3204,45 @@ async def fake_open(
assert ws.close_calls == 1


@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_websocket_model_prepare_websocket_request_includes_client_auth_headers():
client = AsyncOpenAI(api_key="test-key")
model = OpenAIResponsesWSModel(model="gpt-4", openai_client=client)

_frame, _ws_url, headers = await model._prepare_websocket_request(
{
"model": "gpt-4",
"input": "hi",
"stream": True,
}
)

assert "Authorization" not in client.default_headers
assert headers["Authorization"] == "Bearer test-key"


@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_websocket_model_prepare_websocket_request_preserves_client_header_overrides():
client = AsyncOpenAI(
api_key="test-key",
default_headers={"authorization": "Bearer proxy-key"},
)
model = OpenAIResponsesWSModel(model="gpt-4", openai_client=client)

_frame, _ws_url, headers = await model._prepare_websocket_request(
{
"model": "gpt-4",
"input": "hi",
"stream": True,
}
)

assert headers["authorization"] == "Bearer proxy-key"
assert "Authorization" not in headers


@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_websocket_model_prepare_websocket_request_omit_removes_inherited_header():
Expand Down