Skip to content

Commit 7596fb8

Browse files
fix: use bearer authentication for beta Realtime sessions (#4317)
Match the current Realtime client secret endpoint’s authentication policy for legacy session and transcription session creation. Both sync and async clients use the regular API key. An admin key alone no longer satisfies authentication. Verified with 81 focused credential, Realtime, and authentication tests, plus Ruff and ty.
1 parent 9d7f58e commit 7596fb8

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎src/openai/resources/beta/realtime/sessions.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ def create(
196196
session_create_params.SessionCreateParams,
197197
),
198198
options=make_request_options(
199-
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
199+
extra_headers=extra_headers,
200+
extra_query=extra_query,
201+
extra_body=extra_body,
202+
timeout=timeout,
203+
security={"bearer_auth": True},
200204
),
201205
cast_to=SessionCreateResponse,
202206
)
@@ -380,7 +384,11 @@ async def create(
380384
session_create_params.SessionCreateParams,
381385
),
382386
options=make_request_options(
383-
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
387+
extra_headers=extra_headers,
388+
extra_query=extra_query,
389+
extra_body=extra_body,
390+
timeout=timeout,
391+
security={"bearer_auth": True},
384392
),
385393
cast_to=SessionCreateResponse,
386394
)

‎src/openai/resources/beta/realtime/transcription_sessions.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ def create(
125125
transcription_session_create_params.TranscriptionSessionCreateParams,
126126
),
127127
options=make_request_options(
128-
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
128+
extra_headers=extra_headers,
129+
extra_query=extra_query,
130+
extra_body=extra_body,
131+
timeout=timeout,
132+
security={"bearer_auth": True},
129133
),
130134
cast_to=TranscriptionSession,
131135
)
@@ -238,7 +242,11 @@ async def create(
238242
transcription_session_create_params.TranscriptionSessionCreateParams,
239243
),
240244
options=make_request_options(
241-
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
245+
extra_headers=extra_headers,
246+
extra_query=extra_query,
247+
extra_body=extra_body,
248+
timeout=timeout,
249+
security={"bearer_auth": True},
242250
),
243251
cast_to=TranscriptionSession,
244252
)

‎tests/lib/test_request_credentials.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,29 @@ async def async_provider() -> str:
268268
await async_client.models.list()
269269

270270
assert received == [["Bearer fake-hook-value"]]
271+
272+
273+
@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
274+
@pytest.mark.parametrize("resource_name", ["sessions", "transcription_sessions"])
275+
@pytest.mark.parametrize("api_key", ["", "fake-project-key"], ids=["admin-only", "both-keys"])
276+
async def test_beta_realtime_session_credentials(sync: bool, resource_name: str, api_key: str) -> None:
277+
received: list[str] = []
278+
279+
def handler(request: httpx2.Request) -> httpx2.Response:
280+
received.append(request.headers["Authorization"])
281+
return httpx2.Response(200, json={})
282+
283+
transport = httpx2.MockTransport(handler)
284+
with nullcontext() if api_key else pytest.raises(TypeError, match="Could not resolve authentication"):
285+
if sync:
286+
with OpenAI(
287+
api_key=api_key, admin_api_key="fake-admin", http_client=httpx2.Client(transport=transport)
288+
) as client:
289+
getattr(client.beta.realtime, resource_name).create()
290+
else:
291+
async with AsyncOpenAI(
292+
api_key=api_key, admin_api_key="fake-admin", http_client=httpx2.AsyncClient(transport=transport)
293+
) as async_client:
294+
await getattr(async_client.beta.realtime, resource_name).create()
295+
296+
assert received == (["Bearer fake-project-key"] if api_key else [])

0 commit comments

Comments
 (0)