The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[BOT-1662] Support for Microsoft Azure BYOK (#75768)
Tyler Pludeeca496d66e93
diff --git a/frontend/src/metabase-types/api/metabot.ts b/frontend/src/metabase-types/api/metabot.tsindex 7633ea627f4..a6f67ba9758 100644--- a/frontend/src/metabase-types/api/metabot.ts+++ b/frontend/src/metabase-types/api/metabot.ts@@ -189,6 +189,7 @@ export type MetabotAgentResponse = {export type MetabotProvider =| "metabase"| "anthropic"+ | "azure"| "bedrock"| "openai"| "openrouter";@@ -200,6 +201,16 @@ export interface BedrockCredentials {"session-token"?: string | null;}+export interface AzureCredentials {+ "api-key"?: string | null;+ "base-url"?: string | null;+}++/** One permissive map mirroring the backend's request schema: Bedrock sends AWS key+ * material, Azure sends an API key and base URL. */+export interface MetabotCredentials+ extends BedrockCredentials, AzureCredentials {}+export interface MetabotSettingsResponse {value: string | null;"credentials-error"?: string | null;@@ -214,7 +225,7 @@ export interface UpdateMetabotSettingsRequest {provider: MetabotProvider;model?: string;"api-key"?: string | null;- credentials?: BedrockCredentials | null;+ credentials?: MetabotCredentials | null;}/* Metabot - Suggested Prompts */diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 8d374b91f82..c95d927a2a0 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -534,6 +534,8 @@ interface SettingsManagerSettings {"llm-anthropic-api-key"?: string | null;"llm-anthropic-api-base-url"?: string | null;"llm-openrouter-api-key"?: string | null;+ "llm-azure-api-key"?: string | null;+ "llm-azure-api-base-url"?: string | null;"llm-bedrock-access-key-id"?: string | null;"llm-bedrock-secret-access-key"?: string | null;"llm-bedrock-region"?: string | null;diff --git a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsxindex 74ab5c436ed..9e66c68c3ee 100644--- a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx@@ -19,6 +19,7 @@ import { reinitialize } from "metabase/plugins";import { defer } from "metabase/utils/promise";import type {BedrockCredentials,+ MetabotCredentials,MetabotProvider,MetabotSettingsResponse,SettingDefinition,@@ -67,6 +68,11 @@ const DEFAULT_RESPONSES: Record<MetabotProvider, MetabotSettingsResponse> = {},],},+ azure: {+ // Azure has no model dropdown — deployment names are free text.+ value: "azure/anthropic/claude-sonnet-4-5",+ models: [],+ },bedrock: {value: "bedrock/anthropic.claude-haiku-4-5",models: [@@ -115,6 +121,8 @@ type MetabotSettingsApiResponse =type MetabotSettingKey =| "llm-metabot-provider"| "llm-anthropic-api-key"+ | "llm-azure-api-key"+ | "llm-azure-api-base-url"| "llm-openai-api-key"| "llm-openrouter-api-key"| "llm-bedrock-access-key-id"@@ -127,7 +135,7 @@ type MetabotSettingsUpdateBody = {provider: MetabotProvider;model?: string;"api-key"?: string | null;- credentials?: BedrockCredentials | null;+ credentials?: MetabotCredentials | null;};type SetupOptions = {@@ -202,10 +210,11 @@ async function setup({const updateMetabotSettingsDeferred = defer<void>();const mergedApiKeyValues: Record<- MetabotApiKeyProvider | "bedrock",+ MetabotApiKeyProvider | "azure" | "bedrock",string | null> = {anthropic: "**********45",+ azure: null,bedrock: null,openai: null,openrouter: null,@@ -252,6 +261,17 @@ async function setup({is_env_setting: apiKeySettingIsEnv,env_name: apiKeySettingIsEnv ? apiKeySettingEnvName : undefined,}),+ "llm-azure-api-key": createMockSettingDefinition({+ key: "llm-azure-api-key",+ value: mergedApiKeyValues.azure ?? undefined,+ }),+ // The base URL is configured whenever the Azure API key is — they are saved together.+ "llm-azure-api-base-url": createMockSettingDefinition({+ key: "llm-azure-api-base-url",+ value: mergedApiKeyValues.azure+ ? "https://my-resource.services.ai.azure.com/anthropic"+ : undefined,+ }),"llm-openai-api-key": createMockSettingDefinition({key: "llm-openai-api-key",value: mergedApiKeyValues.openai ?? undefined,@@ -1767,6 +1787,205 @@ describe("AIProviderSettingsSection", () => {});});+ describe("Microsoft Azure", () => {+ it("shows Microsoft Azure as selectable in the provider dropdown", async () => {+ await setup({ savedProviderValue: null, isConfigured: false });++ await userEvent.click(screen.getByLabelText("Provider"));++ const azureOption = await screen.findByRole("option", {+ name: "Microsoft Azure",+ });+ expect(azureOption).toBeInTheDocument();+ expect(azureOption).not.toHaveAttribute("data-combobox-disabled");+ });++ it("shows the Azure fields without a model dropdown when selected", async () => {+ await setup({ savedProviderValue: null, isConfigured: false });++ await selectProvider("Microsoft Azure");++ expect(+ await screen.findByLabelText("Model provider"),+ ).toBeInTheDocument();+ expect(screen.getByLabelText("Base URL")).toBeInTheDocument();+ expect(screen.getByLabelText("API key")).toBeInTheDocument();+ expect(screen.getByLabelText("Deployment name")).toBeInTheDocument();+ expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();+ });++ it("connects Azure by sending the composed model and the credentials object", async () => {+ await setup({+ savedProviderValue: null,+ isConfigured: false,+ updateResponse: {+ value: "azure/openai/my-deployment",+ models: [],+ },+ });++ await selectProvider("Microsoft Azure");++ const connectButton = screen.getByRole("button", { name: "Connect" });+ expect(connectButton).toBeDisabled();++ await userEvent.click(await screen.findByLabelText("Model provider"));+ await userEvent.click(+ await screen.findByRole("option", { name: "OpenAI" }),+ );+ await userEvent.type(+ screen.getByLabelText("Base URL"),+ "https://my-resource.services.ai.azure.com/openai",+ );+ await userEvent.type(screen.getByLabelText("API key"), "azure-key");+ await userEvent.type(+ screen.getByLabelText("Deployment name"),+ "my-deployment",+ );++ expect(connectButton).toBeEnabled();+ await userEvent.click(connectButton);++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/metabot/settings", {+ method: "PUT",+ }),+ ).toBe(true);+ });++ const [request] = fetchMock.callHistory.calls(+ "path:/api/metabot/settings",+ { method: "PUT" },+ );++ expect(request?.options?.body).toBe(+ JSON.stringify({+ provider: "azure",+ model: "openai/my-deployment",+ credentials: {+ "api-key": "azure-key",+ "base-url": "https://my-resource.services.ai.azure.com/openai",+ },+ }),+ );+ });++ it("shows the saved family, base URL, and deployment for a connected Azure provider", async () => {+ await setup({+ savedProviderValue: "azure/anthropic/claude-sonnet-4-5",+ apiKeyValues: { azure: "**********ey" },+ });++ expect(await screen.findByLabelText("Model provider")).toHaveValue(+ "Anthropic",+ );+ expect(screen.getByLabelText("Base URL")).toHaveValue(+ "https://my-resource.services.ai.azure.com/anthropic",+ );+ expect(screen.getByLabelText("Deployment name")).toHaveValue(+ "claude-sonnet-4-5",+ );+ expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: "Disconnect" }),+ ).toBeInTheDocument();+ });++ it("sends untouched Azure credential fields as null when editing only the deployment", async () => {+ await setup({+ savedProviderValue: "azure/anthropic/claude-sonnet-4-5",+ apiKeyValues: { azure: "**********ey" },+ updateResponse: {+ value: "azure/anthropic/renamed-deployment",+ models: [],+ },+ });++ const deploymentInput = await screen.findByLabelText("Deployment name");+ await userEvent.clear(deploymentInput);+ await userEvent.type(deploymentInput, "renamed-deployment");++ await userEvent.click(screen.getByRole("button", { name: "Connect" }));++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/metabot/settings", {+ method: "PUT",+ }),+ ).toBe(true);+ });++ const [request] = fetchMock.callHistory.calls(+ "path:/api/metabot/settings",+ { method: "PUT" },+ );++ // The untouched key and base URL round-trip as displayed values in the form, but must be+ // sent as null so the backend keeps the real saved values.+ expect(request?.options?.body).toBe(+ JSON.stringify({+ provider: "azure",+ model: "anthropic/renamed-deployment",+ credentials: {+ "api-key": null,+ "base-url": null,+ },+ }),+ );+ });++ it("disconnects Azure by clearing the credentials before the provider setting", async () => {+ await setup({+ savedProviderValue: "azure/anthropic/claude-sonnet-4-5",+ apiKeyValues: { azure: "**********ey" },+ });++ await screen.findByLabelText("Deployment name");+ await confirmDisconnectProvider();++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/metabot/settings", {+ method: "PUT",+ body: { provider: "azure", credentials: null },+ }),+ ).toBe(true);+ });++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/setting", {+ method: "PUT",+ body: { "llm-metabot-provider": null },+ }),+ ).toBe(true);+ });++ const callHistory = fetchMock.callHistory.calls();+ const [credentialsRequest] = fetchMock.callHistory.calls(+ "path:/api/metabot/settings",+ { method: "PUT" },+ );+ const [providerRequest] = fetchMock.callHistory.calls(+ "path:/api/setting",+ { method: "PUT" },+ );++ if (!credentialsRequest || !providerRequest) {+ throw new Error("Expected credentials and provider requests to exist");+ }++ expect(callHistory.indexOf(credentialsRequest)).toBeLessThan(+ callHistory.indexOf(providerRequest),+ );++ expect(+ await screen.findByText("Connect to an AI provider"),+ ).toBeInTheDocument();+ });+ });+describe("Amazon Bedrock", () => {it("shows Amazon Bedrock as selectable in the provider dropdown", async () => {await setup({ savedProviderValue: null, isConfigured: false });diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsxindex 75de327cf84..5b452f4b88d 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx@@ -20,6 +20,7 @@ import type { MetabotProvider } from "metabase-types/api";import { AIProviderConfigurationContext } from "./AIProviderConfigurationContext";import { ApiKeyProviderFields } from "./ApiKeyProviderFields";+import { AzureProviderFields } from "./AzureProviderFields";import { BedrockProviderFields } from "./BedrockProviderFields";import {API_KEY_SETTING_BY_PROVIDER,@@ -94,7 +95,11 @@ export function AIProviderConfigurationForm({"llm-metabot-provider": null,};- if (connectedProvider !== "metabase" && connectedProvider !== "bedrock") {+ if (+ connectedProvider !== "metabase" &&+ connectedProvider !== "bedrock" &&+ connectedProvider !== "azure"+ ) {const apiKeySettingKey = API_KEY_SETTING_BY_PROVIDER[connectedProvider];const apiKeySetting = providerApiKeyDetails[apiKeySettingKey];@@ -104,12 +109,12 @@ export function AIProviderConfigurationForm({}try {- if (connectedProvider === "bedrock") {- // Bedrock key material spans several settings; an explicit `credentials: null`- // clears them all in one call. It runs before the provider is deselected so a- // failure can't leave saved keys behind.+ if (connectedProvider === "bedrock" || connectedProvider === "azure") {+ // Bedrock and Azure key material spans several settings; an explicit+ // `credentials: null` clears them all in one call. It runs before the provider+ // is deselected so a failure can't leave saved keys behind.await updateMetabotSettings({- provider: "bedrock",+ provider: connectedProvider,credentials: null,}).unwrap();}@@ -247,6 +252,13 @@ export function AIProviderConfigurationForm({.with("metabase", () => (<MetabaseAIProviderSetup onConnect={onClose} />))+ .with("azure", () => (+ <AzureProviderFields+ connectedModel={connectedModel}+ isCurrentConfigured={isCurrentConfigured}+ isEnvSetting={isEnvSetting}+ />+ )).with("bedrock", () => (<BedrockProviderFieldsconnectedModel={connectedModel}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AzureProviderFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AzureProviderFields.tsxnew file mode 100644index 00000000000..f263f36b3e0--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AzureProviderFields.tsx@@ -0,0 +1,174 @@+import { type FormikHelpers, useFormikContext } from "formik";+import { useMemo } from "react";+import { c, t } from "ttag";++import { useUpdateMetabotSettingsMutation } from "metabase/api";+import { useAdminSettings } from "metabase/api/utils";+import { ExternalLink } from "metabase/common/components/ExternalLink";+import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";+import {+ FormErrorMessage,+ FormProvider,+ FormSelect,+ FormTextInput,+} from "metabase/forms";++import { useAIProviderConfigurationContext } from "./AIProviderConfigurationContext";+import {+ AZURE_MODEL_FAMILIES,+ getProviderOptions,+ parseAzureModel,+} from "./utils";++const AZURE_SETTING_KEYS = [+ "llm-azure-api-key",+ "llm-azure-api-base-url",+] as const;++type AzureCredentialValues = {+ family: string;+ apiKey: string;+ baseUrl: string;+ deployment: string;+};++export const AzureProviderFields = ({+ connectedModel,+ isCurrentConfigured,+ isEnvSetting,+}: {+ connectedModel: string | undefined;+ isCurrentConfigured: boolean;+ isEnvSetting: boolean;+}) => {+ const [updateMetabotSettings] = useUpdateMetabotSettingsMutation();+ const { details } = useAdminSettings(AZURE_SETTING_KEYS);++ const initialValues = useMemo<AzureCredentialValues>(() => {+ const { family, deployment } = parseAzureModel(+ isCurrentConfigured ? connectedModel : undefined,+ );++ return {+ family: family ?? "",+ apiKey: String(details["llm-azure-api-key"]?.value ?? ""),+ baseUrl: String(details["llm-azure-api-base-url"]?.value ?? ""),+ deployment: deployment ?? "",+ };+ }, [connectedModel, details, isCurrentConfigured]);++ const handleSubmit = async (+ values: AzureCredentialValues,+ { resetForm }: FormikHelpers<AzureCredentialValues>,+ ) => {+ const changedValueOrNull = (field: "apiKey" | "baseUrl") =>+ values[field] !== initialValues[field] ? values[field] || null : null;++ await updateMetabotSettings({+ provider: "azure",+ model: `${values.family}/${values.deployment}`,+ credentials: {+ "api-key": changedValueOrNull("apiKey"),+ "base-url": changedValueOrNull("baseUrl"),+ },+ }).unwrap();++ resetForm({ values });+ };++ return (+ <FormProvider+ initialValues={initialValues}+ onSubmit={handleSubmit}+ enableReinitialize+ >+ <AzureCredentialFields+ isCurrentConfigured={isCurrentConfigured}+ isEnvSetting={isEnvSetting}+ />+ </FormProvider>+ );+};++const AzureCredentialFields = ({+ isCurrentConfigured,+ isEnvSetting,+}: {+ isCurrentConfigured: boolean;+ isEnvSetting: boolean;+}) => {+ const { dirty, submitForm, values } =+ useFormikContext<AzureCredentialValues>();++ const { details } = useAdminSettings(AZURE_SETTING_KEYS);+ const apiKeySetting = details["llm-azure-api-key"];+ const baseUrlSetting = details["llm-azure-api-base-url"];++ const apiKeyEnvName = apiKeySetting?.is_env_setting+ ? apiKeySetting.env_name+ : undefined;+ const baseUrlEnvName = baseUrlSetting?.is_env_setting+ ? baseUrlSetting.env_name+ : undefined;++ const isComplete =+ !!values.family &&+ !!values.apiKey.trim() &&+ !!values.baseUrl.trim() &&+ !!values.deployment.trim();+ const connectHandler =+ isComplete && (!isCurrentConfigured || dirty) ? submitForm : null;+ const { isMutating } = useAIProviderConfigurationContext(connectHandler);++ const providerDetails = getProviderOptions(true).azure;++ return (+ <>+ <FormSelect+ name="family"+ label={t`Model provider`}+ description={t`Whether your deployment serves an Anthropic or an OpenAI model.`}+ placeholder={t`Select a model provider`}+ data={[...AZURE_MODEL_FAMILIES]}+ disabled={isMutating || isEnvSetting}+ />++ <FormTextInput+ name="baseUrl"+ label={t`Base URL`}+ description={t`The full URL of your Azure resource's model-provider surface, including the provider segment.`}+ placeholder="https://<resource>.services.ai.azure.com/openai"+ disabled={isMutating || isEnvSetting || !!baseUrlEnvName}+ w="100%"+ />+ {baseUrlEnvName && <SetByEnvVar varName={baseUrlEnvName} />}++ <FormTextInput+ name="apiKey"+ label={t`API key`}+ type="password"+ description={+ <ExternalLink href={providerDetails.apiKey.addKeyUrl}>+ {c("{0} is the name of an AI provider")+ .t`Get or manage keys in ${providerDetails.label}`}+ </ExternalLink>+ }+ placeholder={providerDetails.apiKey.placeholder}+ disabled={isMutating || isEnvSetting || !!apiKeyEnvName}+ w="100%"+ />+ {apiKeyEnvName && <SetByEnvVar varName={apiKeyEnvName} />}++ <FormTextInput+ name="deployment"+ label={t`Deployment name`}+ description={t`The name of the model deployment on your Azure resource. We recommend naming deployments after the model they serve.`}+ placeholder={t`Enter your Azure deployment name`}+ disabled={isMutating || isEnvSetting}+ w="100%"+ />++ <FormErrorMessage />+ </>+ );+};diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.tsindex a625632c255..d8a007090f2 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts@@ -1,3 +1,5 @@+import { t } from "ttag";+import type { MetabotProvider, SettingDefinition } from "metabase-types/api";type ApiKeylessProviders = "metabase";@@ -41,6 +43,15 @@ export function getProviderOptions(addKeyUrl: "https://console.anthropic.com/settings/keys",},},+ azure: {+ value: "azure",+ label: "Microsoft Azure",+ apiKey: {+ // Azure data-plane keys have no recognizable prefix+ placeholder: t`Enter your Azure API key`,+ addKeyUrl: "https://ai.azure.com",+ },+ },bedrock: {value: "bedrock",label: "Amazon Bedrock",@@ -71,7 +82,7 @@ export function getProviderOptions(export type MetabotApiKeyProvider = Exclude<MetabotProvider,- "metabase" | "bedrock"+ "metabase" | "azure" | "bedrock">;export function isMetabotProvider(@@ -83,6 +94,7 @@ export function isMetabotProvider(export function isAvailableProvider(provider: MetabotProvider): boolean {return (provider === "anthropic" ||+ provider === "azure" ||provider === "bedrock" ||provider === "metabase");@@ -97,6 +109,16 @@ export const API_KEY_SETTING_BY_PROVIDER: Record<openrouter: "llm-openrouter-api-key",};+export const AZURE_MODEL_FAMILIES = [+ { value: "anthropic", label: "Anthropic" },+ { value: "openai", label: "OpenAI" },+] as const;++export function parseAzureModel(model: string | undefined) {+ const [family, deployment] = model?.split(/\/(.+)/, 2) ?? [];+ return { family, deployment };+}+export function parseProviderAndModel(value: string | null | undefined) {if (!value) {return undefined;diff --git a/src/metabase/llm/settings.clj b/src/metabase/llm/settings.cljindex b9868530c0c..44546d645b5 100644--- a/src/metabase/llm/settings.clj+++ b/src/metabase/llm/settings.clj@@ -186,6 +186,34 @@(trimmed-string (llm-bedrock-secret-access-key)))):doc false)+;;; ----------------------------------------------- Microsoft Azure ---------------------------------------------++(defsetting llm-azure-api-key+ (deferred-tru "The API key for the Azure resource hosting your models.")+ ;; Azure data-plane keys are unprefixed, so unlike the direct-provider keys there is no format validation.+ :sensitive? true+ :visibility :settings-manager+ :export? false+ :doc false+ :setter (partial set-trimmed-string! :llm-azure-api-key))++(defn normalize-llm-base-url+ "Trim whitespace and trailing slashes from an admin-entered LLM base URL; blank values become nil.+ The URL is otherwise persisted exactly as entered — admin-entered URLs are not silently rewritten."+ [value]+ (some-> (trimmed-string value)+ (str/replace #"/+$" "")+ not-empty))++(defsetting llm-azure-api-base-url+ (deferred-tru "The base URL of the Azure resource''s OpenAI- or Anthropic-compatible surface, e.g. https://<resource>.services.ai.azure.com/openai.")+ :encryption :no+ :visibility :settings-manager+ :export? false+ :doc false+ :setter (fn [new-value]+ (setting/set-value-of-type! :string :llm-azure-api-base-url (normalize-llm-base-url new-value))))+;;; --------------------------------------------------- Proxy ---------------------------------------------------(defsetting llm-proxy-base-urldiff --git a/src/metabase/metabot/api.clj b/src/metabase/metabot/api.cljindex fba49170d04..aff9da7a023 100644--- a/src/metabase/metabot/api.clj+++ b/src/metabase/metabot/api.clj@@ -12,6 +12,7 @@[metabase.config.core :as config][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib]+ [metabase.llm.settings :as llm.settings][metabase.metabot.agent.core :as agent][metabase.metabot.api.conversations][metabase.metabot.api.document]@@ -304,19 +305,23 @@[:credentials-error {:optional true} [:maybe :string]][:models [:sequential llm-model-response-schema]]])-(def ^:private bedrock-credentials-schema+(def ^:private provider-credentials-schema+ "Provider credentials carried by the request body's `:credentials` map.+ Bedrock sends AWS key material; Azure sends an API key and base URL."[:map[:access-key-id {:optional true} [:maybe :string]][:secret-access-key {:optional true} [:maybe :string]][:region {:optional true} [:maybe :string]]- [:session-token {:optional true} [:maybe :string]]])+ [:session-token {:optional true} [:maybe :string]]+ [:api-key {:optional true} [:maybe :string]]+ [:base-url {:optional true} [:maybe :string]]])(def ^:private metabot-settings-request-schema[:map[:provider metabot-provider-schema][:model {:optional true} [:maybe :string]][:api-key {:optional true} [:maybe :string]]- [:credentials {:optional true} [:maybe bedrock-credentials-schema]]])+ [:credentials {:optional true} [:maybe provider-credentials-schema]]])(defn- provider-api-key-setting-key[provider]@@ -338,16 +343,18 @@(or (non-blank-string model)(metabot.settings/default-model-for-provider provider))))-(def ^:private invalid-api-key-statuses- #{401 403})--(defn- invalid-credentials-error?- "Whether a provider api-error means the credentials were rejected (or missing)."+(defn- provider-client-error?+ "Whether a provider api-error is a client-side 4xx we should surface rather than treat as an+ outage. Covers rejected or missing credentials (401/403) and a request the provider refused+ outright (e.g. a custom base URL pointing at the wrong surface, which 400s). `rethrow-api-error!`+ tags these with `:status`; other callers throw with `:status-code`. Provider 5xx and network+ failures are left to propagate as 500s so outages aren't reported as client errors."[error]- (let [status (or (:status (ex-data error))- (:status-code (ex-data error)))]- (and (:api-error (ex-data error))- (contains? invalid-api-key-statuses status))))+ (let [{:keys [api-error status status-code]} (ex-data error)+ status (or status status-code)]+ (and api-error+ (number? status)+ (<= 400 status 499))))(defn- title-case-token[token]@@ -419,11 +426,12 @@(defn- provider-models-response"List a provider's models.- Validate against `credentials-override` when provided and the provider's saved credentials otherwise. The shape of- the credentials map varies by provider. See [[metabot.settings/configured-provider-credentials]]."+ Validate against `:credentials` in `opts` when provided and the provider's saved credentials otherwise. The shape+ of the credentials map varies by provider (see [[metabot.settings/configured-provider-credentials]]). `:model` in+ `opts` is the candidate model for providers whose validation depends on it (Azure's wire family)."([provider](provider-models-response provider nil))- ([provider credentials-override]+ ([provider {credentials-override :credentials model :model}](if (= provider provider-util/metabase-provider-prefix){:models (decorate-provider-modelsprovider@@ -434,9 +442,10 @@(try{:models (decorate-provider-modelsprovider- (:models (metabot.self/list-models provider {:credentials credentials})))}+ (:models (metabot.self/list-models provider (cond-> {:credentials credentials}+ model (assoc :model model)))))}(catch clojure.lang.ExceptionInfo e- (if (invalid-credentials-error? e)+ (if (provider-client-error? e){:models []:credentials-error (.getMessage e)}(throw e))))@@ -445,10 +454,10 @@(defn- settings-response([provider](settings-response provider nil))- ([provider credentials-override]+ ([provider opts](merge{:value (metabot.settings/llm-metabot-provider)}- (provider-models-response provider credentials-override))))+ (provider-models-response provider opts))))(defn- current-provider[]@@ -492,17 +501,34 @@(metabot.settings/configured-provider-credentials "bedrock")bedrock-credential-fields))+(defn- effective-azure-credentials+ "The Azure credentials a settings request resolves to.++ Non-blank request fields are layered over the saved `llm-azure-*` settings, so e.g. a key-only rotation keeps the+ saved base URL. The settings are read individually (not via the all-or-nothing configured-credentials map) so a+ partially-configured or env-set field still participates in layering — completeness is the caller's check. The+ base URL is normalized the same way its setter normalizes it (whitespace/trailing-slash trim) so the validation+ round-trip exercises exactly what would be persisted."+ [{:keys [api-key base-url]}]+ {:api-key (or (non-blank-string api-key)+ (non-blank-string (llm.settings/llm-azure-api-key)))+ :base-url (or (llm.settings/normalize-llm-base-url base-url)+ (llm.settings/normalize-llm-base-url (llm.settings/llm-azure-api-base-url)))})+(defn- request-credentials"The credentials override carried by a `PUT /api/metabot/settings` request body as a provider credentials map.nil when the request does not touch credentials for `provider`.- An explicitly nil credential field in the body — `:api-key` for API-key providers, `:credentials` for Bedrock —- resolves to a credentials map whose key material is nil: an explicit clear. Fields *inside* the Bedrock credentials- map follow the same presence contract (see [[effective-bedrock-credentials]]). Throws a 400 when non-nil Bedrock- credentials don't resolve to a complete set."+ An explicitly nil credential field in the body — `:api-key` for API-key providers, `:credentials` for Bedrock and+ Azure — resolves to a credentials map whose key material is nil: an explicit clear. Fields *inside* the Bedrock+ credentials map follow that map's presence contract (see [[effective-bedrock-credentials]]); blank fields *inside*+ the Azure credentials map mean \"keep the saved value\" (see [[effective-azure-credentials]]), so e.g. a key-only+ rotation can't wipe the base URL. Throws a 400 when non-nil Bedrock/Azure credentials don't resolve to a complete+ set."[provider {:keys [api-key credentials] :as body}]- (if (= provider "bedrock")+ (case provider+ "bedrock"(when (contains? body :credentials)(if (nil? credentials){:access-key-id nil@@ -517,6 +543,21 @@:missing-keys (vec (remove #(non-blank-string (get creds %))[:access-key-id :secret-access-key]))})))creds)))++ "azure"+ (when (contains? body :credentials)+ (if (nil? credentials)+ {:api-key nil+ :base-url nil}+ (let [creds (effective-azure-credentials credentials)]+ (when-not (metabot.settings/provider-credentials-complete? provider creds)+ (throw (ex-info (tru "Azure credentials are incomplete.")+ {:status-code 400+ :api-error true+ :missing-keys (vec (remove #(non-blank-string (get creds %))+ [:api-key :base-url]))})))+ creds)))+(when (contains? body :api-key){:api-key (non-blank-string api-key)})))@@ -532,14 +573,45 @@(when (contains? credentials :region)(setting/set! :llm-bedrock-region (:region credentials))))+(defn- check-not-env-shadowed!+ "Throw a 400 when `setting-key` is controlled by an env var. Writes to env-shadowed settings+ persist to the app DB but the env var wins on every read, so they silently do nothing — reject+ them up front instead."+ [setting-key]+ (when (some? (setting/env-var-value setting-key))+ (throw (ex-info (tru "This setting is set by the {0} environment variable and cannot be changed via the API."+ (setting/env-var-name setting-key))+ {:status-code 400+ :setting setting-key}))))++(defn- save-azure-credentials!+ "Persist an Azure credentials map resolved by [[request-credentials]]; nil values clear those settings."+ [{:keys [api-key base-url]}]+ (setting/set! :llm-azure-api-key api-key)+ (setting/set! :llm-azure-api-base-url base-url))+(defn- save-credentials!"Persist the credentials override resolved by [[request-credentials]]; nil leaves the saved settings untouched."[provider credentials](when credentials- (if (= provider "bedrock")- (save-bedrock-credentials! credentials)+ (case provider+ "bedrock" (save-bedrock-credentials! credentials)+ "azure" (save-azure-credentials! credentials)(setting/set! (provider-api-key-setting-key provider) (:api-key credentials)))))+(defn- credential-setting-keys+ "The app-DB settings that persisting `provider`'s resolved `credentials` map would write+ (mirrors [[save-credentials!]]). Used to reject writes to env-shadowed settings up front: a write+ to an env-shadowed setting persists a DB row the env var then silently wins over. Bedrock writes+ `:llm-bedrock-region` only when the credentials carry it (see [[save-bedrock-credentials!]]), so it+ is guarded only then; the other fields are always written."+ [provider credentials]+ (case provider+ "bedrock" (cond-> [:llm-bedrock-access-key-id :llm-bedrock-secret-access-key :llm-bedrock-session-token]+ (contains? credentials :region) (conj :llm-bedrock-region))+ "azure" [:llm-azure-api-key :llm-azure-api-base-url]+ [(provider-api-key-setting-key provider)]))+(api.macros/defendpoint :put "/settings":- metabot-settings-response-schema"Update the Metabot provider credentials and/or model setting and return the refreshed settings payload."@@ -561,8 +633,33 @@:elsenil)+ ;; Azure has no default model: the FE composes `{family}/{deployment}` from required+ ;; inputs, so a connect (provider switch) without one is a malformed request, and a+ ;; supplied model must parse before the validation round-trip relies on its wire family.+ _ (when (= provider "azure")+ (when (and provider-changed? (nil? model))+ (throw (ex-info (tru "A model provider and deployment name are required to connect Azure.")+ {:status-code 400+ :api-error true+ :provider provider})))+ (when model+ (metabot.settings/validate-azure-model! (str provider "/" model) model)))+ ;; Reject writes to env-shadowed settings before verifying or persisting anything: guard every+ ;; credential setting a save would touch (see [[credential-setting-keys]]), plus the+ ;; provider/model setting whenever a provider/model write would happen.+ _ (when credentials+ (run! check-not-env-shadowed! (credential-setting-keys provider credentials)))+ _ (when model+ (check-not-env-shadowed! :llm-metabot-provider))+ ;; Azure connect validation needs the candidate model's wire family; credential-only+ ;; rotations on a connected Azure provider fall back to the saved model.+ validation-model (when (= provider "azure")+ (or model+ (when-not provider-changed?+ (provider-util/provider-and-model->model (metabot.settings/llm-metabot-provider)))));; The model listing validates the request credentials before anything is saved.- response (-> (settings-response provider credentials)+ response (-> (settings-response provider {:credentials credentials+ :model validation-model})throw-credentials-error!)](when credentials(save-credentials! provider credentials))diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex 1d47e322935..acf4b4a82ac 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -14,6 +14,7 @@[metabase.analytics.core :as analytics.core][metabase.api.common :as api][metabase.metabot.provider-util :as provider-util]+ [metabase.metabot.self.azure :as azure][metabase.metabot.self.bedrock :as bedrock][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core]@@ -30,6 +31,7 @@;; a `case` inside of function instead of a map so that with-redefs work well(case provider"anthropic" claude/claude+ "azure" azure/azure"bedrock" bedrock/bedrock"openai" openai/openai"openrouter" openrouter/openrouter@@ -40,6 +42,7 @@;; a `case` inside of function instead of a map so that with-redefs work well(case provider"anthropic" claude/list-models+ "azure" azure/list-models"bedrock" bedrock/list-models"openai" openai/list-models"openrouter" openrouter/list-modelsdiff --git a/src/metabase/metabot/self/azure.clj b/src/metabase/metabot/self/azure.cljnew file mode 100644index 00000000000..ed35340b3fc--- /dev/null+++ b/src/metabase/metabot/self/azure.clj@@ -0,0 +1,220 @@+(ns metabase.metabot.self.azure+ "Microsoft Azure LLM provider adapter.++ Talks to the OpenAI- and Anthropic-compatible \"v1\" surfaces of a customer's Azure resource+ (`https://<resource>.services.ai.azure.com/{openai|anthropic}`, or the `*.openai.azure.com`+ equivalent), authenticating with `Authorization: Bearer {llm-azure-api-key}`:++ - `POST {base-url}/v1/messages` — Anthropic Messages API, for `anthropic/*` models+ - `POST {base-url}/v1/responses` — OpenAI Responses API, for `openai/*` models++ Azure serves admin-named *deployments*, not a callable catalog, so the model string is+ `{family}/{deployment-name}`: the first segment selects the wire protocol and the rest is the+ deployment name sent as the body's `model`. These are the same protocols the direct+ Anthropic/OpenAI adapters speak, so this namespace reuses their request-body and chunks-xf fns,+ exactly like the Bedrock adapter. Only the v1 surfaces are supported (no classic+ deployment-scoped endpoints or Entra ID auth)."+ (:require+ [clojure.string :as str]+ [metabase.llm.settings :as llm]+ [metabase.metabot.provider-util :as provider-util]+ [metabase.metabot.self.claude :as claude]+ [metabase.metabot.self.core :as core]+ [metabase.metabot.self.debug :as debug]+ [metabase.metabot.self.openai :as openai]+ [metabase.metabot.settings :as metabot.settings]+ [metabase.util.i18n :refer [tru]]+ [metabase.util.json :as json]+ [metabase.util.malli :as mu]+ [metabase.util.o11y :refer [with-span]]))++(set! *warn-on-reflection* true)++(def ^:private anthropic-version "2023-06-01")++;;; --------------------------------------------- API family dispatch -------------------------------------------++(defn- model->family+ "Which wire-protocol family serves `model`, by its explicit first segment: `:anthropic` or `:openai`."+ [model]+ (cond+ (str/starts-with? (str model) "anthropic/") :anthropic+ (str/starts-with? (str model) "openai/") :openai+ :else+ (throw (ex-info (tru "Unsupported Azure model {0}. Only anthropic/* and openai/* models are supported." (pr-str model))+ {:api-error true+ :error-code :unsupported-model+ :model model}))))++(defn- model->deployment+ "The Azure deployment name carried by a `{family}/{deployment-name}` model string."+ [model]+ (second (str/split (str model) #"/" 2)))++;;; ------------------------------------------------ HTTP plumbing ----------------------------------------------++(defn- settings-credentials+ "Azure credentials from the `llm-azure-*` settings."+ []+ {:api-key (not-empty (llm/llm-azure-api-key))+ :base-url (not-empty (llm/llm-azure-api-base-url))})++(defn- missing-credentials-ex []+ (ex-info (tru "Azure credentials are not configured")+ {:api-error true+ :error-code :api-key-missing+ :status-code 403}))++(defn- ai-proxy-unsupported-ex []+ (ex-info (tru "AI proxy is not supported for Azure")+ {:api-error true+ :error-code :proxy-unsupported}))++(defn- ensure-credentials+ "Validate an Azure credentials map, falling back to [[settings-credentials]] when nil.+ Throws when the API key or base URL is missing."+ [credentials]+ (let [creds (or credentials (settings-credentials))]+ (when-not (metabot.settings/provider-credentials-complete? "azure" creds)+ (throw (missing-credentials-ex)))+ creds))++(defn- azure-error-msg+ "Canonical, status-specific Azure error message."+ [res]+ (let [status (long (:status res 0))]+ (case status+ 401 (tru "Azure rejected the API key for this resource")+ 403 (tru "Azure API key lacks permission for this resource or deployment")+ 404 (tru "Azure API endpoint or deployment was not found — check the base URL and deployment name")+ 429 (tru "Azure has rate limited us")+ 500 (tru "Azure is not working but not saying why")+ (tru "Azure API error (HTTP {0})" status))))++(defn- azure-request+ "Perform an HTTP request against the Azure resource's compatible surface.+ `credentials` is an optional `{:api-key ... :base-url ...}` map; when nil, the+ `llm-azure-*` settings are used. `headers` are extra headers (e.g. `anthropic-version`).+ `ai-proxy?` is accepted for parity with the other provider adapters but is not supported:+ throws when true. Auth is resolved through [[core/resolve-auth]]/[[core/request]] so proxy+ redirection is already wired up should Azure proxying ever be supported."+ [{:keys [method path body as headers credentials ai-proxy?]}]+ (when ai-proxy?+ (throw (ai-proxy-unsupported-ex)))+ (let [{:keys [api-key base-url]} (ensure-credentials credentials)+ auth (core/resolve-auth "azure" "Azure"+ {:url base-url+ :headers {"Authorization" (str "Bearer " api-key)}}+ ai-proxy?)]+ (core/request auth+ (cond-> {:method method+ :url path+ :headers headers}+ as (assoc :as as)+ body (-> (assoc :body body)+ (assoc-in [:headers "Content-Type"] "application/json"))))))++;;; ---------------------------------------------- Connect validation -------------------------------------------++(defn- validate-openai-surface!+ "Round-trip the `/openai` surface: `GET /v1/models` succeeds (with the regional catalog,+ which we discard) iff the key and base URL reach an authenticated OpenAI-compatible surface."+ [credentials ai-proxy?]+ (azure-request {:method :get :path "/v1/models" :as :json :credentials credentials :ai-proxy? ai-proxy?}))++(defn- validate-anthropic-surface!+ "Round-trip the `/anthropic` surface, which exposes no GET routes (they 404 with+ `api_not_supported` even when authenticated). `POST /v1/messages` with an empty body+ returns 400 `no_model_name` from the messages route itself — auth is checked before+ routing (bad keys 401, wrong paths 404) — so a 400 proves surface + auth without+ invoking a model."+ [credentials ai-proxy?]+ (try+ (azure-request {:method :post+ :path "/v1/messages"+ :body "{}"+ :headers {"anthropic-version" anthropic-version}+ :credentials credentials+ :ai-proxy? ai-proxy?})+ (catch Exception e+ (when-not (= 400 (:status (ex-data e)))+ (throw e)))))++(defn- configured-azure-model+ "The saved `{family}/{deployment}` model string when the configured Metabot provider is Azure."+ []+ (let [value (metabot.settings/llm-metabot-provider)]+ (when (= (provider-util/provider-and-model->provider value) "azure")+ (provider-util/provider-and-model->model value))))++(defn list-models+ "Validate Azure credentials with a model-free round trip and return an empty model list.++ There is never a dropdown to populate (Azure's listing returns the regional catalog, not the+ customer's deployments — deployment names are free text), so the round trip only proves the+ credentials reach an authenticated surface of the right family (see [[validate-openai-surface!]]+ and [[validate-anthropic-surface!]]); deployment existence is not validated and first fails at+ chat time with `DeploymentNotFound`.++ Opts: `:credentials` (`{:api-key ... :base-url ...}`), `:model` (the `{family}/{deployment}`+ string selecting which surface family to validate; defaults to the saved Azure model), and+ `:ai-proxy?`, which is not supported for Azure and throws when true."+ ([] (list-models {}))+ ([{:keys [credentials model ai-proxy?]}]+ (when-let [model (or (not-empty model) (configured-azure-model))]+ (try+ (case (model->family model)+ :anthropic (validate-anthropic-surface! credentials ai-proxy?)+ :openai (validate-openai-surface! credentials ai-proxy?))+ (catch Exception e+ (core/rethrow-api-error! "azure" azure-error-msg e))))+ {:models []}))++;;; --------------------------------------------------- Streaming -----------------------------------------------++(mu/defn azure-raw+ "Perform a streaming request to an Azure-hosted model deployment.+ `:ai-proxy?` is not supported for Azure and throws when true."+ [{:keys [model input tools ai-proxy?] :as opts} :- core/LLMRequestOpts]+ (let [family (model->family model)+ opts (assoc opts :model (model->deployment model))+ {:keys [path headers req]}+ (case family+ :anthropic {:path "/v1/messages"+ :headers {"anthropic-version" anthropic-version}+ :req (claude/claude-request-body opts)}+ :openai {:path "/v1/responses"+ :req (openai/openai-request-body opts)})]+ (with-span :info {:name :metabot.azure/request+ :model model+ :family family+ :msg-count (count input)+ :tool-count (count tools)}+ (try+ (let [response (azure-request {:method :post+ :path path+ :as :stream+ :headers headers+ :body (json/encode req)+ :ai-proxy? ai-proxy?})]+ (-> (core/sse-reducible (:body response))+ (debug/capture-stream {:provider "azure"+ :model model+ :url path+ :request req})))+ (catch Exception e+ (core/rethrow-api-error! "azure" azure-error-msg e))))))++(defn- model->aisdk-chunks-xf+ "The SSE->AISDK translating transducer for an Azure model string.+ Claude's for `anthropic/*` models, OpenAI's for `openai/*` models."+ [model]+ (case (model->family model)+ :anthropic (claude/claude->aisdk-chunks-xf)+ :openai (openai/openai->aisdk-chunks-xf)))++(defn azure+ "Call an Azure-hosted model deployment, return AISDK stream."+ [& [{:keys [model]} :as args]]+ (let [raw (apply azure-raw args)]+ (eduction (model->aisdk-chunks-xf model) raw)))diff --git a/src/metabase/metabot/settings.clj b/src/metabase/metabot/settings.cljindex b8abc64ff58..1e61a5a9807 100644--- a/src/metabase/metabot/settings.clj+++ b/src/metabase/metabot/settings.clj@@ -104,7 +104,7 @@(def ^:private direct-providers"Providers that can be used directly (not via the metabase/ proxy prefix)."- #{"anthropic" "bedrock" "openai" "openrouter"})+ #{"anthropic" "azure" "bedrock" "openai" "openrouter"})(def ^:private default-anthropic-llm-metabot-model"Default Anthropic model used for Metabot when no explicit model is selected."@@ -141,6 +141,26 @@"Set of supported LLM provider prefixes for the `llm-metabot-provider` setting."(conj direct-providers provider-util/metabase-provider-prefix))+(def azure-model-families+ "Wire-protocol families for models hosted on Azure: the first segment of the azure model+ string `{family}/{deployment-name}`, selecting the Anthropic or OpenAI wire protocol."+ #{"anthropic" "openai"})++(defn validate-azure-model!+ "Validate the model segment of an `azure/{family}/{deployment-name}` provider string:+ a supported wire family followed by a non-blank deployment name without slashes+ (Azure deployment names cannot contain `/`). Throws on invalid input."+ [value model]+ (let [[family deployment] (str/split (str model) #"/" 2)]+ (when-not (and (contains? azure-model-families family)+ (not (str/blank? deployment))+ (not (str/includes? deployment "/")))+ (throw (ex-info (tru "Invalid Azure model {0}. Expected format: azure/<family>/<deployment-name> where <family> is one of: {1}"+ (pr-str value)+ (str/join ", " (sort azure-model-families)))+ {:status-code 400+ :value value})))))+(defn- validate-direct-provider!"Validate that `value` is a `provider/model` string for one of the [[direct-providers]](i.e. *not* using the `metabase/` proxy prefix). Throws on invalid input."@@ -162,7 +182,9 @@(when (str/blank? model)(throw (ex-info (tru "Model name is required. Expected format: provider/model, e.g. \"anthropic/claude-haiku-4-5\""){:status-code 400- :value value})))))+ :value value})))+ (when (= provider "azure")+ (validate-azure-model! value model))))(defn- validate-metabase-managed-provider!"Validate that `value` is a `metabase/provider/model` string whose inner provider and@@ -248,12 +270,17 @@(defn configured-provider-credentials"Returns the configured credentials map for the given provider, or nil if unrecognized or unconfigured.- The shape of the map varies by provider: API-key providers return `{:api-key ...}`, while Bedrock returns- `:access-key-id`, `:secret-access-key`, `:session-token`, and `:region` from the `llm-bedrock-*` settings. Bedrock- counts as configured only when both the access key ID and secret access key are set."+ The shape of the map varies by provider: API-key providers return `{:api-key ...}`, Azure returns `:api-key` and+ `:base-url` from the `llm-azure-*` settings, and Bedrock returns `:access-key-id`, `:secret-access-key`,+ `:session-token`, and `:region` from the `llm-bedrock-*` settings. Azure counts as configured only when both the+ API key and base URL are set; Bedrock only when both the access key ID and secret access key are set."[provider](case provider"anthropic" (configured-api-key-credentials (llm.settings/llm-anthropic-api-key))+ "azure" (let [api-key (non-blank (llm.settings/llm-azure-api-key))+ base-url (non-blank (llm.settings/llm-azure-api-base-url))]+ (when (and api-key base-url)+ {:api-key api-key :base-url base-url}))"bedrock" (when (llm.settings/llm-bedrock-configured?){:access-key-id (non-blank (llm.settings/llm-bedrock-access-key-id)):secret-access-key (non-blank (llm.settings/llm-bedrock-secret-access-key))@@ -265,12 +292,15 @@(defn provider-credentials-complete?"Whether a credentials map carries everything `provider` needs to make requests: both the AWS access key ID and- secret access key for Bedrock, an `:api-key` for the other direct providers."+ secret access key for Bedrock, both the API key and base URL for Azure, an `:api-key` for the other direct+ providers."[provider credentials](boolean- (if (= provider "bedrock")- (and (non-blank (:access-key-id credentials))- (non-blank (:secret-access-key credentials)))+ (case provider+ "bedrock" (and (non-blank (:access-key-id credentials))+ (non-blank (:secret-access-key credentials)))+ "azure" (and (non-blank (:api-key credentials))+ (non-blank (:base-url credentials)))(non-blank (:api-key credentials)))))(defn- llm-provider-configured?diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.cljindex 6ef0346a209..28de905256d 100644--- a/src/metabase/metabot/tools/construct.clj+++ b/src/metabase/metabot/tools/construct.clj@@ -34,6 +34,57 @@[:map {:closed true}[:chart_type :string]])+(def ^:private construct-notebook-query-json-schema+ "Hand-authored JSON Schema for the `:query` argument, attached to the deliberately open,+ property-less malli `:map` via a `:json-schema` override. It does not participate in validation —+ it only replaces the schema we hand the LLM. Malli would otherwise emit an empty-`properties`+ object, which weaker models (e.g. gpt-4.1-mini) read as \"this object has no fields\" and answer+ with `{}`; the structured `:required`/`:properties` here stop that, and the prose carries the+ per-clause shape JSON Schema can't express well."+ {:type "object"+ :description (str "An MBQL 5 query as a JSON **object** (never a quoted string) matching "+ "`metabase.lib.schema/external-query`. The FIRST stage MUST contain exactly one of `source-table` "+ "(a portable FK `[\"<db-name>\", \"<schema-or-null>\", \"<table-name>\"]`) or `source-card` "+ "(an entity_id string) — the target database is inferred from it; there is no top-level "+ "`database` field. Every clause is `[\"<op>\", {<options>}, ...args]` with a mandatory "+ "(possibly empty) options map at position 1, and every field reference is `[\"field\", {}, "+ "[\"<db>\", \"<schema>\", \"<table>\", \"<field>\"]]`. Minimal example — count of orders by "+ "month: `{\"lib/type\": \"mbql/query\", \"stages\": [{\"lib/type\": \"mbql.stage/mbql\", "+ "\"source-table\": [\"Sample Database\", \"PUBLIC\", \"ORDERS\"], \"aggregation\": "+ "[[\"count\", {}]], \"breakout\": [[\"field\", {\"temporal-unit\": \"month\"}, "+ "[\"Sample Database\", \"PUBLIC\", \"ORDERS\", \"CREATED_AT\"]]]}]}`. Load the "+ "`construct-notebook-query-*` skills for the full operator catalog, joins, expressions, "+ "and multi-stage rules.")+ :required ["lib/type" "stages"]+ :properties {"lib/type" {:type "string"+ :const "mbql/query"+ :description "Must be the literal string `mbql/query`."}+ "stages" {:type "array"+ :minItems 1+ :description (str "Non-empty array of query stages. The FIRST stage must carry the "+ "source (`source-table` or `source-card`); later stages read from "+ "the previous one.")+ :items+ {:type "object"+ :properties {"lib/type" {:type "string" :const "mbql.stage/mbql"}+ "source-table" {:type "array"+ :minItems 3+ :maxItems 3+ :items {:type "string"}+ :description "Portable FK `[<db-name>, <schema-or-null>, <table-name>]`."}+ "source-card" {:type "string"+ :description "entity_id of a saved question/model used as the source."}+ ;; Each clause is itself a `["<op>", {opts}, ...args]` array; the inner+ ;; `items {}` (any) keeps the shape open while satisfying the API's+ ;; requirement that every `array` schema declare `items`.+ "aggregation" {:type "array" :items {:type "array" :items {}}}+ "breakout" {:type "array" :items {:type "array" :items {}}}+ "filters" {:type "array" :items {:type "array" :items {}}}+ "fields" {:type "array" :items {:type "array" :items {}}}+ "order-by" {:type "array" :items {:type "array" :items {}}}+ "joins" {:type "array" :items {:type "object"}}+ "expressions" {:type "object"}}}}}})+(def ^:private construct-notebook-query-args-schema"Args schema for `construct_notebook_query`.@@ -50,7 +101,9 @@omits `:source_entity` and `:referenced_entities` — the query body is self-describing."[:map {:closed true}[:reasoning {:optional true} :string]- [:query :map]+ ;; Validation stays a fully open, property-less `:map` (the repair layer fixes LLM shortcuts); the+ ;; `:json-schema` override only changes what the LLM sees. See [[construct-notebook-query-json-schema]].+ [:query [:map {:json-schema construct-notebook-query-json-schema}]][:visualization {:optional true} construct-visualization-schema][:title :string]])diff --git a/test/metabase/llm/settings_test.clj b/test/metabase/llm/settings_test.cljindex f0bd469faea..d1815c35d4f 100644--- a/test/metabase/llm/settings_test.clj+++ b/test/metabase/llm/settings_test.clj@@ -124,6 +124,48 @@(llm.settings/llm-bedrock-region! "")(is (= "us-east-1" (llm.settings/llm-bedrock-region)))))))+;;; ------------------------------------------- llm-azure setting Setter Tests -------------------------------------------++(deftest llm-azure-api-key-setter-test+ (testing "accepts an unprefixed Azure data-plane key and trims whitespace"+ (mt/with-temp-env-var-value! [mb-llm-azure-api-key nil]+ (mt/discard-setting-changes [llm-azure-api-key]+ (llm.settings/llm-azure-api-key! " 2QyICJz8sExampleDataPlaneKey ")+ (is (= "2QyICJz8sExampleDataPlaneKey" (llm.settings/llm-azure-api-key))))))+ (testing "empty/nil clears the setting"+ (mt/with-temp-env-var-value! [mb-llm-azure-api-key nil]+ (mt/discard-setting-changes [llm-azure-api-key]+ (llm.settings/llm-azure-api-key! "2QyICJz8sExampleDataPlaneKey")+ (llm.settings/llm-azure-api-key! "")+ (is (nil? (llm.settings/llm-azure-api-key)))))))++(deftest llm-azure-api-base-url-setter-test+ (testing "trims whitespace and trailing slashes"+ (mt/with-temp-env-var-value! [mb-llm-azure-api-base-url nil]+ (mt/discard-setting-changes [llm-azure-api-base-url]+ (llm.settings/llm-azure-api-base-url! " https://my-resource.services.ai.azure.com/openai/// ")+ (is (= "https://my-resource.services.ai.azure.com/openai"+ (llm.settings/llm-azure-api-base-url))))))+ (testing "is otherwise persisted exactly as entered, with no silent rewriting"+ (mt/with-temp-env-var-value! [mb-llm-azure-api-base-url nil]+ (mt/discard-setting-changes [llm-azure-api-base-url]+ (llm.settings/llm-azure-api-base-url! "https://my-resource.services.ai.azure.com/api/projects/my-project")+ (is (= "https://my-resource.services.ai.azure.com/api/projects/my-project"+ (llm.settings/llm-azure-api-base-url))))))+ (testing "blank clears the setting"+ (mt/with-temp-env-var-value! [mb-llm-azure-api-base-url nil]+ (mt/discard-setting-changes [llm-azure-api-base-url]+ (llm.settings/llm-azure-api-base-url! "https://my-resource.services.ai.azure.com/openai")+ (llm.settings/llm-azure-api-base-url! " ")+ (is (nil? (llm.settings/llm-azure-api-base-url)))))))++(deftest ^:parallel normalize-llm-base-url-test+ (is (= "https://x.example/openai" (llm.settings/normalize-llm-base-url " https://x.example/openai/ ")))+ (is (= "https://x.example" (llm.settings/normalize-llm-base-url "https://x.example///")))+ (is (nil? (llm.settings/normalize-llm-base-url " ")))+ (is (nil? (llm.settings/normalize-llm-base-url nil)))+ (is (nil? (llm.settings/normalize-llm-base-url "///"))))+;;; ------------------------------------------- llm-proxy-base-url Feature Guard Tests -------------------------------------------(deftest llm-proxy-base-url-feature-guard-testdiff --git a/test/metabase/metabot/api_test.clj b/test/metabase/metabot/api_test.cljindex 96f68724749..7b5703a3444 100644--- a/test/metabase/metabot/api_test.clj+++ b/test/metabase/metabot/api_test.clj@@ -590,6 +590,45 @@(mt/user-http-request :crowberto :get 200 "metabot/settings":provider "openai"))))))+(deftest settings-get-degrades-non-credential-provider-4xx-test+ (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-valid"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ ;; e.g. a base URL pointing at the wrong Azure surface;+ ;; rethrow-api-error! tags these with :status, not :status-code+ (throw (ex-info "OpenAI API error (HTTP 400) — Missing required query parameter: api-version"+ {:api-error true+ :status 400})))]+ (let [response (mt/user-http-request :crowberto :get 200 "metabot/settings"+ :provider "openai")]+ (is (= [] (:models response)))+ (is (re-find #"api-version" (:credentials-error response))+ "a non-401 provider 4xx (misconfigured surface) keeps GET /settings loadable and returns the provider message")))))++(deftest settings-put-rejects-env-shadowed-provider-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider "anthropic/claude-haiku-4-5"+ mb-llm-openai-api-key nil]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ (is false "should reject before verifying credentials"))]+ (let [response (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "openai"+ :model "gpt-5.1"+ :api-key "sk-new"})]+ (is (re-find #"MB_LLM_METABOT_PROVIDER" (:message response))+ "a provider/model write is rejected when the provider setting is env-controlled")))))++(deftest settings-put-allows-api-key-rotation-when-provider-env-set-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider "openai/gpt-5.1"+ mb-llm-openai-api-key nil]+ (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-old"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]+ (is (= {:api-key "sk-new"} credentials))+ {:models [{:id "gpt-5.1" :display_name "GPT-5.1"}]})]+ (mt/user-http-request :crowberto :put 200 "metabot/settings"+ {:provider "openai"+ :api-key "sk-new"})+ (is (= "sk-new" (llm.settings/llm-openai-api-key))+ "rotating the key for an env-set provider does not require a provider write and so is allowed")))))+(deftest settings-permissions-test(mt/user-http-request :rasta :get 403 "metabot/settings" :provider "anthropic")(mt/user-http-request :rasta :put 403 "metabot/settings"@@ -1246,6 +1285,18 @@(is (= "us-east-2" (llm.settings/llm-bedrock-region)))(is (= "test-token" (llm.settings/llm-bedrock-session-token)))))))+(deftest settings-put-rejects-env-shadowed-bedrock-credential-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-bedrock-secret-access-key "env-secret"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ (is false "should reject before verifying credentials"))]+ (let [response (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "bedrock"+ :credentials {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"}})]+ (is (re-find #"MB_LLM_BEDROCK_SECRET_ACCESS_KEY" (:message response))+ "a bedrock credentials write is rejected when one of its settings is env-controlled")))))+(deftest settings-put-bedrock-rejects-incomplete-credentials-test(mt/with-temp-env-var-value! [mb-llm-metabot-provider nilmb-llm-bedrock-access-key-id nil@@ -1467,3 +1518,162 @@(is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))(is (= "test-token" (llm.settings/llm-bedrock-session-token)))(is (= "us-east-2" (llm.settings/llm-bedrock-region))))))))++;;; ------------------------------------------------ Azure ------------------------------------------------++(deftest settings-put-saves-azure-credentials-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil+ llm.settings/llm-azure-api-base-url nil+ metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials model]}]+ (is (= "azure" provider))+ (is (= {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"}+ credentials)+ "validation runs against the normalized request credentials")+ (is (= "anthropic/claude-sonnet-4-5" model)+ "the candidate model selects the surface family to validate")+ (is (nil? (llm.settings/llm-azure-api-key))+ "validation should happen before saving the credentials")+ {:models []})]+ (testing "connecting azure saves the credentials and the composed provider/model value"+ (is (=? {:value "azure/anthropic/claude-sonnet-4-5"+ :models []}+ (mt/user-http-request :crowberto :put 200 "metabot/settings"+ {:provider "azure"+ :model "anthropic/claude-sonnet-4-5"+ :credentials {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic/"}}))))+ (is (= "azure-key" (llm.settings/llm-azure-api-key)))+ (testing "the trailing slash is trimmed before persisting"+ (is (= "https://my-resource.services.ai.azure.com/anthropic"+ (llm.settings/llm-azure-api-base-url))))))))++(deftest settings-put-azure-requires-model-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url nil]+ (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ (is false "should reject before verifying credentials"))]+ (testing "switching to azure without a model is rejected — there is no default deployment"+ (is (re-find #"model provider and deployment name are required"+ (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "azure"+ :credentials {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/openai"}}))))+ (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider))))))))++(deftest settings-put-azure-rejects-invalid-model-format-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url nil]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ (is false "should reject before verifying credentials"))]+ (testing "an unsupported wire family is rejected before the validation round-trip"+ (is (re-find #"Invalid Azure model"+ (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "azure"+ :model "gemini/some-deployment"+ :credentials {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/openai"}}))))))))++(deftest settings-put-azure-rejects-incomplete-credentials-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil+ llm.settings/llm-azure-api-base-url nil+ metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (testing "credentials missing the base URL fail before validation and nothing is saved"+ (is (=? {:message "Azure credentials are incomplete."+ :missing-keys ["base-url"]}+ (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "azure"+ :model "openai/gpt-4.1-mini"+ :credentials {:api-key "azure-key"}})))+ (is (nil? (llm.settings/llm-azure-api-key)))+ (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))++(deftest settings-put-azure-key-rotation-uses-saved-base-url-and-model-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "old-key"+ llm.settings/llm-azure-api-base-url "https://my-resource.services.ai.azure.com/anthropic"+ metabot.settings/llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials model]}]+ (is (= {:api-key "new-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"}+ credentials)+ "the new key is layered over the saved base URL")+ (is (= "anthropic/claude-sonnet-4-5" model)+ "a credentials-only rotation validates against the saved model's family")+ {:models []})]+ (is (=? {:value "azure/anthropic/claude-sonnet-4-5"}+ (mt/user-http-request :crowberto :put 200 "metabot/settings"+ {:provider "azure"+ :credentials {:api-key "new-key"}})))+ (is (= "new-key" (llm.settings/llm-azure-api-key)))+ (is (= "https://my-resource.services.ai.azure.com/anthropic"+ (llm.settings/llm-azure-api-base-url)))))))++(deftest settings-put-nil-azure-credentials-clears-saved-credentials-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url "https://my-resource.services.ai.azure.com/openai"+ metabot.settings/llm-metabot-provider "azure/openai/gpt-4.1-mini"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]+ (is false (str "unexpected list-models call: " provider)))]+ (testing "an explicit nil credentials clears both saved settings without validating against them"+ (is (=? {:value "azure/openai/gpt-4.1-mini"+ :models []}+ (mt/user-http-request :crowberto :put 200 "metabot/settings"+ {:provider "azure"+ :credentials nil})))+ (is (nil? (llm.settings/llm-azure-api-key)))+ (is (nil? (llm.settings/llm-azure-api-base-url))))))))++(deftest settings-put-rejects-env-shadowed-azure-settings-test+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key "env-key"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ (is false "should reject before verifying credentials"))]+ (testing "an azure credentials write is rejected when an azure setting is env-controlled"+ (is (re-find #"MB_LLM_AZURE_API_KEY"+ (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "azure"+ :model "openai/gpt-4.1-mini"+ :credentials {:api-key "new-key"+ :base-url "https://my-resource.services.ai.azure.com/openai"}})))))))+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil+ mb-llm-azure-api-key nil+ mb-llm-azure-api-base-url "https://env.services.ai.azure.com/openai"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]+ (is false "should reject before verifying credentials"))]+ (testing "the base URL env var is guarded the same way"+ (is (re-find #"MB_LLM_AZURE_API_BASE_URL"+ (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"+ {:provider "azure"+ :model "openai/gpt-4.1-mini"+ :credentials {:api-key "new-key"}}))))))))++(deftest settings-get-azure-returns-empty-models-test+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url "https://my-resource.services.ai.azure.com/anthropic"+ metabot.settings/llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]+ (is (= "azure" provider))+ (is (= {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"}+ credentials))+ {:models []})]+ (testing "azure never returns models — deployment names are free text, not a dropdown"+ (is (= {:value "azure/anthropic/claude-sonnet-4-5"+ :models []}+ (mt/user-http-request :crowberto :get 200 "metabot/settings" :provider "azure")))))))diff --git a/test/metabase/metabot/self/azure_test.clj b/test/metabase/metabot/self/azure_test.cljnew file mode 100644index 00000000000..8c3e66cf7f7--- /dev/null+++ b/test/metabase/metabot/self/azure_test.clj@@ -0,0 +1,254 @@+(ns metabase.metabot.self.azure-test+ (:require+ [clj-http.client :as http]+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase.llm.settings :as llm.settings]+ [metabase.metabot.self.azure :as azure]+ [metabase.metabot.self.core :as self.core]+ [metabase.metabot.self.debug :as debug]+ [metabase.metabot.settings :as metabot.settings]+ [metabase.test :as mt]+ [metabase.util.json :as json]))++(set! *warn-on-reflection* true)++(def ^:private test-base-url "https://my-resource.services.ai.azure.com/openai")++;;; ──────────────────────────────────────────────────────────────────+;;; Connect validation (list-models)+;;; ──────────────────────────────────────────────────────────────────++(deftest list-models-missing-credentials-test+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil+ llm.settings/llm-azure-api-base-url nil]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Azure credentials are not configured"+ (azure/list-models {:model "openai/gpt-4.1-mini"})))))++(deftest list-models-requires-both-credential-fields-test+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url nil]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Azure credentials are not configured"+ (azure/list-models {:model "openai/gpt-4.1-mini"})))))++(deftest list-models-openai-family-round-trips-the-catalog-endpoint-test+ (testing "validation for the openai family is a GET /v1/models against the candidate credentials"+ (let [captured (atom nil)]+ (with-redefs [http/request (fn [req] (reset! captured req) {:status 200 :body {:data []}})]+ (is (= {:models []}+ (azure/list-models {:credentials {:api-key "override-key" :base-url test-base-url}+ :model "openai/gpt-4.1-mini"})))+ (is (=? {:method :get+ :url (str test-base-url "/v1/models")+ :headers {"Authorization" "Bearer override-key"}}+ @captured))))))++(deftest list-models-anthropic-family-accepts-the-model-free-400-test+ (testing "the anthropic surface has no GET routes; an empty-body POST /v1/messages 400s from+ the messages route itself, which proves surface + auth without invoking a model"+ (let [captured (atom nil)]+ (with-redefs [http/request (fn [req]+ (reset! captured req)+ (throw (ex-info "HTTP error" {:status 400+ :body "{\"error\":{\"code\":\"no_model_name\"}}"})))]+ (is (= {:models []}+ (azure/list-models {:credentials {:api-key "override-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"}+ :model "anthropic/claude-sonnet-4-5"})))+ (is (=? {:method :post+ :url "https://my-resource.services.ai.azure.com/anthropic/v1/messages"+ :body "{}"+ :headers {"Authorization" "Bearer override-key"+ "anthropic-version" "2023-06-01"}}+ @captured))))))++(deftest list-models-anthropic-family-still-rejects-bad-keys-test+ (testing "auth is checked before routing on the anthropic surface, so a bad key 401s and is translated"+ (with-redefs [http/request (fn [_] (throw (ex-info "HTTP error" {:status 401 :body "{}"})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Azure rejected the API key for this resource"+ (azure/list-models {:credentials {:api-key "bogus"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"}+ :model "anthropic/claude-sonnet-4-5"}))))))++(deftest list-models-falls-back-to-the-configured-azure-model-test+ (testing "without a candidate model, validation uses the saved Azure model's family"+ (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "azure/openai/gpt-4.1-mini"+ llm.settings/llm-azure-api-key "saved-key"+ llm.settings/llm-azure-api-base-url test-base-url]+ (let [captured (atom nil)]+ (with-redefs [http/request (fn [req] (reset! captured req) {:status 200 :body {:data []}})]+ (is (= {:models []} (azure/list-models)))+ (is (=? {:method :get+ :url (str test-base-url "/v1/models")}+ @captured)))))))++(deftest list-models-skips-validation-without-any-model-test+ (testing "with no candidate model and a non-Azure provider configured, there is no surface to probe"+ (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (= {:models []} (azure/list-models)))))))++;;; ──────────────────────────────────────────────────────────────────+;;; API family dispatch and request construction+;;; ──────────────────────────────────────────────────────────────────++(defn- captured-raw-request!+ "Run `azure-raw` with HTTP stubbed out and return the clj-http request map it would send."+ ([opts] (captured-raw-request! test-base-url opts))+ ([base-url opts]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url base-url]+ (with-redefs [self.core/sse-reducible identity+ debug/capture-stream (fn [r _] r)+ http/request (fn [req] {:body req})]+ (azure/azure-raw opts)))))++(deftest anthropic-family-dispatches-to-messages-api-test+ (let [req (captured-raw-request! "https://my-resource.services.ai.azure.com/anthropic"+ {:model "anthropic/my-claude-deployment"+ :system "be brief"+ :input [{:role :user :content "hi"}]})+ body (json/decode+kw (:body req))]+ (is (= "https://my-resource.services.ai.azure.com/anthropic/v1/messages" (:url req)))+ (is (=? {"Authorization" "Bearer azure-key"+ "anthropic-version" "2023-06-01"+ "Content-Type" "application/json"}+ (:headers req)))+ (testing "the body is the shared Anthropic Messages request with the deployment name as the model"+ (is (=? {:model "my-claude-deployment"+ :stream true+ :system [{:type "text" :text "be brief" :cache_control {:type "ephemeral"}}]+ :messages [{:role "user" :content [{:type "text" :text "hi"}]}]}+ body)))))++(deftest openai-family-dispatches-to-responses-api-test+ (let [req (captured-raw-request! {:model "openai/gpt-5-deployment"+ :system "be brief"+ :input [{:role :user :content "hi"}]+ :temperature 0.3+ :max-tokens 128})+ body (json/decode+kw (:body req))]+ (is (= (str test-base-url "/v1/responses") (:url req)))+ (is (=? {"Authorization" "Bearer azure-key"+ "Content-Type" "application/json"}+ (:headers req)))+ (is (=? {:model "gpt-5-deployment"+ :stream true+ :instructions "be brief"+ :max_output_tokens 128+ :input [{:role "user" :content "hi"}]}+ body))+ (testing "temperature is omitted when the deployment is named after a reasoning model"+ (is (not (contains? body :temperature))))))++(deftest unsupported-family-throws-test+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Unsupported Azure model \"gemini/some-deployment\". Only anthropic/\* and openai/\* models are supported."+ (captured-raw-request! {:model "gemini/some-deployment"+ :input [{:role :user :content "hi"}]}))))++;;; ──────────────────────────────────────────────────────────────────+;;; AI proxy (unsupported)+;;; ──────────────────────────────────────────────────────────────────++(deftest list-models-ai-proxy-unsupported-test+ (testing "ai-proxy? throws before credentials are even consulted"+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil+ llm.settings/llm-azure-api-base-url nil]+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"AI proxy is not supported for Azure"+ (azure/list-models {:model "openai/gpt-4.1-mini" :ai-proxy? true})))))))++(deftest azure-raw-ai-proxy-unsupported-test+ (testing "ai-proxy? throws before credentials are even consulted"+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil+ llm.settings/llm-azure-api-base-url nil]+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"AI proxy is not supported for Azure"+ (azure/azure-raw {:model "openai/gpt-4.1-mini"+ :input [{:role :user :content "hi"}]+ :ai-proxy? true})))))))++;;; ──────────────────────────────────────────────────────────────────+;;; Stream translation (xf selection by model family)+;;; ──────────────────────────────────────────────────────────────────++(defn- sse-response-for+ "A stubbed clj-http response whose body is an SSE stream of `events`."+ [events]+ {:status 200+ :body (java.io.ByteArrayInputStream.+ (.getBytes (str/join (map #(str "data: " (json/encode %) "\n\n") events)) "UTF-8"))})++(defn- aisdk-parts-for! [model events]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url test-base-url]+ (with-redefs [debug/capture-stream (fn [r _] r)+ http/request (fn [_] (sse-response-for events))]+ (into [] (self.core/aisdk-xf)+ (azure/azure {:model model+ :input [{:role :user :content "hi"}]})))))++(deftest anthropic-family-uses-claude-stream-translation-test+ (is (=? [{:type :start :id "msg_1"}+ {:type :text :text "pong"}+ {:type :usage :usage {:promptTokens 3 :completionTokens 2}}]+ (aisdk-parts-for!+ "anthropic/my-claude-deployment"+ [{:type "message_start" :message {:id "msg_1" :model "claude-sonnet-4-5" :usage {:input_tokens 3}}}+ {:type "content_block_start" :index 0 :content_block {:type "text"}}+ {:type "content_block_delta" :index 0 :delta {:type "text_delta" :text "pong"}}+ {:type "content_block_stop" :index 0}+ {:type "message_delta" :delta {:stop_reason "end_turn"} :usage {:input_tokens 3 :output_tokens 2}}+ {:type "message_stop"}]))))++(deftest openai-family-uses-openai-stream-translation-test+ (is (=? [{:type :start :id "resp_1"}+ {:type :text :text "pong"}+ {:type :usage :usage {:promptTokens 3 :completionTokens 2}}]+ (aisdk-parts-for!+ "openai/gpt-4.1-mini"+ [{:type "response.created" :response {:id "resp_1" :model "gpt-4.1-mini"}}+ {:type "response.output_item.added" :item {:type "message" :id "item_1"} :id "item_1"}+ {:type "response.output_text.delta" :delta "pong" :id "item_1"}+ {:type "response.output_item.done" :item {:type "message" :id "item_1"} :id "item_1"}+ {:type "response.completed"+ :response {:id "resp_1" :usage {:input_tokens 3 :output_tokens 2}}}]))))++;;; ──────────────────────────────────────────────────────────────────+;;; Error translation+;;; ──────────────────────────────────────────────────────────────────++(defn- list-models-error-message!+ "The translated message `list-models` throws when the HTTP layer fails with `status`/`body`."+ [status body]+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url test-base-url]+ (with-redefs [http/request (fn [_] (throw (ex-info "HTTP error" {:status status+ :headers {"content-type" "application/json"}+ :body body})))]+ (try+ (azure/list-models {:model "openai/gpt-4.1-mini"})+ (catch Exception e+ (ex-message e))))))++(deftest auth-error-is-translated-without-body-preview-test+ (testing "401s get the canonical message; the upstream body is withheld (may carry auth detail)"+ (is (= "Azure rejected the API key for this resource"+ (list-models-error-message! 401 "{\"error\":{\"message\":\"secret account detail\"}}")))))++(deftest not-found-error-is-translated-with-body-preview-test+ (testing "a wrong-surface base URL produces the canonical 404 message plus the provider's detail"+ (is (= "Azure API endpoint or deployment was not found — check the base URL and deployment name — Resource not found"+ (list-models-error-message! 404 "{\"error\":{\"message\":\"Resource not found\"}}")))))diff --git a/test/metabase/metabot/settings_test.clj b/test/metabase/metabot/settings_test.cljindex f5b6477f517..2a31279225e 100644--- a/test/metabase/metabot/settings_test.clj+++ b/test/metabase/metabot/settings_test.clj@@ -277,6 +277,70 @@{:secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"})))(is (false? (metabot.settings/provider-credentials-complete? "bedrock" nil)))))+(deftest validate-metabot-provider-accepts-valid-direct-azure-test+ (testing "accepts azure provider strings with a wire family and deployment name"+ (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]+ (is (= "azure/anthropic/claude-sonnet-4-5" (metabot.settings/llm-metabot-provider))))+ (mt/with-temporary-setting-values [llm-metabot-provider "azure/openai/my-gpt-deployment"]+ (is (= "azure/openai/my-gpt-deployment" (metabot.settings/llm-metabot-provider))))))++(deftest validate-metabot-provider-rejects-invalid-azure-models-test+ (testing "rejects an unsupported wire family"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/gemini/some-deployment"))))+ (testing "rejects a missing deployment name"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/anthropic/")))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/anthropic"))))+ (testing "rejects a deployment name containing a slash (Azure deployment names cannot contain /)"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/anthropic/a/b")))))++(deftest metabot-configured-with-azure-credentials-test+ (testing "returns true when azure has both the API key and base URL set"+ (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"+ llm-azure-api-key "azure-key"+ llm-azure-api-base-url "https://my-resource.services.ai.azure.com/anthropic"]+ (is (true? (metabot.settings/llm-metabot-configured?))))))++(deftest metabot-configured-with-partial-azure-credentials-test+ (testing "returns false when azure has an API key but no base URL"+ (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"+ llm-azure-api-key "azure-key"+ llm-azure-api-base-url nil]+ (is (false? (metabot.settings/llm-metabot-configured?))))))++(deftest configured-provider-credentials-azure-fully-configured-test+ (testing "returns the api-key/base-url credentials map when azure is fully configured"+ (mt/with-temporary-setting-values [llm-azure-api-key "azure-key"+ llm-azure-api-base-url "https://my-resource.services.ai.azure.com/openai"]+ (is (= {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/openai"}+ (metabot.settings/configured-provider-credentials "azure"))))))++(deftest configured-provider-credentials-azure-partial-credentials-test+ (testing "returns nil when azure has a base URL but no API key"+ (mt/with-temporary-setting-values [llm-azure-api-key nil+ llm-azure-api-base-url "https://my-resource.services.ai.azure.com/openai"]+ (is (nil? (metabot.settings/configured-provider-credentials "azure"))))))++(deftest provider-credentials-complete?-azure-test+ (testing "azure credentials are complete only with both a non-blank API key and base URL"+ (is (true? (metabot.settings/provider-credentials-complete?+ "azure"+ {:api-key "azure-key" :base-url "https://my-resource.services.ai.azure.com/openai"})))+ (is (false? (metabot.settings/provider-credentials-complete? "azure" {:api-key "azure-key"})))+ (is (false? (metabot.settings/provider-credentials-complete? "azure" {:api-key "azure-key" :base-url " "})))+ (is (false? (metabot.settings/provider-credentials-complete?+ "azure"+ {:base-url "https://my-resource.services.ai.azure.com/openai"})))+ (is (false? (metabot.settings/provider-credentials-complete? "azure" nil)))))+(deftest provider-credentials-complete?-api-key-provider-test(testing "API-key provider credentials are complete only with a non-blank :api-key"(is (true? (metabot.settings/provider-credentials-complete? "openai" {:api-key "sk-valid"})))