abhigyanpatwari/GitNexus · error · Error
OpenAI API key is required but was not provided
Error message
OpenAI API key is required but was not provided
What it means
Thrown by createChatModel() in the 'openai' provider branch when config.apiKey is falsy or whitespace-only. The GitNexus web agent requires a non-empty OpenAI API key to instantiate ChatOpenAI; this guard fails fast at model construction rather than letting LangChain surface a cryptic 401 on the first request.
Source
Thrown at gitnexus-web/src/core/llm/agent.ts:179
When generating diagrams:
- NO special characters in node labels: quotes, (), /, &, <, >
- Wrap labels with spaces in quotes: A["My Label"]
- Use simple IDs: A, B, C or auth, db, api
- Flowchart: graph TD or graph LR (not flowchart)
- Keep diagrams focused — 5-10 nodes max
- Always test mentally: would this parse?
BAD: A[User's Data] --> B(Process & Save)
GOOD: A["User Data"] --> B["Process and Save"]
`;
export const createChatModel = (config: ProviderConfig): BaseChatModel => {
switch (config.provider) {
case 'openai': {
const openaiConfig = config as OpenAIConfig;
if (!openaiConfig.apiKey || openaiConfig.apiKey.trim() === '') {
throw new Error('OpenAI API key is required but was not provided');
}
return new ChatOpenAI({
apiKey: openaiConfig.apiKey,
modelName: openaiConfig.model,
temperature: openaiConfig.temperature ?? 0.1,
maxTokens: openaiConfig.maxTokens,
configuration: {
apiKey: openaiConfig.apiKey,
...(openaiConfig.baseUrl ? { baseURL: openaiConfig.baseUrl } : {}),
},
streaming: true,
});
}
case 'azure-openai': {
const azureConfig = config as AzureOpenAIConfig;
return new AzureChatOpenAI({View on GitHub (pinned to d540b00184)
Solutions
- Set the OpenAI API key in the provider settings UI (or the backing env var) and reload
- Ensure the key is non-empty and non-whitespace — the guard uses .trim()
- If developing locally, restart `npm run dev` after changing .env so Vite picks up the new value
- Switch to a different provider (e.g. ollama, which needs no key) if you don't have an OpenAI key
Example fix
// before — key missing or empty
createChatModel({ provider: 'openai', apiKey: '', model: 'gpt-4o' }); // throws
// after — supply a non-empty key
createChatModel({ provider: 'openai', apiKey: process.env.OPENAI_API_KEY!, model: 'gpt-4o' }); Defensive patterns
Strategy: validation
Validate before calling
function hasOpenAIKey(cfg: { apiKey?: string }): boolean {
return typeof cfg.apiKey === 'string' && cfg.apiKey.trim().length > 0;
}
if (cfg.provider === 'openai' && !hasOpenAIKey(cfg)) {
throw new Error('Configure an OpenAI API key before starting the agent');
} Type guard
function isOpenAIConfigReady(cfg: unknown): cfg is { provider: 'openai'; apiKey: string } {
return typeof cfg === 'object' && cfg !== null
&& (cfg as any).provider === 'openai'
&& typeof (cfg as any).apiKey === 'string'
&& (cfg as any).apiKey.trim().length > 0;
} Try / catch
try {
createChatModel({ provider: 'openai', apiKey, model });
} catch (e) {
if (e instanceof Error && /OpenAI API key is required/.test(e.message)) {
promptUserForKey('openai');
return;
}
throw e;
} Prevention
- Read the key from a single source (env or settings) and validate non-empty before agent construction
- Restart the Vite dev server after changing .env so the new value is picked up
- Treat whitespace-only keys as missing (the guard uses .trim())
- Provide a no-key fallback provider (ollama) for users without an OpenAI account
When it happens
Trigger: Calling createChatModel({ provider: 'openai', apiKey: '', ... }) or omitting apiKey entirely (undefined). Also fires when apiKey contains only whitespace, since the check uses .trim() === ''.
Common situations: The OPENAI_API_KEY env var wasn't set when the web app loaded; the key was cleared from settings but the provider stayed 'openai'; a .env change that didn't restart the Vite dev server; deploying without provisioning the secret.
Related errors
- OpenRouter API key is required but was not provided
- MiniMax API key is required but was not provided
- GLM API key is required but was not provided
- DeepSeek API key is required but was not provided
- Unsupported provider: ${(config as any).provider}
AI-assisted analysis of abhigyanpatwari/GitNexus@d540b00184 (2026-08-12).
Data as JSON: /api/errors/d1541397aefac8e5.
Report an issue: GitHub.