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

  1. Add a valid SiliconFlow API key to the connection config: { apiKey: 'sk-...' }
  2. Verify the config key is spelled exactly 'apiKey' (case-sensitive)
  3. Confirm the settings UI actually persists the key before triggering validation
  4. 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

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


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/8d2cdbf576a61051. Report an issue: GitHub.