Skip to content

Commit a4ccbd2

Browse files
Copilotpamelafox
andcommitted
Migrate from AsyncAzureOpenAI to AsyncOpenAI client
Co-authored-by: pamelafox <[email protected]>
1 parent 135498c commit a4ccbd2

File tree

8 files changed

+54
-48
lines changed

8 files changed

+54
-48
lines changed

.env.sample

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
AZURE_OPENAI_API_VERSION=2024-02-15-preview
21
AZURE_OPENAI_ENDPOINT=https://YOUR-ENDPOINT-HERE.openai.azure.com/
32
# Name of the Azure OpenAI GPT deployment (different from the model name)
43
AZURE_OPENAI_CHAT_DEPLOYMENT=gpt4o-mini

=1.108.1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Defaulting to user installation because normal site-packages is not writeable
2+
Requirement already satisfied: openai in /home/runner/.local/lib/python3.12/site-packages (1.107.2)
3+
Requirement already satisfied: anyio<5,>=3.5.0 in /home/runner/.local/lib/python3.12/site-packages (from openai) (4.10.0)
4+
Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.9.0)
5+
Requirement already satisfied: httpx<1,>=0.23.0 in /home/runner/.local/lib/python3.12/site-packages (from openai) (0.28.1)
6+
Requirement already satisfied: jiter<1,>=0.4.0 in /home/runner/.local/lib/python3.12/site-packages (from openai) (0.10.0)
7+
Requirement already satisfied: pydantic<3,>=1.9.0 in /home/runner/.local/lib/python3.12/site-packages (from openai) (2.11.9)
8+
Requirement already satisfied: sniffio in /home/runner/.local/lib/python3.12/site-packages (from openai) (1.3.1)
9+
Requirement already satisfied: tqdm>4 in /home/runner/.local/lib/python3.12/site-packages (from openai) (4.67.1)
10+
Requirement already satisfied: typing-extensions<5,>=4.11 in /home/runner/.local/lib/python3.12/site-packages (from openai) (4.15.0)
11+
Requirement already satisfied: idna>=2.8 in /home/runner/.local/lib/python3.12/site-packages (from anyio<5,>=3.5.0->openai) (3.10)
12+
Requirement already satisfied: certifi in /home/runner/.local/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (2025.8.3)
13+
Requirement already satisfied: httpcore==1.* in /home/runner/.local/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (1.0.9)
14+
Requirement already satisfied: h11>=0.16 in /home/runner/.local/lib/python3.12/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0)
15+
Requirement already satisfied: annotated-types>=0.6.0 in /home/runner/.local/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)
16+
Requirement already satisfied: pydantic-core==2.33.2 in /home/runner/.local/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (2.33.2)
17+
Requirement already satisfied: typing-inspection>=0.4.0 in /home/runner/.local/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (0.4.1)

infra/aca.bicep

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ param serviceName string = 'aca'
99
param exists bool
1010
param openAiDeploymentName string
1111
param openAiEndpoint string
12-
param openAiApiVersion string
1312

1413
resource acaIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
1514
name: identityName
@@ -25,10 +24,6 @@ var env = [
2524
name: 'AZURE_OPENAI_ENDPOINT'
2625
value: openAiEndpoint
2726
}
28-
{
29-
name: 'AZURE_OPENAI_API_VERSION'
30-
value: openAiApiVersion
31-
}
3227
{
3328
name: 'RUNNING_IN_PRODUCTION'
3429
value: 'true'

infra/main.bicep

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ param openAiResourceGroupName string = ''
5555
})
5656
param openAiResourceLocation string
5757
param openAiSkuName string = ''
58-
param openAiApiVersion string = '' // Used by the SDK in the app code
5958
param disableKeyBasedAuth bool = true
6059

