Skip to content

Commit c7d0759

Browse files
[azopenai] Prep for release on Monday (#25576)
- Removing example boilerplate that checks for environment variables existence. These examples aren't executable, so it wasn't doing anything except being distracting for people reading the example - Change over more eligible examples to use the openai/v1 endpoint - Addressing valid lint warnings.
1 parent 7fae087 commit c7d0759

20 files changed

+298
-301
lines changed

sdk/ai/azopenai/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Release History
22

3-
## 0.9.0 (2025-11-07)
3+
## 0.9.0 (2025-11-10)
44

55
### Features Added
66

77
- Updating to /v3 of the OpenAI SDK (github.com/openai/openai-go/v3).
88

99
### Other Changes
1010

11-
- Added examples demonstrating support for Managed Identity
11+
- Added examples demonstrating support for Managed Identity.
1212
- Added examples demonstrating support for deepseek-r1 reasoning.
13+
- Migrated examples to using the openai/v1 endpoint.
1314

1415
## 0.8.0 (2025-06-03)
1516

sdk/ai/azopenai/client_audio_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ func TestClient_GetAudioSpeech(t *testing.T) {
107107
// when it sends the request.
108108
tempFile, err = os.CreateTemp("", "audio*.flac")
109109
require.NoError(t, err)
110-
defer tempFile.Close()
110+
111+
t.Cleanup(func() {
112+
err := tempFile.Close()
113+
require.NoError(t, err)
114+
})
111115

112116
_, err = tempFile.Write(audioBytes)
113117
require.NoError(t, err)

sdk/ai/azopenai/client_chat_completions_extensions_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ func TestChatExtensionsStreaming_extensions_bringYourOwnData(t *testing.T) {
6969
&azureOpenAI.Cognitive,
7070
))
7171

72-
defer streamer.Close()
72+
t.Cleanup(func() {
73+
err := streamer.Close()
74+
require.NoError(t, err)
75+
})
7376

7477
text := ""
7578

sdk/ai/azopenai/client_chat_completions_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var expectedContent = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10."
3939
var expectedRole = constant.ValueOf[constant.Assistant]()
4040

4141
func TestClient_GetChatCompletions(t *testing.T) {
42-
testFn := func(t *testing.T, client *openai.ChatCompletionService, deployment string, returnedModel string, checkRAI bool) {
42+
testFn := func(t *testing.T, client *openai.ChatCompletionService, deployment string, checkRAI bool) {
4343
resp, err := client.New(context.Background(), newStainlessTestChatCompletionOptions(deployment))
4444
skipNowIfThrottled(t, err)
4545
require.NoError(t, err)
@@ -76,12 +76,12 @@ func TestClient_GetChatCompletions(t *testing.T) {
7676
t.Run("AzureOpenAI", func(t *testing.T) {
7777
client := newStainlessTestClientWithAzureURL(t, azureOpenAI.ChatCompletionsRAI.Endpoint)
7878

79-
testFn(t, &client.Chat.Completions, azureOpenAI.ChatCompletionsRAI.Model, "gpt-4", true)
79+
testFn(t, &client.Chat.Completions, azureOpenAI.ChatCompletionsRAI.Model, true)
8080
})
8181

8282
t.Run("AzureOpenAI.DefaultAzureCredential", func(t *testing.T) {
8383
client := newStainlessTestClientWithAzureURL(t, azureOpenAI.ChatCompletionsRAI.Endpoint)
84-
testFn(t, &client.Chat.Completions, azureOpenAI.ChatCompletions.Model, "gpt-4", true)
84+
testFn(t, &client.Chat.Completions, azureOpenAI.ChatCompletions.Model, true)
8585
})
8686
}
8787

sdk/ai/azopenai/client_shared_test.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,8 @@ func getEnvVariable(varName string, playbackValue string) string {
7474
return val
7575
}
7676

77-
func getEndpoint(ev string, azure bool) string {
78-
fakeEP := fakeAzureEndpoint
79-
80-
if !azure {
81-
fakeEP = fakeOpenAIEndpoint
82-
}
83-
84-
v := getEnvVariable(ev, fakeEP)
77+
func getEndpoint(ev string) string {
78+
v := getEnvVariable(ev, fakeAzureEndpoint)
8579

8680
if !strings.HasSuffix(v, "/") {
8781
// (this just makes recording replacement easier)
@@ -116,22 +110,22 @@ var azureOpenAI = func() testVars {
116110
OpenAI endpoint
117111
}{
118112
USEast: endpoint{
119-
URL: getEndpoint("AOAI_ENDPOINT_USEAST", true),
113+
URL: getEndpoint("AOAI_ENDPOINT_USEAST"),
120114
APIKey: getEnvVariable("AOAI_ENDPOINT_USEAST_API_KEY", fakeAPIKey),
121115
Azure: true,
122116
},
123117
USEast2: endpoint{
124-
URL: getEndpoint("AOAI_ENDPOINT_USEAST2", true),
118+
URL: getEndpoint("AOAI_ENDPOINT_USEAST2"),
125119
APIKey: getEnvVariable("AOAI_ENDPOINT_USEAST2_API_KEY", fakeAPIKey),
126120
Azure: true,
127121
},
128122
USNorthCentral: endpoint{
129-
URL: getEndpoint("AOAI_ENDPOINT_USNORTHCENTRAL", true),
123+
URL: getEndpoint("AOAI_ENDPOINT_USNORTHCENTRAL"),
130124
APIKey: getEnvVariable("AOAI_ENDPOINT_USNORTHCENTRAL_API_KEY", fakeAPIKey),
131125
Azure: true,
132126
},
133127
SWECentral: endpoint{
134-
URL: getEndpoint("AOAI_ENDPOINT_SWECENTRAL", true),
128+
URL: getEndpoint("AOAI_ENDPOINT_SWECENTRAL"),
135129
APIKey: getEnvVariable("AOAI_ENDPOINT_SWECENTRAL_API_KEY", fakeAPIKey),
136130
Azure: true,
137131
},
@@ -223,9 +217,9 @@ var azureOpenAI = func() testVars {
223217
}
224218

225219
for area, epm := range remaps {
226-
os.Setenv("AOAI_"+area+"_ENDPOINT", epm.Endpoint.URL)
227-
os.Setenv("AOAI_"+area+"_API_KEY", epm.Endpoint.APIKey)
228-
os.Setenv("AOAI_"+area+"_MODEL", epm.Model)
220+
_ = os.Setenv("AOAI_"+area+"_ENDPOINT", epm.Endpoint.URL)
221+
_ = os.Setenv("AOAI_"+area+"_API_KEY", epm.Endpoint.APIKey)
222+
_ = os.Setenv("AOAI_"+area+"_MODEL", epm.Model)
229223
}
230224
}
231225

@@ -275,7 +269,6 @@ func newStainlessTestClientWithV1URL(t *testing.T, ep endpoint) openai.Client {
275269
}
276270

277271
const fakeAzureEndpoint = "https://Sanitized.openai.azure.com/"
278-
const fakeOpenAIEndpoint = "https://Sanitized.openai.com/v1"
279272
const fakeAPIKey = "redacted"
280273
const fakeCognitiveEndpoint = "https://Sanitized.openai.azure.com"
281274
const fakeCognitiveIndexName = "index"

sdk/ai/azopenai/custom_client_image_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ func TestImageGeneration_AzureOpenAI(t *testing.T) {
4242
headResp, err := http.DefaultClient.Head(resp.Data[0].URL)
4343
require.NoError(t, err)
4444

45-
headResp.Body.Close()
45+
err = headResp.Body.Close()
46+
require.NoError(t, err)
47+
4648
require.Equal(t, http.StatusOK, headResp.StatusCode)
4749
require.NotEmpty(t, resp.Data[0].RevisedPrompt)
4850
}

sdk/ai/azopenai/example_audio_test.go

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"io"
1010
"os"
1111

12+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1213
"github.com/openai/openai-go/v3"
14+
"github.com/openai/openai-go/v3/azure"
1315
)
1416

1517
// Example_audioTranscription demonstrates how to transcribe speech to text using Azure OpenAI's Whisper model.
@@ -22,30 +24,38 @@ import (
2224
// The example uses environment variables for configuration:
2325
// - AOAI_WHISPER_ENDPOINT: Your Azure OpenAI endpoint URL
2426
// - AOAI_WHISPER_MODEL: The deployment name of your Whisper model
27+
// - AZURE_OPENAI_API_VERSION: Azure OpenAI service API version to use. See https://learn.microsoft.com/azure/ai-foundry/openai/api-version-lifecycle?tabs=go for information about API versions.
2528
//
2629
// Audio transcription is useful for accessibility features, creating searchable archives of audio content,
2730
// generating captions or subtitles, and enabling voice commands in applications.
2831
func Example_audioTranscription() {
29-
if !CheckRequiredEnvVars("AOAI_WHISPER_ENDPOINT", "AOAI_WHISPER_MODEL") {
30-
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
31-
return
32-
}
33-
3432
endpoint := os.Getenv("AOAI_WHISPER_ENDPOINT")
3533
model := os.Getenv("AOAI_WHISPER_MODEL")
34+
apiVersion := os.Getenv("AZURE_OPENAI_API_VERSION")
35+
36+
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
3637

37-
client, err := CreateOpenAIClientWithToken(endpoint, "")
3838
if err != nil {
3939
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
4040
return
4141
}
4242

43+
client := openai.NewClient(
44+
azure.WithEndpoint(endpoint, apiVersion),
45+
azure.WithTokenCredential(tokenCredential),
46+
)
47+
4348
audio_file, err := os.Open("testdata/sampledata_audiofiles_myVoiceIsMyPassportVerifyMe01.mp3")
4449
if err != nil {
4550
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
4651
return
4752
}
48-
defer audio_file.Close()
53+
54+
defer func() {
55+
if err := audio_file.Close(); err != nil {
56+
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
57+
}
58+
}()
4959

5060
resp, err := client.Audio.Transcriptions.New(context.TODO(), openai.AudioTranscriptionNewParams{
5161
Model: openai.AudioModel(model),
@@ -71,24 +81,26 @@ func Example_audioTranscription() {
7181
// The example uses environment variables for configuration:
7282
// - AOAI_TTS_ENDPOINT: Your Azure OpenAI endpoint URL
7383
// - AOAI_TTS_MODEL: The deployment name of your text-to-speech model
84+
// - AZURE_OPENAI_API_VERSION: Azure OpenAI service API version to use. See https://learn.microsoft.com/azure/ai-foundry/openai/api-version-lifecycle?tabs=go for information about API versions.
7485
//
7586
// Text-to-speech conversion is valuable for creating audiobooks, virtual assistants,
7687
// accessibility tools, and adding voice interfaces to applications.
7788
func Example_generateSpeechFromText() {
78-
if !CheckRequiredEnvVars("AOAI_TTS_ENDPOINT", "AOAI_TTS_MODEL") {
79-
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
80-
return
81-
}
82-
8389
endpoint := os.Getenv("AOAI_TTS_ENDPOINT")
8490
model := os.Getenv("AOAI_TTS_MODEL")
91+
apiVersion := os.Getenv("AZURE_OPENAI_API_VERSION")
8592

86-
client, err := CreateOpenAIClientWithToken(endpoint, "")
93+
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
8794
if err != nil {
8895
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
8996
return
9097
}
9198

99+
client := openai.NewClient(
100+
azure.WithEndpoint(endpoint, apiVersion),
101+
azure.WithTokenCredential(tokenCredential),
102+
)
103+
92104
audioResp, err := client.Audio.Speech.New(context.Background(), openai.AudioSpeechNewParams{
93105
Model: openai.SpeechModel(model),
94106
Input: "i am a computer",
@@ -101,7 +113,11 @@ func Example_generateSpeechFromText() {
101113
return
102114
}
103115

104-
defer audioResp.Body.Close()
116+
defer func() {
117+
if err := audioResp.Body.Close(); err != nil {
118+
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
119+
}
120+
}()
105121

106122
audioBytes, err := io.ReadAll(audioResp.Body)
107123

@@ -124,30 +140,37 @@ func Example_generateSpeechFromText() {
124140
// The example uses environment variables for configuration:
125141
// - AOAI_WHISPER_ENDPOINT: Your Azure OpenAI endpoint URL
126142
// - AOAI_WHISPER_MODEL: The deployment name of your Whisper model
143+
// - AZURE_OPENAI_API_VERSION: Azure OpenAI service API version to use. See https://learn.microsoft.com/azure/ai-foundry/openai/api-version-lifecycle?tabs=go for information about API versions.
127144
//
128145
// Speech translation is essential for cross-language communication, creating multilingual content,
129146
// and building applications that break down language barriers.
130147
func Example_audioTranslation() {
131-
if !CheckRequiredEnvVars("AOAI_WHISPER_ENDPOINT", "AOAI_WHISPER_MODEL") {
132-
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
133-
return
134-
}
135-
136148
endpoint := os.Getenv("AOAI_WHISPER_ENDPOINT")
137149
model := os.Getenv("AOAI_WHISPER_MODEL")
150+
apiVersion := os.Getenv("AZURE_OPENAI_API_VERSION")
138151

139-
client, err := CreateOpenAIClientWithToken(endpoint, "")
152+
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
140153
if err != nil {
141154
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
142155
return
143156
}
144157

158+
client := openai.NewClient(
159+
azure.WithEndpoint(endpoint, apiVersion),
160+
azure.WithTokenCredential(tokenCredential),
161+
)
162+
145163
audio_file, err := os.Open("testdata/sampleaudio_hindi_myVoiceIsMyPassportVerifyMe.mp3")
146164
if err != nil {
147165
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
148166
return
149167
}
150-
defer audio_file.Close()
168+
169+
defer func() {
170+
if err := audio_file.Close(); err != nil {
171+
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
172+
}
173+
}()
151174

152175
resp, err := client.Audio.Translations.New(context.TODO(), openai.AudioTranslationNewParams{
153176
Model: openai.AudioModel(model),

sdk/ai/azopenai/example_azure_auth_methods_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ import (
3131
// - Managed Identity
3232
// - Azure CLI credentials
3333
func Example_usingDefaultAzureCredential() {
34-
if !CheckRequiredEnvVars("AOAI_ENDPOINT", "AOAI_MODEL") {
35-
fmt.Fprintf(os.Stderr, "Environment variables are not set, not running example.")
36-
return
37-
}
38-
3934
endpoint := os.Getenv("AOAI_ENDPOINT")
4035
model := os.Getenv("AOAI_MODEL")
4136
tenantID := os.Getenv("AZURE_TENANT_ID")
@@ -80,11 +75,6 @@ func Example_usingManagedIdentityCredential() {
8075
endpoint := os.Getenv("AOAI_ENDPOINT")
8176
model := os.Getenv("AOAI_MODEL")
8277

83-
if endpoint == "" || model == "" {
84-
fmt.Fprintf(os.Stderr, "Environment variables are not set, not running example.")
85-
return
86-
}
87-
8878
var credential *azidentity.ManagedIdentityCredential
8979
var err error
9080

sdk/ai/azopenai/example_azure_content_filtering_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"os"
1010

1111
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
12+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1213
"github.com/openai/openai-go/v3"
14+
"github.com/openai/openai-go/v3/azure"
1315
)
1416

1517
// Example_usingAzureContentFiltering demonstrates how to use Azure OpenAI's content filtering capabilities.
@@ -23,27 +25,29 @@ import (
2325
// The example uses environment variables for configuration:
2426
// - AOAI_ENDPOINT: Your Azure OpenAI endpoint URL
2527
// - AOAI_MODEL: The deployment name of your model
28+
// - AZURE_OPENAI_API_VERSION: Azure OpenAI service API version to use. See https://learn.microsoft.com/azure/ai-foundry/openai/api-version-lifecycle?tabs=go for information about API versions.
2629
//
2730
// Content filtering is essential for:
2831
// - Maintaining content safety and compliance
2932
// - Monitoring content severity levels
3033
// - Implementing content moderation policies
3134
// - Handling filtered content gracefully
3235
func Example_usingAzureContentFiltering() {
33-
if !CheckRequiredEnvVars("AOAI_ENDPOINT", "AOAI_MODEL") {
34-
fmt.Fprintf(os.Stderr, "Environment variables are not set, not running example.")
35-
return
36-
}
37-
3836
endpoint := os.Getenv("AOAI_ENDPOINT")
3937
model := os.Getenv("AOAI_MODEL")
38+
apiVersion := os.Getenv("AZURE_OPENAI_API_VERSION")
4039

41-
client, err := CreateOpenAIClientWithToken(endpoint, "")
40+
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
4241
if err != nil {
4342
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
4443
return
4544
}
4645

46+
client := openai.NewClient(
47+
azure.WithEndpoint(endpoint, apiVersion),
48+
azure.WithTokenCredential(tokenCredential),
49+
)
50+
4751
// Standard OpenAI chat completion request
4852
chatParams := openai.ChatCompletionNewParams{
4953
Model: openai.ChatModel(model),
@@ -135,27 +139,29 @@ func Example_usingAzureContentFiltering() {
135139
// The example uses environment variables for configuration:
136140
// - AOAI_ENDPOINT: Your Azure OpenAI endpoint URL
137141
// - AOAI_MODEL: The deployment name of your model
142+
// - AZURE_OPENAI_API_VERSION: Azure OpenAI service API version to use. See https://learn.microsoft.com/azure/ai-foundry/openai/api-version-lifecycle?tabs=go for information about API versions.
138143
//
139144
// Streaming with prompt filtering is useful for:
140145
// - Real-time content moderation
141146
// - Progressive content delivery
142147
// - Monitoring content safety during generation
143148
// - Building responsive applications with content safety checks
144149
func Example_usingAzurePromptFilteringWithStreaming() {
145-
if !CheckRequiredEnvVars("AOAI_ENDPOINT", "AOAI_MODEL") {
146-
fmt.Fprintf(os.Stderr, "Environment variables are not set, not running example.")
147-
return
148-
}
149-
150150
endpoint := os.Getenv("AOAI_ENDPOINT")
151151
model := os.Getenv("AOAI_MODEL")
152+
apiVersion := os.Getenv("AZURE_OPENAI_API_VERSION")
152153

153-
client, err := CreateOpenAIClientWithToken(endpoint, "")
154+
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
154155
if err != nil {
155156
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
156157
return
157158
}
158159

160+
client := openai.NewClient(
161+
azure.WithEndpoint(endpoint, apiVersion),
162+
azure.WithTokenCredential(tokenCredential),
163+
)
164+
159165
// Example of streaming with Azure extensions
160166
fmt.Fprintf(os.Stderr, "Streaming example:\n")
161167
streamingParams := openai.ChatCompletionNewParams{

0 commit comments

Comments
 (0)