From d08b28821efc6f46b6a5c14cd446fdc256b73367 Mon Sep 17 00:00:00 2001 From: octo-patch Date: Wed, 22 Apr 2026 20:07:28 +0800 Subject: [PATCH] fix: use createAzure in validate-model and fix resourceName fallback (fixes #819) Two related bugs caused Azure OpenAI to pass validation but fail in actual use: 1. validate-model/route.ts used createOpenAI for Azure, while ai-providers.ts uses createAzure. These differ in URL construction and authentication headers (api-key vs Authorization: Bearer), so validation did not test the real code path. Switch to createAzure for consistency. 2. In ai-providers.ts, when a user provided their own API key without a custom base URL, resourceName was unconditionally set to undefined. This left createAzure with no endpoint information, resulting in resource not found. resourceName is an endpoint component, not a credential, so it is safe to fall back to AZURE_RESOURCE_NAME from the environment whenever no baseURL is available. --- app/api/validate-model/route.ts | 7 ++++--- lib/ai-providers.ts | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/api/validate-model/route.ts b/app/api/validate-model/route.ts index d9964677..68ccc29d 100644 --- a/app/api/validate-model/route.ts +++ b/app/api/validate-model/route.ts @@ -4,6 +4,7 @@ import { createDeepSeek, deepseek } from "@ai-sdk/deepseek" import { createGateway } from "@ai-sdk/gateway" import { createGoogleGenerativeAI } from "@ai-sdk/google" import { createVertex } from "@ai-sdk/google-vertex" +import { createAzure } from "@ai-sdk/azure" import { createOpenAI } from "@ai-sdk/openai" import { createOpenRouter } from "@openrouter/ai-sdk-provider" import { generateText } from "ai" @@ -126,11 +127,11 @@ export async function POST(req: Request) { } case "azure": { - const azure = createOpenAI({ + const azureClient = createAzure({ apiKey, - baseURL: baseUrl, + ...(baseUrl && { baseURL: baseUrl }), }) - model = azure.chat(modelId) + model = azureClient(modelId) break } diff --git a/lib/ai-providers.ts b/lib/ai-providers.ts index 5c0aa7e3..3137adfe 100644 --- a/lib/ai-providers.ts +++ b/lib/ai-providers.ts @@ -913,10 +913,10 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig { overrides?.baseUrl, serverBaseUrl, ) - // Only use server's resourceName if user is NOT providing their own API key - const resourceName = overrides?.apiKey - ? undefined - : process.env.AZURE_RESOURCE_NAME + // Only use server's resourceName when no baseURL is available. + // resourceName is an endpoint component (not a credential), so it is + // safe to use as a fallback even when the user brings their own API key. + const resourceName = !baseURL ? process.env.AZURE_RESOURCE_NAME : undefined // Azure requires either baseURL or resourceName to construct the endpoint // resourceName constructs: https://{resourceName}.openai.azure.com/openai/v1{path} if (baseURL || resourceName || overrides?.apiKey) {