Skip to content

Commit 77411b0

Browse files
Merge pull request #178 from jamesrochabrun/jroch-azure-fix
Fix for Azure Real time API
2 parents eacb892 + 26cb0ef commit 77411b0

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Sources/OpenAI/Public/Service/DefaultOpenAIService.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,24 @@ struct DefaultOpenAIService: OpenAIService {
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)"
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+
}
92108

93109
guard let url = URL(string: urlString) else {
94110
throw APIError.requestFailed(description: "Invalid realtime session URL")
@@ -97,7 +113,11 @@ struct DefaultOpenAIService: OpenAIService {
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")
116+
117+
// Only add openai-beta header for non-Azure endpoints
118+
if !isAzureEndpoint {
119+
request.setValue("realtime=v1", forHTTPHeaderField: "openai-beta")
120+
}
101121

102122
if let organizationID {
103123
request.setValue(organizationID, forHTTPHeaderField: "OpenAI-Organization")

0 commit comments

Comments
 (0)