{"record":{"id":"b662d3526e44b724","repo":"linshenkx/prompt-optimizer","slug":"cloudflare-model-search-returned-an-unexpected-res","errorCode":null,"errorMessage":"Cloudflare model search returned an unexpected response format","messagePattern":"Cloudflare model search returned an unexpected response format","errorType":"exception","errorClass":"APIError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/llm/adapters/cloudflare-adapter.ts","lineNumber":80,"sourceCode":"  public async getModelsAsync(config: TextModelConfig): Promise<TextModel[]> {\n    const accountId = this.getAccountId(config);\n    const baseURL = this.resolveCloudflareManagementBaseURL(config.connectionConfig.baseURL, accountId);\n    const url = `${baseURL}/models/search?task=${encodeURIComponent('Text Generation')}&hide_experimental=true&per_page=100`;\n\n    const response = await fetch(url, {\n      method: 'GET',\n      headers: {\n        Authorization: `Bearer ${config.connectionConfig.apiKey || ''}`\n      }\n    });\n\n    if (!response.ok) {\n      throw new APIError(`Cloudflare model search failed: ${await this.getErrorMessage(response)}`);\n    }\n\n    const data = await response.json();\n    if (!data?.success || !Array.isArray(data?.result)) {\n      throw new APIError('Cloudflare model search returned an unexpected response format');\n    }\n\n    const models = data.result\n      .filter((model: CloudflareModelSearchResult) => {\n        return model?.task?.name === 'Text Generation' && typeof model?.name === 'string' && !!model.name.trim();\n      })\n      .map((model: CloudflareModelSearchResult) => this.mapDynamicModel(model));\n\n    return models.length > 0 ? models : this.getModels();\n  }\n\n  public getModels(): TextModel[] {\n    return CLOUDFLARE_STATIC_MODELS.map((definition) => {\n      const baseModel = this.buildDefaultModel(definition.id);\n\n      return {\n        ...baseModel,\n        name: definition.name,","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/llm/adapters/cloudflare-adapter.ts#L62-L98","documentation":"Cloudflare's model search endpoint returned HTTP 200 but the JSON body failed the shape check: it must have success===true and result as an array. The adapter treats this as an unexpected contract violation rather than trying to read fields from it.","triggerScenarios":"Cloudflare changes/versions the search API response schema, an intermediate proxy rewrites the response, or a paginated/different endpoint is accidentally targeted via a custom base URL.","commonSituations":"Custom resolveCloudflareApiBaseURL overrides pointing at the wrong path, gateway mangling responses, upstream API evolution.","solutions":["curl the model search URL manually and inspect the payload for { success, result }","Remove custom base URL overrides so the adapter uses the default Cloudflare endpoint","Upgrade the package if Cloudflare changed the schema and a fix shipped","Fall back to a static list of Cloudflare models in the UI when this format error occurs"],"exampleFix":"// before\nconst models = await adapter.getModelsAsync()\n\n// after\nlet models\ntry { models = await adapter.getModelsAsync() }\ncatch (e) {\n  if (e instanceof APIError && e.message.includes('unexpected response format')) {\n    models = DEFAULT_CLOUDFLARE_MODELS\n  } else throw e\n}","handlingStrategy":"fallback","validationCode":"null","typeGuard":"function isCloudflareSearchResult(d: unknown): d is { success: true; result: unknown[] } {\n  return !!(d as any)?.success && Array.isArray((d as any).result)\n}","tryCatchPattern":"try { models = await adapter.getModelsAsync() }\ncatch (e) {\n  if (e instanceof APIError && e.message.includes('unexpected response format')) models = FALLBACK_CF_MODELS\n  else throw e\n}","preventionTips":["Avoid overriding the Cloudflare base URL/path","Pin package versions against known-good Cloudflare API behavior","Test model listing in CI against the real endpoint"],"tags":["cloudflare","response-format","schema","model-list"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}