Repository navigation
[Fix] Azure OpenAI 404 does not name the missing deployment #2172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ed4cea3
53b364a
f3ccca5
448208d
f48e15f
b1136b4
1bf03fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { | ||
| azureOpenAiDefaultApiVersion, | ||
| getAzureOpenAiBaseUrl, | ||
| getAzureOpenAiChatCompletionsUrl, | ||
| } from "../providers/openai.js" | ||
|
|
||
| describe("Azure OpenAI URLs", () => { | ||
| it.each([ | ||
| ["https://resource.openai.azure.com", "https://resource.openai.azure.com/openai"], | ||
| ["https://resource.openai.azure.com/", "https://resource.openai.azure.com/openai"], | ||
| ["https://resource.openai.azure.com/openai", "https://resource.openai.azure.com/openai"], | ||
| ["https://resource.openai.azure.com/OpenAI/", "https://resource.openai.azure.com/openai"], | ||
| ["https://models.example.com/azure/", "https://models.example.com/azure/openai"], | ||
| ])("normalizes %s to the /openai root", (baseUrl, expected) => { | ||
| expect(getAzureOpenAiBaseUrl(baseUrl)).toBe(expected) | ||
| }) | ||
|
|
||
| it.each([ | ||
| { | ||
| apiVersion: "2025-01-01-preview", | ||
| expected: | ||
| "https://resource.openai.azure.com/openai/deployments/brotherhood/chat/completions?api-version=2025-01-01-preview", | ||
| }, | ||
| { | ||
| apiVersion: undefined, | ||
| expected: `https://resource.openai.azure.com/openai/deployments/brotherhood/chat/completions?api-version=${azureOpenAiDefaultApiVersion}`, | ||
| }, | ||
| { | ||
| apiVersion: "", | ||
| expected: `https://resource.openai.azure.com/openai/deployments/brotherhood/chat/completions?api-version=${azureOpenAiDefaultApiVersion}`, | ||
| }, | ||
| { | ||
| apiVersion: "2025-01-01&x=1#frag", | ||
| expected: | ||
| "https://resource.openai.azure.com/openai/deployments/brotherhood/chat/completions?api-version=2025-01-01%26x%3D1%23frag", | ||
| }, | ||
| ])("builds the deployment chat completions URL for API version $apiVersion", ({ apiVersion, expected }) => { | ||
| expect(getAzureOpenAiChatCompletionsUrl("https://resource.openai.azure.com/", "brotherhood", apiVersion)).toBe( | ||
| expected, | ||
| ) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import { | ||
| type ModelInfo, | ||
| azureOpenAiDefaultApiVersion, | ||
| getAzureOpenAiBaseUrl, | ||
| isAzureAiInferenceBaseUrl, | ||
| isAzureOpenAiBaseUrl, | ||
| openAiModelInfoSaneDefaults, | ||
|
|
@@ -15,6 +16,7 @@ | |
| } from "@roo-code/types" | ||
|
|
||
| import type { ApiHandlerOptions } from "../../shared/api" | ||
| import { t } from "../../i18n" | ||
|
|
||
| import { TagMatcher } from "../../utils/tag-matcher" | ||
|
|
||
|
|
@@ -27,7 +29,7 @@ | |
| import { DEFAULT_HEADERS, NOT_PROVIDED } from "./constants" | ||
| import { BaseProvider } from "./base-provider" | ||
| import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index" | ||
| import { handleOpenAIError } from "./utils/error-handler" | ||
| import { handleOpenAIError, handleProviderError } from "./utils/error-handler" | ||
| import { extractReasoningFromDelta } from "./utils/extract-reasoning" | ||
|
|
||
| type OpenAiUsage = | ||
|
|
@@ -49,6 +51,7 @@ | |
| protected client: OpenAI | ||
| private readonly providerName = "OpenAI" | ||
| private readonly extraBody: Record<string, unknown> | ||
| private readonly isAzureOpenAi: boolean | ||
|
|
||
| constructor(options: ApiHandlerOptions) { | ||
| super() | ||
|
|
@@ -59,6 +62,7 @@ | |
| const apiKey = this.options.openAiApiKey ?? NOT_PROVIDED | ||
| const isAzureAiInference = this._isAzureAiInference(this.options.openAiBaseUrl) | ||
| const isAzureOpenAi = isAzureOpenAiBaseUrl(this.options.openAiBaseUrl, options.openAiUseAzure) | ||
| this.isAzureOpenAi = isAzureOpenAi | ||
|
|
||
| const headers = { | ||
| ...DEFAULT_HEADERS, | ||
|
|
@@ -77,9 +81,8 @@ | |
| } else if (isAzureOpenAi) { | ||
| // Azure API shape slightly differs from the core API shape: | ||
| // https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai | ||
| const azureBaseURL = `${baseURL.replace(/\/openai\/?$/i, "").replace(/\/$/, "")}/openai` | ||
| this.client = new AzureOpenAI({ | ||
| baseURL: azureBaseURL, | ||
| baseURL: getAzureOpenAiBaseUrl(baseURL), | ||
| apiKey, | ||
| apiVersion: this.options.azureApiVersion || azureOpenAiDefaultApiVersion, | ||
| defaultHeaders: headers, | ||
|
|
@@ -204,7 +207,7 @@ | |
| ...(isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}), | ||
| }) | ||
| } catch (error) { | ||
| throw handleOpenAIError(error, this.providerName) | ||
| throw this.handleRequestError(error, modelId) | ||
| } | ||
|
|
||
| const matcher = new TagMatcher( | ||
|
|
@@ -272,7 +275,7 @@ | |
| ...(this._isAzureAiInference(modelUrl) ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}), | ||
| }) | ||
| } catch (error) { | ||
| throw handleOpenAIError(error, this.providerName) | ||
| throw this.handleRequestError(error, modelId) | ||
| } | ||
|
|
||
| const message = response.choices?.[0]?.message | ||
|
|
@@ -377,13 +380,14 @@ | |
| isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}, | ||
| ) | ||
| } catch (error) { | ||
| throw handleOpenAIError(error, this.providerName) | ||
| throw this.handleRequestError(error, model.id) | ||
| } | ||
|
|
||
| return response.choices?.[0]?.message.content || "" | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| const wrapped = new Error(`${this.providerName} completion error: ${error.message}`, { cause: error }) | ||
| const prefix = `${this.providerName} completion error:` | ||
| if (error instanceof Error && !error.message.startsWith(prefix)) { | ||
|
Check warning on line 389 in src/api/providers/openai.ts
|
||
| const wrapped = new Error(`${prefix} ${error.message}`, { cause: error }) | ||
|
Check warning on line 390 in src/api/providers/openai.ts
|
||
|
Comment on lines
+389
to
+390
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a regression test for the outer error wrapper. The mutation report shows that replacing this condition with In As per path instructions: “When Stryker reports a surviving mutant on changed code, prefer a test that kills it.”
|
||
| const source = error as Error & { status?: number; errorDetails?: unknown; code?: unknown } | ||
| const target = wrapped as Error & { status?: number; errorDetails?: unknown; code?: unknown } | ||
| if (source.status !== undefined) target.status = source.status | ||
|
|
@@ -444,7 +448,7 @@ | |
| ...(methodIsAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}), | ||
| }) | ||
| } catch (error) { | ||
| throw handleOpenAIError(error, this.providerName) | ||
| throw this.handleRequestError(error, modelId) | ||
| } | ||
|
|
||
| yield* this.handleStreamResponse(stream) | ||
|
|
@@ -479,7 +483,7 @@ | |
| ...(methodIsAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}), | ||
| }) | ||
| } catch (error) { | ||
| throw handleOpenAIError(error, this.providerName) | ||
| throw this.handleRequestError(error, modelId) | ||
| } | ||
|
|
||
| const message = response.choices?.[0]?.message | ||
|
|
@@ -581,6 +585,25 @@ | |
| return urlHost === "api.x.ai" || urlHost.endsWith(".x.ai") | ||
| } | ||
|
|
||
| /** | ||
| * Azure OpenAI routes each request to `/openai/deployments/<model>`, so a 404 there | ||
| * almost always means the configured deployment name does not exist. Name the | ||
| * deployment in the error instead of surfacing a bare "Resource not found". | ||
| */ | ||
| private handleRequestError(error: unknown, deploymentName: string): Error { | ||
| if (this.isAzureOpenAi && isNotFoundError(error)) { | ||
| return handleProviderError(error, this.providerName, { | ||
| messageTransformer: (message) => | ||
| `${this.providerName} completion error: ${t("common:errors.api.azureDeploymentNotFound", { | ||
| deployment: deploymentName, | ||
| message, | ||
| })}`, | ||
|
PierrunoYT marked this conversation as resolved.
Resolved
|
||
| }) | ||
| } | ||
|
|
||
| return handleOpenAIError(error, this.providerName) | ||
| } | ||
|
|
||
| protected _isAzureAiInference(baseUrl?: string): boolean { | ||
| return isAzureAiInferenceBaseUrl(baseUrl) | ||
| } | ||
|
|
@@ -609,6 +632,10 @@ | |
| } | ||
| } | ||
|
|
||
| function isNotFoundError(error: unknown): boolean { | ||
| return typeof error === "object" && error !== null && "status" in error && error.status === 404 | ||
|
Check warning on line 636 in src/api/providers/openai.ts
|
||
| } | ||
|
|
||
| export async function getOpenAiModels(baseUrl?: string, apiKey?: string, openAiHeaders?: Record<string, string>) { | ||
| try { | ||
| if (!baseUrl) { | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.