abhigyanpatwari/GitNexus · error · Error

GLM API key is required but was not provided

Error message

GLM API key is required but was not provided

What it means

Thrown by createChatModel() in the 'glm' provider branch when glmConfig.apiKey is falsy or whitespace-only. GLM (Zhipu AI) is accessed through a ChatOpenAI wrapper pointed at https://api.z.ai/api/coding/paas/v4 (overridable via baseUrl); the key is required at construction time. Fails fast to avoid a 401 from the Z.AI API on first request.

Source

Thrown at gitnexus-web/src/core/llm/agent.ts:311

      return new ChatAnthropic({
        anthropicApiKey: minimaxConfig.apiKey,
        model: minimaxConfig.model,
        ...(temperature !== undefined ? { temperature } : {}),
        maxTokens: minimaxConfig.maxTokens ?? 8192,
        streaming: true,
        ...(thinking ? { thinking } : {}),
        clientOptions: {
          baseURL: minimaxConfig.baseUrl ?? MINIMAX_ANTHROPIC_BASE_URLS.global_en,
        },
      });
    }

    case 'glm': {
      const glmConfig = config as GLMConfig;

      if (!glmConfig.apiKey || glmConfig.apiKey.trim() === '') {
        throw new Error('GLM API key is required but was not provided');
      }

      return new ChatOpenAI({
        apiKey: glmConfig.apiKey,
        modelName: glmConfig.model,
        temperature: glmConfig.temperature ?? 0.1,
        maxTokens: glmConfig.maxTokens,
        configuration: {
          apiKey: glmConfig.apiKey,
          baseURL: glmConfig.baseUrl ?? 'https://api.z.ai/api/coding/paas/v4',
        },
        streaming: true,
      });
    }

    case 'deepseek': {
      const deepseekConfig = config as DeepSeekConfig;

View on GitHub (pinned to d540b00184)

Solutions

  1. Obtain a key from api.z.ai (Zhipu AI) and enter it in provider settings
  2. Ensure the key is non-empty and non-whitespace
  3. If using a custom GLM-compatible endpoint, set baseUrl accordingly
  4. Recreate the agent after setting the key

Example fix

// before
createChatModel({ provider: 'glm', apiKey: '', model: 'glm-4.6' }); // throws

// after
createChatModel({ provider: 'glm', apiKey: userKey, model: 'glm-4.6' });
Defensive patterns

Strategy: validation

Validate before calling

function hasGlmKey(cfg: { apiKey?: string }): boolean {
  return typeof cfg.apiKey === 'string' && cfg.apiKey.trim().length > 0;
}
if (cfg.provider === 'glm' && !hasGlmKey(cfg)) {
  throw new Error('Configure a GLM/Z.AI API key (from api.z.ai)');
}

Type guard

function isGlmConfigReady(cfg: unknown): cfg is { provider: 'glm'; apiKey: string } {
  return typeof cfg === 'object' && cfg !== null
    && (cfg as any).provider === 'glm'
    && typeof (cfg as any).apiKey === 'string'
    && (cfg as any).apiKey.trim().length > 0;
}

Try / catch

try {
  createChatModel({ provider: 'glm', apiKey, model });
} catch (e) {
  if (e instanceof Error && /GLM API key is required/.test(e.message)) {
    promptUserForKey('glm');
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling createChatModel({ provider: 'glm', apiKey: undefined | '' | ' ', model: 'glm-4.6', ... }).

Common situations: The GLM/Z.AI key wasn't set in provider settings; the env var is unset in the deployed environment; switching to glm without a z.ai account.

Related errors


AI-assisted analysis of abhigyanpatwari/GitNexus@d540b00184 (2026-08-12). Data as JSON: /api/errors/498366846b342d48. Report an issue: GitHub.