{"record":{"id":"b240b4e2cc51fe25","repo":"Mintplex-Labs/anything-llm","slug":"ollama-getchatcompletion-failed-to-communicate-wi","errorCode":null,"errorMessage":"Ollama::getChatCompletion failed to communicate with Ollama. ${this.#errorHandler(e).message}","messagePattern":"Ollama::getChatCompletion failed to communicate with Ollama\\. (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/ollama/index.js","lineNumber":298,"sourceCode":"            num_ctx: this.promptWindowLimit(),\n          },\n        })\n        .then((res) => {\n          let content = res.message.content;\n          if (res.message.thinking)\n            content = `<think>${res.message.thinking}</think>${content}`;\n          return {\n            content,\n            usage: {\n              prompt_tokens: res.prompt_eval_count,\n              completion_tokens: res.eval_count,\n              total_tokens: res.prompt_eval_count + res.eval_count,\n              duration: res.eval_duration / 1e9,\n            },\n          };\n        })\n        .catch((e) => {\n          throw new Error(\n            `Ollama::getChatCompletion failed to communicate with Ollama. ${this.#errorHandler(e).message}`\n          );\n        })\n    );\n\n    if (!result.output.content || !result.output.content.length)\n      throw new Error(`Ollama::getChatCompletion text response was empty.`);\n\n    return {\n      textResponse: result.output.content,\n      metrics: {\n        prompt_tokens: result.output.usage.prompt_tokens,\n        completion_tokens: result.output.usage.completion_tokens,\n        total_tokens: result.output.usage.total_tokens,\n        outputTps:\n          result.output.usage.completion_tokens / result.output.usage.duration,\n        duration: result.output.usage.duration,\n        model: this.model,","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/ollama/index.js#L280-L316","documentation":"Catch-all wrapper around any rejection from this.client.chat(...) in the non-streaming path. It routes the error through #errorHandler, which either rewrites 'fetch failed' into the friendly unreachable message (error 263) or returns the original error; the result is prefixed with 'Ollama::getChatCompletion failed to communicate with Ollama.' This is the umbrella error for all non-network Ollama chat failures.","triggerScenarios":"Model not pulled on the Ollama server; model name typo; num_ctx/prompt exceeding the model's capacity; GPU OOM during inference; Ollama internal 500; connection refused (suffix becomes the fetch-failed message); invalid options payload.","commonSituations":"User selected a model in the UI that was never 'ollama pull'ed; quantized model OOMs on the available VRAM; chat history grew past the context window; Ollama restarted mid-session; wrong model id casing.","solutions":["Read the suffix after 'failed to communicate with Ollama.' to get the true cause.","If the suffix is the unreachable message, follow error 263's connectivity steps.","Run 'ollama list' on the server and confirm this.model is present; 'ollama pull' if missing.","For context/OOM errors, lower OLLAMA_MODEL_TOKEN_LIMIT or reduce injected context."],"exampleFix":"// before\n.catch((e) => {\n  throw new Error(`Ollama::getChatCompletion failed to communicate with Ollama. ${this.#errorHandler(e).message}`);\n})\n\n// after - distinguish network vs model errors for callers\n.catch((e) => {\n  const friendly = this.#errorHandler(e);\n  const wrapped = new Error(`Ollama::getChatCompletion failed: ${friendly.message}`);\n  wrapped.code = e?.cause?.code || (e.message === 'fetch failed' ? 'ECONNREFUSED' : 'OLLAMA_ERROR');\n  throw wrapped;\n})","handlingStrategy":"try-catch","validationCode":"const ensureOllamaModel = async (client, model) => {\n  const { models = [] } = await client.list().catch(() => ({ models: [] }));\n  if (!models.some((m) => m.name === model))\n    throw new Error(`Ollama model '${model}' is not pulled. Run: ollama pull ${model}`);\n};\nawait ensureOllamaModel(llm.client, llm.model);","typeGuard":"const isOllamaCommError = (e) =>\n  !!e && /failed to communicate with Ollama/.test(e.message);","tryCatchPattern":"try {\n  return await llm.getChatCompletion(messages, { temperature });\n} catch (e) {\n  if (e.message.includes('could not be reached')) { /* connectivity path */ }\n  if (e.message.toLowerCase().includes('model not found')) { /* pull/fix model */ }\n  throw e;\n}","preventionTips":["Verify the model is pulled with 'ollama list' before pointing chat at it.","Pre-warm large models (keep_alive) so first-request load failures surface at warmup.","Cap OLLAMA_MODEL_TOKEN_LIMIT below the model's real context to avoid overflow errors."],"tags":["ollama","llm-provider","runtime","error-handling"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}