linshenkx/prompt-optimizer · error · ImageError
API_KEY_REQUIRED
API_KEY_REQUIRED
Error message
API_KEY_REQUIRED
What it means
The SiliconFlow adapter requires an `apiKey` in its connection config. After the base adapter validation passes, the SiliconFlow-specific check throws API_KEY_REQUIRED when `connectionConfig.apiKey` is falsy.
Source
Thrown at packages/core/src/services/image/adapters/siliconflow.ts:242
image2image: true,
multiImage: false
},
parameterDefinitions: this.getParameterDefinitions(model.id),
defaultParameterValues: this.getDefaultParameterValues(model.id)
})
}
})
return Array.from(allModelsMap.values())
}
protected validateConnectionConfig(connectionConfig: Record<string, any>): void {
// 基础验证
super.validateConnectionConfig(connectionConfig)
// SiliconFlow 特定验证
if (!connectionConfig.apiKey) {
throw new ImageError(IMAGE_ERROR_CODES.API_KEY_REQUIRED, undefined, { providerName: 'SiliconFlow' })
}
}
protected getTestImageRequest(testType: 'text2image' | 'image2image'): Omit<ImageRequest, 'configId'> {
if (testType === 'text2image') {
return {
prompt: 'a flower',
count: 1
}
}
if (testType === 'image2image') {
return {
prompt: 'make it red',
count: 1,
inputImage: {
b64: AbstractImageProviderAdapter.TEST_IMAGE_BASE64.split(',')[1], // 去掉data:前缀
mimeType: 'image/png'View on GitHub (pinned to 3e677b1d9f)
Solutions
- Add a valid SiliconFlow API key to the connection config: { apiKey: 'sk-...' }
- Verify the config key is spelled exactly 'apiKey' (case-sensitive)
- Confirm the settings UI actually persists the key before triggering validation
- Generate a key at SiliconFlow dashboard if you don't have one
Example fix
// before
await adapter.validateConnectionConfig({ baseUrl: 'https://api.siliconflow.cn' })
// after
await adapter.validateConnectionConfig({
baseUrl: 'https://api.siliconflow.cn',
apiKey: process.env.SILICONFLOW_API_KEY
}) Defensive patterns
Strategy: validation
Validate before calling
if (!connectionConfig?.apiKey || typeof connectionConfig.apiKey !== 'string') {
throw new Error('SiliconFlow connection requires connectionConfig.apiKey')
}
await adapter.validateConnectionConfig(connectionConfig) Type guard
function hasSiliconFlowConfig(c: unknown): c is { apiKey: string; baseUrl?: string } {
return typeof c === 'object' && c !== null && typeof (c as any).apiKey === 'string' && (c as any).apiKey.length > 0
} Try / catch
try {
await adapter.validateConnectionConfig(cfg)
} catch (e) {
if (e instanceof ImageError && e.code === IMAGE_ERROR_CODES.API_KEY_REQUIRED) {
promptUserForApiKey(); return
}
throw e
} Prevention
- Mark apiKey as required in connection form schemas
- Validate config shape before save, not only before API calls
- Never commit configs; inject keys from env/secret store at runtime
When it happens
Trigger: Calling validateConnectionConfig (directly or via getModelsAsync / connection save flow) with a connectionConfig that is missing apiKey, sets it to empty string, or passes undefined because the key was never prompted for.
Common situations: User creates a SiliconFlow image connection but leaves the API key field blank; the key is stored under a different config key name; env-var-based key injection not set in CI; UI silently dropping the apiKey field on edit.
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
- UNSUPPORTED_TEST_TYPE
- CONTEXT_ERROR_CODES.STORAGE_ERROR
- CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR
- DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE
- Data must be an object
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/8d2cdbf576a61051.
Report an issue: GitHub.