{"record":{"id":"e43650a792215db1","repo":"janhq/jan","slug":"failed-to-determine-embedding-context-size-e-in","errorCode":null,"errorMessage":"Failed to determine embedding context size: ${e instanceof Error ? e.message : String(e)}","messagePattern":"Failed to determine embedding context size: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"extensions/vector-db-extension/src/index.ts","lineNumber":211,"sourceCode":"    for (const chunk of chunks) {\n      out.push(...(await this.splitChunkToFit(chunk, budget, llm)))\n    }\n    return out\n  }\n\n  /**\n   * A rejected probe/count means the embedding engine is unhealthy (e.g. the\n   * embedding model failed to load). Skipping verification here would let\n   * oversized chunks through and surface later as a confusing HTTP 400\n   * (exceed_context_size_error), so fail ingestion with the real cause.\n   */\n  private async probeEmbeddingContextSize(\n    llm: EmbeddingEngine\n  ): Promise<number | undefined> {\n    try {\n      return await llm.getEmbeddingContextSize()\n    } catch (e) {\n      throw new Error(\n        `Failed to determine embedding context size: ${e instanceof Error ? e.message : String(e)}`\n      )\n    }\n  }\n\n  private async splitChunkToFit(\n    text: string,\n    budget: number,\n    llm: EmbeddingEngine\n  ): Promise<string[]> {\n    if (!text) return []\n    let count: number\n    try {\n      ;[count] = await llm.countEmbeddingTokens([text])\n    } catch (e) {\n      throw new Error(\n        `Failed to count embedding tokens: ${e instanceof Error ? e.message : String(e)}`\n      )","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/janhq/jan/blob/7205d770c1e097c3daf35a911176410e93bc5564/extensions/vector-db-extension/src/index.ts#L193-L229","documentation":"probeEmbeddingContextSize wraps any failure from the embedding engine's getEmbeddingContextSize() call into a single descriptive error. The extension needs to know the model's max context size to size chunks; if the engine (local model, remote API, or plugin) fails to answer, this error propagates with the underlying message appended.","triggerScenarios":"Calling ctxSize() (which invokes probeEmbeddingContextSize) when the configured embedding engine throws from getEmbeddingContextSize() — e.g. the model/backend is unavailable, misconfigured, or the engine implementation rejects the call.","commonSituations":"Embedding provider API key missing or invalid, local embedding model failed to load, network outage when querying a remote embedding service, or an engine that does not implement getEmbeddingContextSize.","solutions":["Check the underlying message in the error text to identify the root cause (auth, network, model load).","Verify the embedding engine/provider configuration (API key, base URL, model name) is valid and reachable.","Confirm the configured EmbeddingEngine implements getEmbeddingContextSize() and that its model files/plugins are installed.","Test the embedding backend independently (e.g. a direct embed request) to confirm it is healthy before ingesting."],"exampleFix":"// before\nconst size = await ctxSize(llm) // throws 'Failed to determine embedding context size: ...'\n// after\nlet size: number | undefined\ntry {\n  size = await ctxSize(llm)\n} catch (e) {\n  console.error('embedding engine unreachable:', e)\n  size = undefined // fall back to a conservative default chunk size\n}","handlingStrategy":"try-catch","validationCode":"if (typeof llm.getEmbeddingContextSize !== 'function') {\n  throw new Error('embedding engine does not implement getEmbeddingContextSize')\n}","typeGuard":"function hasCtxSize(e: unknown): e is EmbeddingEngine & { getEmbeddingContextSize(): Promise<number> } {\n  return typeof (e as any)?.getEmbeddingContextSize === 'function'\n}","tryCatchPattern":"try {\n  const size = await probeEmbeddingContextSize(llm)\n} catch (e) {\n  const cause = e instanceof Error ? e.message : String(e)\n  if (/auth|api key|401|403/i.test(cause)) fixCredentials()\n  else if (/network|fetch|timeout/i.test(cause)) await retryWithBackoff()\n  else useDefaultChunkBudget()\n}","preventionTips":["Validate embedding provider config (key, URL, model) at startup, before any ingest.","Health-check the embedding engine with a trivial embed call before large operations.","Provide a sensible default context size fallback for engines without the API."],"tags":["embedding","initialization","wrapper-error"],"backgroundTag":"api-error-response","analyzedSha":"7205d770c1e097c3daf35a911176410e93bc5564","analyzedAt":"2026-09-17T14:27:30.100Z","contentChangedAt":"2026-09-17T14:27:30.100Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}