Skip to content

Commit 3af401d

Browse files
Support for azure
1 parent eacb892 commit 3af401d

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

Sources/OpenAI/Public/Service/DefaultOpenAIService.swift

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,55 @@ struct DefaultOpenAIService: OpenAIService {
8282
func realtimeSession(
8383
model: String,
8484
configuration: OpenAIRealtimeSessionConfiguration)
85-
async throws -> OpenAIRealtimeSession
85+
async throws -> OpenAIRealtimeSession
8686
{
8787
// Build the WebSocket URL
8888
let baseURL = openAIEnvironment.baseURL.replacingOccurrences(of: "https://", with: "wss://")
8989
let version = openAIEnvironment.version ?? "v1"
90-
let path = openAIEnvironment.proxyPath.map { "\($0)/\(version)" } ?? version
91-
let urlString = "\(baseURL)/\(path)/realtime?model=\(model)"
92-
90+
91+
// Check if this is an Azure endpoint (contains "azure_openai" in base URL or proxy path)
92+
let isAzureEndpoint = openAIEnvironment.baseURL.contains("azure_openai") ||
93+
(openAIEnvironment.proxyPath?.contains("azure_openai") ?? false)
94+
95+
let path: String
96+
let urlString: String
97+
98+
if isAzureEndpoint {
99+
// Azure format: path/realtime?api-version=X&deployment=Y
100+
// For Airbnb's Azure proxy, deployment is passed as a query parameter
101+
path = openAIEnvironment.proxyPath ?? ""
102+
urlString = "\(baseURL)/\(path)/realtime?api-version=\(version)&deployment=\(model)"
103+
} else {
104+
// OpenAI format: path/version/realtime?model=Y
105+
path = openAIEnvironment.proxyPath.map { "\($0)/\(version)" } ?? version
106+
urlString = "\(baseURL)/\(path)/realtime?model=\(model)"
107+
}
108+
93109
guard let url = URL(string: urlString) else {
94110
throw APIError.requestFailed(description: "Invalid realtime session URL")
95111
}
96-
112+
97113
// Create the WebSocket request with auth headers
98114
var request = URLRequest(url: url)
99115
request.setValue(apiKey.value, forHTTPHeaderField: apiKey.headerField)
100-
request.setValue("realtime=v1", forHTTPHeaderField: "openai-beta")
101-
116+
117+
// Only add openai-beta header for non-Azure endpoints
118+
if !isAzureEndpoint {
119+
request.setValue("realtime=v1", forHTTPHeaderField: "openai-beta")
120+
}
121+
102122
if let organizationID {
103123
request.setValue(organizationID, forHTTPHeaderField: "OpenAI-Organization")
104124
}
105-
125+
106126
// Add any extra headers
107127
extraHeaders?.forEach { key, value in
108128
request.setValue(value, forHTTPHeaderField: key)
109129
}
110-
130+
111131
// Create the WebSocket task
112132
let webSocketTask = URLSession.shared.webSocketTask(with: request)
113-
133+
114134
// Return the realtime session
115135
return OpenAIRealtimeSession(
116136
webSocketTask: webSocketTask,

0 commit comments

Comments
 (0)