6160
// Parameters for the specific Azure OpenAI deployment:
@@ -152,7 +151,6 @@ module aca 'aca.bicep' = {
152151
containerRegistryName: containerApps.outputs.registryName
153152
openAiDeploymentName: openAiDeploymentName
154153
openAiEndpoint: createAzureOpenAi ? openAi.outputs.endpoint : openAiEndpoint
155-
openAiApiVersion: openAiApiVersion
156154
exists: acaExists
157155
}
158156
}
@@ -185,7 +183,6 @@ output AZURE_TENANT_ID string = tenant().tenantId
185183
output AZURE_OPENAI_RESOURCE_GROUP string = openAiResourceGroup.name
186184
output AZURE_OPENAI_RESOURCE_NAME string = openAi.outputs.name
187185
output AZURE_OPENAI_CHAT_DEPLOYMENT string = openAiDeploymentName
188-
output AZURE_OPENAI_API_VERSION string = openAiApiVersion
189186
output AZURE_OPENAI_ENDPOINT string = createAzureOpenAi ? openAi.outputs.endpoint : openAiEndpoint
190187

191188
output SERVICE_ACA_IDENTITY_PRINCIPAL_ID string = aca.outputs.SERVICE_ACA_IDENTITY_PRINCIPAL_ID

src/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"httptools",
1414
# Used by uvicorn for reload functionality
1515
"watchfiles",
16-
"openai",
16+
"openai>=1.108.1",
1717
"azure-identity",
1818
"aiohttp>=3.11.0",
1919
"python-dotenv",

