Skip to content

Commit fcb808d

Browse files
nimanikooglecaros
andauthored
fix: make token retrieval in _get_auth asynchronous to prevent blocki… (#23)
* fix: make token retrieval in _get_auth asynchronous to prevent blocking the event loop * Updating to use the proper async type. --------- Co-authored-by: Gerardo Lecaros <10088504+glecaros@users.noreply.github.com>
1 parent 6779885 commit fcb808d

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

python/rtclient/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from typing import Literal, Optional
99

1010
from aiohttp import ClientSession, WSMsgType
11-
from azure.core.credentials import AzureKeyCredential, TokenCredential
11+
from azure.core.credentials import AzureKeyCredential
12+
from azure.core.credentials_async import AsyncTokenCredential
1213

1314
from rtclient.models import (
1415
AssistantContentPart,
@@ -110,7 +111,7 @@ class RTLowLevelClient:
110111
def __init__(
111112
self,
112113
url: Optional[str] = None,
113-
token_credential: Optional[TokenCredential] = None,
114+
token_credential: Optional[AsyncTokenCredential] = None,
114115
key_credential: Optional[AzureKeyCredential] = None,
115116
model: Optional[str] = None,
116117
azure_deployment: Optional[str] = None,
@@ -138,10 +139,10 @@ def __init__(
138139
def _user_agent(self):
139140
return "ms-rtclient-0.4.3"
140141

141-
def _get_auth(self):
142+
async def _get_auth(self):
142143
if self._token_credential:
143-
scopes = ["https://cognitiveservices.azure.com/.default"]
144-
token = self._token_credential.get_token(scopes)
144+
scope = "https://cognitiveservices.azure.com/.default"
145+
token = await self._token_credential.get_token(scope)
145146
return {"Authorization": f"Bearer {token.token}"}
146147
elif self._key_credential:
147148
return {"api-key": self._key_credential.key}
@@ -151,9 +152,9 @@ def _get_auth(self):
151152
async def connect(self):
152153
self.request_id = uuid.uuid4()
153154
if self._is_azure_openai:
154-
auth_headers = self._get_auth()
155-
headers = {"x-ms-client-request-id": str(self.request_id), "User-Agent": self._user_agent(), **auth_headers}
156-
self.ws = await self._session.ws_connect(
155+
auth_headers = await self._get_auth()
156+
headers = {"x-ms-client-request-id": str(self.request_id), "User-Agent": self._user_agent(), **auth_headers}
157+
self.ws = await self._session.ws_connect(
157158
"/openai/realtime",
158159
headers=headers,
159160
params={"deployment": self._azure_deployment, "api-version": "2024-10-01-preview"},
@@ -376,7 +377,7 @@ class RTClient:
376377
def __init__(
377378
self,
378379
url: Optional[str] = None,
379-
token_credential: Optional[TokenCredential] = None,
380+
token_credential: Optional[AsyncTokenCredential] = None,
380381
key_credential: Optional[AzureKeyCredential] = None,
381382
model: Optional[str] = None,
382383
azure_deployment: Optional[str] = None,

0 commit comments

Comments
 (0)