linshenkx/prompt-optimizer · error · RequestConfigError

Cloudflare requires accountId in connection config

Error message

Cloudflare requires accountId in connection config

What it means

Cloudflare's Workers AI REST API requires the account ID in the URL path. getAccountId reads connectionConfig.accountId, trims it, and throws RequestConfigError if empty — every Cloudflare call needs it to build https://api.cloudflare.com/client/v4/accounts/{accountId}/ai/...

Source

Thrown at packages/core/src/services/llm/adapters/cloudflare-adapter.ts:131

  protected createOpenAIInstance(config: TextModelConfig, isStream: boolean = false): OpenAI {
    const accountId = this.getAccountId(config);

    const normalizedConfig: TextModelConfig = {
      ...config,
      connectionConfig: {
        ...config.connectionConfig,
        baseURL: this.resolveCloudflareApiBaseURL(config.connectionConfig.baseURL, accountId)
      }
    };

    return super.createOpenAIInstance(normalizedConfig, isStream);
  }

  private getAccountId(config: TextModelConfig): string {
    const accountId = String(config.connectionConfig.accountId || '').trim();
    if (!accountId) {
      throw new RequestConfigError('Cloudflare requires accountId in connection config');
    }

    return accountId;
  }

  private resolveCloudflareApiBaseURL(rawBaseURL: string | undefined, accountId: string): string {
    const providerBaseURL = this.getProvider().defaultBaseURL;
    let baseURL = (rawBaseURL || providerBaseURL).trim();

    if (baseURL.endsWith('/chat/completions')) {
      baseURL = baseURL.slice(0, -'/chat/completions'.length);
    }

    const encodedAccountId = encodeURIComponent(accountId);
    const trimmed = baseURL.replace(/\/$/, '');

    if (trimmed.includes('{accountId}')) {
      return trimmed.replace('{accountId}', encodedAccountId);

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Add accountId to connectionConfig: it's on the Cloudflare dashboard right side / URL after login (dash.cloudflare.com/<accountId>)
  2. Confirm the field name is exactly accountId (camelCase) inside connectionConfig
  3. Ensure the value survives normalization (no empty string, not undefined)
  4. Pre-validate config before creating the adapter (see validation code)

Example fix

// before
const config = { connectionConfig: { apiKey: CF_TOKEN } }

// after
const config = {
  connectionConfig: { apiKey: CF_TOKEN, accountId: CF_ACCOUNT_ID }
}
Defensive patterns

Strategy: validation

Validate before calling

function validateCloudflareConfig(cfg: TextModelConfig) {
  const id = String(cfg.connectionConfig?.accountId || '').trim()
  if (!id) throw new Error('Missing Cloudflare accountId')
  if (!/^\w{10,}$/.test(id)) throw new Error('accountId looks invalid')
}

Type guard

function hasAccountId(cfg: TextModelConfig): boolean {
  return typeof cfg.connectionConfig?.accountId === 'string' && cfg.connectionConfig.accountId.trim().length > 0
}

Try / catch

null

Prevention

When it happens

Trigger: Creating a Cloudflare provider config without accountId in connectionConfig; passing only apiKey; accountId being whitespace or a non-string that String()s to ''.

Common situations: Copying an API token but forgetting the account ID, config forms that only ask for the key, YAML/JSON config where the field is misnamed (account_id vs accountId).

Understand the failure class

Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.

Related errors


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