src/quartapp/chat.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ManagedIdentityCredential,
88
get_bearer_token_provider,
99
)
10-
from openai import AsyncAzureOpenAI
10+
from openai import AsyncOpenAI
1111
from quart import (
1212
Blueprint,
1313
Response,
@@ -47,11 +47,10 @@ async def configure_openai():
4747
if not os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT"):
4848
raise ValueError("AZURE_OPENAI_CHAT_DEPLOYMENT is required for Azure OpenAI")
4949

50-
# Create the Asynchronous Azure OpenAI client
51-
bp.openai_client = AsyncAzureOpenAI(
52-
api_version=os.getenv("AZURE_OPENAI_API_VERSION") or "2024-02-15-preview",
53-
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
54-
azure_ad_token_provider=token_provider,
50+
# Create the Asynchronous OpenAI client
51+
bp.openai_client = AsyncOpenAI(
52+
base_url=os.getenv("AZURE_OPENAI_ENDPOINT"),
53+
api_key=token_provider,
5554
)
5655
# Set the model name to the Azure OpenAI model deployment name
5756
bp.openai_model = os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT")

src/requirements.txt

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#
2-
# This file is autogenerated by pip-compile with Python 3.12
3-
# by the following command:
4-
#
5-
# pip-compile pyproject.toml
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.12
3+
# by the following command:
4+
#
5+
# pip-compile --output-file=src/requirements.txt src/pyproject.toml
66
#
77
aiofiles==24.1.0
88
# via quart
99
aiohappyeyeballs==2.6.1
1010
# via aiohttp
11-
aiohttp==3.12.15
12-
# via quartapp (pyproject.toml)
11+
aiohttp==3.12.15
12+
# via quartapp (src/pyproject.toml)
1313
aiosignal==1.4.0
1414
# via aiohttp
1515
annotated-types==0.7.0
@@ -24,7 +24,7 @@ attrs==25.3.0
2424
azure-core==1.35.1
2525
# via azure-identity
2626
azure-identity==1.25.0
27-
# via quartapp (pyproject.toml)
27+
# via quartapp (src/pyproject.toml)
2828
blinker==1.9.0
2929
# via
3030
# flask
@@ -57,26 +57,26 @@ frozenlist==1.7.0
5757
# aiohttp
5858
# aiosignal
5959
gunicorn==23.0.0
60-
# via quartapp (pyproject.toml)
60+
# via quartapp (src/pyproject.toml)
6161
h11==0.16.0
6262
# via
6363
# httpcore
6464
# hypercorn
6565
# uvicorn
6666
# wsproto
67-
h2==4.3.0
68-
# via hypercorn
69-
hpack==4.1.0
67+
h2==4.3.0
68+
# via hypercorn
69+
hpack==4.1.0
7070
# via h2
7171
httpcore==1.0.9
7272
# via httpx
7373
httptools==0.6.4
74-
# via quartapp (pyproject.toml)
75-
httpx==0.28.1
74+
# via quartapp (src/pyproject.toml)
75+
httpx==0.28.1
7676
# via openai
7777
hypercorn==0.17.3
7878
# via quart
79-
hyperframe==6.1.0
79+
hyperframe==6.1.0
8080
# via h2
8181
idna==3.10
8282
# via
@@ -110,13 +110,13 @@ multidict==6.6.4
110110
# via
111111
# aiohttp
112112
# yarl
113-
openai==1.107.2
114-
# via quartapp (pyproject.toml)
113+
openai==1.109.0
114+
# via quartapp (src/pyproject.toml)
115115
packaging==25.0
116116
# via gunicorn
117117
priority==2.0.0
118118
# via hypercorn
119-
propcache==0.3.2
119+
propcache==0.3.2
120120
# via
121121
# aiohttp
122122
# yarl
@@ -126,16 +126,16 @@ pydantic==2.11.9
126126
# via openai
127127
pydantic-core==2.33.2
128128
# via pydantic
129-
pyjwt[crypto]==2.10.1
130-
# via
131-
# msal
132-
# pyjwt
133-
python-dotenv==1.1.1
134-
# via quartapp (pyproject.toml)
129+
pyjwt[crypto]==2.10.1
130+
# via
131+
# msal
132+
# pyjwt
133+
python-dotenv==1.1.1
134+
# via quartapp (src/pyproject.toml)
135135
pyyaml==6.0.2
136-
# via quartapp (pyproject.toml)
136+
# via quartapp (src/pyproject.toml)
137137
quart==0.20.0
138-
# via quartapp (pyproject.toml)
138+
# via quartapp (src/pyproject.toml)
139139
requests==2.32.5
140140
# via
141141
# azure-core
@@ -163,16 +163,16 @@ typing-inspection==0.4.1
163163
urllib3==2.5.0
164164
# via requests
165165
uvicorn==0.36.0
166-
# via quartapp (pyproject.toml)
166+
# via quartapp (src/pyproject.toml)
167167
uvloop==0.21.0 ; sys_platform != "win32" and (sys_platform != "cygwin" and platform_python_implementation != "PyPy")
168-
# via quartapp (pyproject.toml)
168+
# via quartapp (src/pyproject.toml)
169169
watchfiles==1.1.0
170-
# via quartapp (pyproject.toml)
170+
# via quartapp (src/pyproject.toml)
171171
werkzeug==3.1.3
172172
# via
173173
# flask
174174
# quart
175-
# quartapp (pyproject.toml)
175+
# quartapp (src/pyproject.toml)
176176
wsproto==1.2.0
177177
# via hypercorn
178178
yarl==1.20.1

tests/test_app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ async def test_openai_managedidentity(monkeypatch):
4848
monkeypatch.setenv("AZURE_OPENAI_CLIENT_ID", "test-client-id")
4949
monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "test-openai-service.openai.azure.com")
5050
monkeypatch.setenv("AZURE_OPENAI_CHAT_DEPLOYMENT", "test-chatgpt")
51-
monkeypatch.setenv("AZURE_OPENAI_VERSION", "2023-10-01-preview")
5251

5352
monkeypatch.setattr("azure.identity.aio.ManagedIdentityCredential", mock_cred.MockAzureCredential)
5453

5554
quart_app = quartapp.create_app(testing=True)
5655

5756
async with quart_app.test_app():
58-
assert quart_app.blueprints["chat"].openai_client._azure_ad_token_provider is not None
57+
assert quart_app.blueprints["chat"].openai_client.api_key is not None

0 commit comments

Comments
 (0)