:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): validate azure fields and show field errors when editing a key (#120381)
Carlos Marchal444e24d9f709
diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.tsindex d6f6d94babb9..d35463ae23ff 100644--- a/frontend/src/initKea.ts+++ b/frontend/src/initKea.ts@@ -115,6 +115,12 @@ const ERROR_FILTER_ALLOW_LIST_BY_LOGIC_PATH: Record<string, string> = {loadReportDiff: 'scenes.inbox.logics.inboxReportDetailLogic',loadTraceSpans: 'products.tracing.frontend.components.TraceDialog.traceDialogLogic', // The dialog shows its own retryloadPreview: 'products.metrics.frontend.components.metricVariableEditorLogic', // The variable editor shows the error under the preview+ // llmProviderKeysLogic toasts each of these failures itself, or shows a rejected field under its input+ loadProviderKeys: 'products.ai_observability.settings.llmProviderKeysLogic',+ loadEvaluationConfig: 'products.ai_observability.settings.llmProviderKeysLogic',+ createProviderKey: 'products.ai_observability.settings.llmProviderKeysLogic',+ updateProviderKey: 'products.ai_observability.settings.llmProviderKeysLogic',+ deleteProviderKey: 'products.ai_observability.settings.llmProviderKeysLogic',}// A keyed logic's path ends with its key, so match the owner path as a prefix.diff --git a/products/ai_observability/frontend/settings/LLMProviderKeysSettings.tsx b/products/ai_observability/frontend/settings/LLMProviderKeysSettings.tsxindex 110c4be30cde..b8e98ba85ec0 100644--- a/products/ai_observability/frontend/settings/LLMProviderKeysSettings.tsx+++ b/products/ai_observability/frontend/settings/LLMProviderKeysSettings.tsx@@ -28,12 +28,14 @@ import {DEFAULT_SYSTEM_ONE_BASE_URL,DEFAULT_SYSTEM_ONE_MODEL,DependentConfigsResponse,+ EditKeyField,KeyValidationResult,LLMProvider,LLMProviderKey,LLMProviderKeyState,LLM_PROVIDER_LABELS,UpdateLLMProviderKeyPayload,+ isFoundryKey,llmProviderKeysLogic,normalizeSystemOneBaseUrlForComparison,sortProviderKeys,@@ -122,6 +124,35 @@ function endpointErrorFieldFromResult(result: KeyValidationResult | null | undefreturn result.error_field === 'azure_endpoint' || result.error_field === 'base_url' ? 'endpoint' : 'key'}+function TextField({+ label,+ value,+ onChange,+ placeholder,+ error,+}: {+ label: string+ value: string+ onChange: (value: string) => void+ placeholder?: string+ error: string | null+}): JSX.Element {+ return (+ <div>+ <label className="text-sm font-medium">{label}</label>+ <LemonInput+ value={value}+ onChange={onChange}+ placeholder={placeholder}+ className="mt-1"+ fullWidth+ status={error ? 'danger' : undefined}+ />+ {error && <p className="text-xs text-danger mt-1">{error}</p>}+ </div>+ )+}+function FoundryEndpointField({value,onChange,@@ -237,10 +268,10 @@ function AddKeyModal({ restrictionReason }: { restrictionReason: string | null }const keyValidated = preValidationResult?.state === 'ok'const isSystemOne = provider === 'system_one'const isValid =- name.length > 0 &&+ name.trim().length > 0 &&(isSystemOne ? systemOneBaseUrl.length > 0 && systemOneModel.length > 0 : apiKey.length > 0) &&- (!isAzure || azureEndpoint.length > 0) &&- (!isOpenAICompatible || baseUrl.length > 0)+ (!isAzure || (azureEndpoint.trim().length > 0 && apiVersion.trim().length > 0)) &&+ (!isOpenAICompatible || baseUrl.trim().length > 0)const validationFailed = !!preValidationResult && preValidationResult.state !== 'ok'const endpointErrorField =hasEndpointField && validationFailed ? endpointErrorFieldFromResult(preValidationResult) : null@@ -540,17 +571,18 @@ function EditKeyModal({keyToEdit: LLMProviderKeyrestrictionReason: string | null}): JSX.Element {- const { providerKeysLoading, preValidationResult, preValidationResultLoading } = useValues(llmProviderKeysLogic)+ const { providerKeysLoading, preValidationResult, preValidationResultLoading, editKeyFieldError } =+ useValues(llmProviderKeysLogic)const { systemOneBaseUrl, systemOneModel } = useValues(llmProviderKeysLogic)- const { setEditingKey, updateProviderKey, preValidateKey, clearPreValidation } = useActions(llmProviderKeysLogic)+ const { setEditingKey, updateProviderKey, preValidateKey, clearPreValidation, setEditKeyFieldError } =+ useActions(llmProviderKeysLogic)const isAzureEdit = keyToEdit.provider === 'azure_openai'const isSystemOne = keyToEdit.provider === 'system_one'const endpointChanged =normalizeSystemOneBaseUrlForComparison(systemOneBaseUrl) !==normalizeSystemOneBaseUrlForComparison(keyToEdit.base_url_display ?? DEFAULT_SYSTEM_ONE_BASE_URL)const isOpenAICompatibleEdit = keyToEdit.provider === 'openai_compatible'- // An Anthropic key keeps the API it was created for, so only Foundry keys show the endpoint.- const isFoundryEdit = keyToEdit.provider === 'anthropic' && !!keyToEdit.azure_endpoint_display+ const isFoundryEdit = isFoundryKey(keyToEdit)const [name, setName] = useState(keyToEdit.name)const [apiKey, setApiKey] = useState('')@@ -558,6 +590,19 @@ function EditKeyModal({const [apiVersion, setApiVersion] = useState(keyToEdit.api_version_display ?? DEFAULT_AZURE_API_VERSION)const [baseUrl, setBaseUrl] = useState(keyToEdit.base_url_display ?? '')+ const fieldErrorFor = (field: EditKeyField): string | null =>+ editKeyFieldError?.field === field ? editKeyFieldError.message : null++ // A rejected field keeps its error until the user edits that field again.+ const withFieldErrorCleared =+ (field: EditKeyField, onChange: (value: string) => void): ((value: string) => void) =>+ (value) => {+ onChange(value)+ if (editKeyFieldError?.field === field) {+ setEditKeyFieldError(null)+ }+ }+const handleClose = (): void => {setEditingKey(null)clearPreValidation()@@ -631,8 +676,10 @@ function EditKeyModal({}const keyValidated = isSystemOne || apiKey.length === 0 || preValidationResult?.state === 'ok'- const baseUrlMissing = isOpenAICompatibleEdit && baseUrl.length === 0- const foundryEndpointMissing = isFoundryEdit && azureEndpoint.length === 0+ const baseUrlMissing = isOpenAICompatibleEdit && baseUrl.trim().length === 0+ const foundryEndpointMissing = isFoundryEdit && azureEndpoint.trim().length === 0+ const azureEndpointMissing = isAzureEdit && azureEndpoint.trim().length === 0+ const apiVersionMissing = isAzureEdit && apiVersion.trim().length === 0// The backend rejects a base URL change that arrives without a key, so the key can't stay// masked here: re-entering it is what proves the caller already has it.const baseUrlChanged = isOpenAICompatibleEdit && !baseUrlMissing && baseUrl !== (keyToEdit.base_url_display ?? '')@@ -647,13 +694,17 @@ function EditKeyModal({? 'Enter the API key again to change the Microsoft Foundry endpoint': nullconst missingFieldReason =- name.length === 0+ name.trim().length === 0? 'Enter a name': baseUrlMissing? 'Enter a base URL': foundryEndpointMissing? 'Enter a Microsoft Foundry endpoint'- : null+ : azureEndpointMissing+ ? 'Enter an Azure endpoint'+ : apiVersionMissing+ ? 'Enter an API version'+ : nullconst disabledReason = restrictionReason ?? missingFieldReason ?? apiKeyRequiredReasonconst isValid =keyValidated && !disabledReason && (!isSystemOne || (systemOneBaseUrl.length > 0 && systemOneModel.length > 0))@@ -661,6 +712,19 @@ function EditKeyModal({const hasEndpointField = isAzureEdit || isFoundryEdit || isOpenAICompatibleEditconst endpointErrorField =hasEndpointField && validationFailed ? endpointErrorFieldFromResult(preValidationResult) : null+ const endpointValidationError = endpointErrorField === 'endpoint' ? preValidationResult?.error_message : null+ const azureEndpointError =+ fieldErrorFor('azure_endpoint') ??+ (endpointErrorField === 'endpoint' ? endpointValidationError || 'Invalid Azure endpoint' : null)+ const foundryEndpointError =+ fieldErrorFor('azure_endpoint') ??+ (endpointErrorField === 'endpoint' ? endpointValidationError || 'Invalid Microsoft Foundry endpoint' : null)+ const baseUrlError =+ fieldErrorFor('base_url') ??+ (endpointErrorField === 'endpoint' ? endpointValidationError || 'Invalid base URL' : null)+ const apiVersionError = fieldErrorFor('api_version')+ const nameError = fieldErrorFor('name')+ const apiKeyError = fieldErrorFor('api_key')return (<LemonModal@@ -694,74 +758,51 @@ function EditKeyModal({</div>{isAzureEdit && (<>- <div>- <label className="text-sm font-medium">Azure endpoint</label>- <LemonInput- value={azureEndpoint}- onChange={setAzureEndpoint}- placeholder="https://my-resource.openai.azure.com/"- className="mt-1"- fullWidth- status={endpointErrorField === 'endpoint' ? 'danger' : undefined}- />- {endpointErrorField === 'endpoint' && (- <p className="text-xs text-danger mt-1">- {preValidationResult?.error_message || 'Invalid Azure endpoint'}- </p>- )}- </div>- <div>- <label className="text-sm font-medium">API version</label>- <LemonInput- value={apiVersion}- onChange={setApiVersion}- placeholder={DEFAULT_AZURE_API_VERSION}- className="mt-1"- fullWidth- />- </div>+ <TextField+ label="Azure endpoint"+ value={azureEndpoint}+ onChange={withFieldErrorCleared('azure_endpoint', setAzureEndpoint)}+ placeholder="https://my-resource.openai.azure.com/"+ error={azureEndpointError}+ />+ <TextField+ label="API version"+ value={apiVersion}+ onChange={withFieldErrorCleared('api_version', setApiVersion)}+ placeholder={DEFAULT_AZURE_API_VERSION}+ error={apiVersionError}+ /></>)}{isFoundryEdit && (<FoundryEndpointFieldoptional={false}value={azureEndpoint}- onChange={handleEndpointChange(setAzureEndpoint)}- errorMessage={- endpointErrorField === 'endpoint'- ? preValidationResult?.error_message || 'Invalid Microsoft Foundry endpoint'- : null- }+ onChange={withFieldErrorCleared('azure_endpoint', handleEndpointChange(setAzureEndpoint))}+ errorMessage={foundryEndpointError}/>)}{isSystemOne && <SystemOneConnectionFields />}{isOpenAICompatibleEdit && (- <div>- <label className="text-sm font-medium">Base URL</label>- <LemonInput- value={baseUrl}- onChange={handleEndpointChange(setBaseUrl)}- placeholder="https://api.example.com/v1"- className="mt-1"- fullWidth- status={endpointErrorField === 'endpoint' ? 'danger' : undefined}- />- {endpointErrorField === 'endpoint' && (- <p className="text-xs text-danger mt-1">- {preValidationResult?.error_message || 'Invalid base URL'}- </p>- )}- </div>+ <TextField+ label="Base URL"+ value={baseUrl}+ onChange={withFieldErrorCleared('base_url', handleEndpointChange(setBaseUrl))}+ placeholder="https://api.example.com/v1"+ error={baseUrlError}+ />)}- <div>- <label className="text-sm font-medium">Name</label>- <LemonInput value={name} onChange={setName} className="mt-1" fullWidth />- </div>+ <TextField+ label="Name"+ value={name}+ onChange={withFieldErrorCleared('name', setName)}+ error={nameError}+ /><div><label className="text-sm font-medium">API key</label><LemonInputvalue={apiKey}- onChange={handleApiKeyChange}+ onChange={withFieldErrorCleared('api_key', handleApiKeyChange)}onBlur={handleApiKeyBlur}placeholder={isSystemOne && endpointChanged@@ -773,12 +814,14 @@ function EditKeyModal({className="mt-1"fullWidthstatus={- validationFailed && (!hasEndpointField || endpointErrorField === 'key')+ apiKeyError || (validationFailed && (!hasEndpointField || endpointErrorField === 'key'))? 'danger': undefined}/>- {apiKey.length > 0 ? (+ {apiKeyError ? (+ <p className="text-xs text-danger mt-1">{apiKeyError}</p>+ ) : apiKey.length > 0 ? (<KeyValidationStatusresult={preValidationResult}isValidating={preValidationResultLoading}diff --git a/products/ai_observability/frontend/settings/llmProviderKeysLogic.test.ts b/products/ai_observability/frontend/settings/llmProviderKeysLogic.test.tsindex 61e6ca217ee4..87727dc1a4bd 100644--- a/products/ai_observability/frontend/settings/llmProviderKeysLogic.test.ts+++ b/products/ai_observability/frontend/settings/llmProviderKeysLogic.test.ts@@ -1,4 +1,37 @@-import { normalizeLLMProvider, normalizeSystemOneBaseUrlForComparison, toLLMProvider } from './llmProviderKeysLogic'+import { expectLogic } from 'kea-test-utils'++import { ApiError } from 'lib/api'+import { lemonToast } from 'lib/lemon-ui/LemonToast'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import {+ LLMProvider,+ LLMProviderKey,+ getEditKeyFieldError,+ llmProviderKeysLogic,+ normalizeLLMProvider,+ normalizeSystemOneBaseUrlForComparison,+ toLLMProvider,+} from './llmProviderKeysLogic'++function makeKey(provider: LLMProvider, azureEndpoint: string | null = null): LLMProviderKey {+ return {+ id: 'key-1',+ provider,+ name: 'Key',+ state: 'ok',+ error_message: null,+ api_key_masked: 'sk-...1234',+ azure_endpoint_display: azureEndpoint,+ api_version_display: null,+ base_url_display: null,+ created_at: '2026-01-01T00:00:00Z',+ created_by: null,+ last_used_at: null,+ }+}describe('llmProviderKeysLogic', () => {it.each([@@ -42,4 +75,82 @@ describe('llmProviderKeysLogic', () => {])('treats %s as the same endpoint as %s', (input, expected) => {expect(normalizeSystemOneBaseUrlForComparison(input)).toBe(expected)})++ it.each([+ ['an Azure endpoint', true, makeKey('azure_openai'), 400, 'azure_endpoint'],+ ['an Azure API version', true, makeKey('azure_openai'), 400, 'api_version'],+ [+ 'a Foundry endpoint',+ true,+ makeKey('anthropic', 'https://example.services.ai.azure.com/'),+ 400,+ 'azure_endpoint',+ ],+ ['an OpenAI-compatible base URL', true, makeKey('openai_compatible'), 400, 'base_url'],+ ['a name', true, makeKey('openai'), 400, 'name'],+ ['a System One base URL', false, makeKey('system_one'), 400, 'base_url'],+ ['an Azure endpoint on a non-Azure key', false, makeKey('openai'), 400, 'azure_endpoint'],+ ['an Azure endpoint with a server error', false, makeKey('azure_openai'), 500, 'azure_endpoint'],+ ['a 400 without a field', false, makeKey('azure_openai'), 400, null],+ ])('attributes %s to an edit modal input: %s', (_, inline, key, status, attr) => {+ const errorObject = new ApiError(undefined, status, undefined, { attr, detail: 'This field may not be blank.' })+ expect(getEditKeyFieldError(key, errorObject)).toEqual(+ inline ? { field: attr, message: 'This field may not be blank.' } : null+ )+ })++ describe('saving an edited key', () => {+ const azureKey = makeKey('azure_openai', 'https://example.openai.azure.com/')+ let logic: ReturnType<typeof llmProviderKeysLogic.build>+ let patchResponse: [number, Record<string, unknown>]++ beforeEach(() => {+ useMocks({+ get: {+ '/api/environments/:teamId/llm_analytics/provider_keys/': { results: [azureKey] },+ '/api/environments/:teamId/llm_analytics/evaluation_config/': { active_provider_key: null },+ },+ patch: {+ '/api/environments/:teamId/llm_analytics/provider_keys/:id/': () => patchResponse,+ },+ })+ initKeaTests()+ logic = llmProviderKeysLogic()+ logic.mount()+ })++ afterEach(() => {+ logic.unmount()+ jest.restoreAllMocks()+ })++ it.each([+ [+ 'shows a rejected Azure endpoint under its input without a toast',+ 'azure_endpoint',+ 'This field may not be blank.',+ { field: 'azure_endpoint', message: 'This field may not be blank.' },+ [],+ ],+ [+ 'toasts a rejected field that has no input exactly once',+ 'provider',+ "A key's provider cannot change. Create a new key instead.",+ null,+ [["Failed to update API key: A key's provider cannot change. Create a new key instead."]],+ ],+ ])('%s', async (_, attr, detail, expectedFieldError, expectedToasts) => {+ patchResponse = [400, { type: 'validation_error', code: 'invalid', detail, attr }]+ const toast = jest.spyOn(lemonToast, 'error').mockReturnValue('')+ logic.actions.setEditingKey(azureKey)++ await expectLogic(logic, () => {+ logic.actions.updateProviderKey({ id: azureKey.id, payload: { azure_endpoint: '' } })+ })+ .toDispatchActions(['updateProviderKeyFailure'])+ .toFinishAllListeners()+ expect(logic.values.editKeyFieldError).toEqual(expectedFieldError)+ expect(toast.mock.calls).toEqual(expectedToasts)+ })+ })})diff --git a/products/ai_observability/frontend/settings/llmProviderKeysLogic.ts b/products/ai_observability/frontend/settings/llmProviderKeysLogic.tsindex 41081b905eb5..314d4d92184f 100644--- a/products/ai_observability/frontend/settings/llmProviderKeysLogic.ts+++ b/products/ai_observability/frontend/settings/llmProviderKeysLogic.ts@@ -184,6 +184,44 @@ export interface KeyValidationResult {error_field?: string | null}+/** Serializer fields that the edit modal renders an input for. */+export type EditKeyField = 'name' | 'api_key' | 'azure_endpoint' | 'api_version' | 'base_url'++export interface ProviderKeyFieldError {+ field: EditKeyField+ message: string+}++// An Anthropic key keeps the API it was created for, so only Foundry keys show the endpoint.+export function isFoundryKey(key: LLMProviderKey): boolean {+ return key.provider === 'anthropic' && !!key.azure_endpoint_display+}++function editKeyFieldsFor(key: LLMProviderKey): EditKeyField[] {+ if (key.provider === 'azure_openai') {+ return ['name', 'api_key', 'azure_endpoint', 'api_version']+ }+ if (isFoundryKey(key)) {+ return ['name', 'api_key', 'azure_endpoint']+ }+ if (key.provider === 'openai_compatible') {+ return ['name', 'api_key', 'base_url']+ }+ return ['name', 'api_key']+}++/**+ * Attributes a validation 400 from saving an edited key to the edit modal input that shows it.+ * Returns null when the modal has no input for the rejected field, so the caller toasts instead.+ */+export function getEditKeyFieldError(key: LLMProviderKey, errorObject: unknown): ProviderKeyFieldError | null {+ if (!(errorObject instanceof ApiError) || errorObject.status !== 400 || !errorObject.detail) {+ return null+ }+ const field = editKeyFieldsFor(key).find((candidate) => candidate === errorObject.attr)+ return field ? { field, message: errorObject.detail } : null+}+export interface DependentEvaluation {id: stringname: string@@ -206,6 +244,7 @@ export interface llmProviderKeysLogicValues {activeProviderKey: LLMProviderKey | null | undefineddependentConfigs: DependentConfigsResponse | nulldependentConfigsLoading: boolean+ editKeyFieldError: ProviderKeyFieldError | nulleditingKey: LLMProviderKey | nullevaluationConfig: EvaluationConfig | nullevaluationConfigLoading: boolean@@ -370,6 +409,9 @@ export interface llmProviderKeysLogicActions {base_url?: string}}+ setEditKeyFieldError: (fieldError: ProviderKeyFieldError | null) => {+ fieldError: ProviderKeyFieldError | null+ }setEditingKey: (key: LLMProviderKey | null) => {key: LLMProviderKey | null}@@ -456,6 +498,7 @@ export const llmProviderKeysLogic = kea<llmProviderKeysLogicType>([clearPreValidation: true,setNewKeyModalOpen: (open: boolean) => ({ open }),setEditingKey: (key: LLMProviderKey | null) => ({ key }),+ setEditKeyFieldError: (fieldError: ProviderKeyFieldError | null) => ({ fieldError }),setKeyToDelete: (key: LLMProviderKey | null) => ({ key }),confirmDelete: (replacementKeyId?: string) => ({ replacementKeyId }),}),@@ -489,6 +532,14 @@ export const llmProviderKeysLogic = kea<llmProviderKeysLogicType>([setEditingKey: (_, { key }) => key,},],+ editKeyFieldError: [+ null as ProviderKeyFieldError | null,+ {+ setEditKeyFieldError: (_, { fieldError }) => fieldError,+ updateProviderKey: () => null,+ setEditingKey: () => null,+ },+ ],validatingKeyId: [null as string | null,{@@ -717,7 +768,12 @@ export const llmProviderKeysLogic = kea<llmProviderKeysLogicType>([createProviderKeyFailure: ({ error }) => {lemonToast.error(`Failed to create API key: ${error || 'Unknown error'}`)},- updateProviderKeyFailure: ({ error }) => {+ updateProviderKeyFailure: ({ error, errorObject }) => {+ const fieldError = values.editingKey ? getEditKeyFieldError(values.editingKey, errorObject) : null+ if (fieldError) {+ actions.setEditKeyFieldError(fieldError)+ return+ }lemonToast.error(`Failed to update API key: ${error || 'Unknown error'}`)},deleteProviderKeyFailure: ({ error }) => {