gitbutlerapp/gitbutler · error · Error

Please provide a valid API key for your selected AI service

Error message

Please provide a valid API key for your selected AI service

What it means

Thrown by the AI settings credential check when validateConfiguration() is false for a user-supplied OpenAI/Anthropic key configuration (usingGitButlerAPI() false). The stored API key for the selected service is missing, empty, or rejected as malformed before any network test runs.

Source

Thrown at apps/desktop/src/components/settings/AiCredentialCheck.svelte:79

		try {
			// Get current model kind
			modelKind = await aiService.getModelKind();
			debugInfo = `Model kind: ${modelKind}`;

			// Check if using GitButler API
			isUsingButlerAPI = await aiService.usingGitButlerAPI();
			debugInfo += `, Using GB API: ${isUsingButlerAPI}`;

			// Check if configuration is valid
			const isConfigValid = await aiService.validateConfiguration();
			debugInfo += `, Config valid: ${isConfigValid}`;

			if (!isConfigValid) {
				if (modelKind === ModelKind.OpenAI || modelKind === ModelKind.Anthropic) {
					if (isUsingButlerAPI && !userService.user) {
						throw new Error("Please sign in to use GitButler's AI API");
					} else {
						throw new Error("Please provide a valid API key for your selected AI service");
					}
				} else if (modelKind === ModelKind.Ollama) {
					// Get Ollama configuration for more detailed error
					const endpoint = await aiService.getOllamaEndpoint();
					const model = await aiService.getOllamaModelName();
					throw new Error(
						`Please check Ollama configuration: endpoint=${endpoint}, model=${model}`,
					);
				} else if (modelKind === ModelKind.LMStudio) {
					// Get LM Studio configuration for more detailed error
					const endpoint = await aiService.getLMStudioEndpoint();
					throw new Error(`Please check LM Studio configuration: endpoint=${endpoint}`);
				}
			}

			debugInfo += `, Testing commit message generation`;

			// Set a timeout to fail if the streaming doesn't start or complete

View on GitHub (pinned to caf1f223d3)

Solutions

  1. Re-enter the API key in Settings, paste without surrounding quotes or whitespace, and save before testing.
  2. Confirm the key is active on the provider dashboard (OpenAI/Anthropic) and has not been revoked.
  3. Verify the key with a direct provider call, e.g. curl https://api.openai.com/v1/models -H "Authorization: Bearer $KEY".
Defensive patterns

Strategy: validation

Validate before calling

const key = await aiService.getApiKey(modelKind);
if (!key || key.trim().length < 20) {
  error = "Enter the API key for your selected AI service first";
  return;
}
// run the credential check

Type guard

function looksLikeProviderKey(kind: ModelKind, key: string | undefined): boolean {
  if (!key) return false;
  if (kind === ModelKind.Anthropic) return key.startsWith("sk-ant-");
  if (kind === ModelKind.OpenAI) return key.startsWith("sk-");
  return key.trim().length > 0;
}

Try / catch

try {
  await runCheck();
} catch (e) {
  error = e instanceof Error ? e.message : "Unknown error occurred";
}

Prevention

When it happens

Trigger: Selecting OpenAI or Anthropic in AI settings and running the check with no key saved, a whitespace-only key, or a key string that fails the provider's format validation.

Common situations: Key was pasted with trailing newline/quotes; key field cleared by a settings reset; switched provider (OpenAI→Anthropic) without entering the new provider's key; key revoked/deleted on the provider side and validation now fails.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@caf1f223d3 (2026-08-20). Data as JSON: /api/errors/bf79dd3fb6ff7724. Report an issue: GitHub.