{"record":{"id":"bb5a153b1f2ebc93","repo":"gitbutlerapp/gitbutler","slug":"invalid-response-json-stringify-result-ollamaclient","errorCode":null,"errorMessage":"Invalid response: \" + JSON.stringify(result)","messagePattern":"Invalid response: \" \\+ JSON\\.stringify\\(result\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/desktop/src/lib/ai/ollamaClient.ts","lineNumber":84,"sourceCode":"\t * Sends a chat message to the LLM model and returns the response.\n\t *\n\t * @param messages - An array of LLMChatMessage objects representing the chat messages.\n\t * @param options - Optional LLMRequestOptions object for specifying additional options.\n\t * @returns A Promise that resolves to an LLMResponse object representing the response from the LLM model.\n\t */\n\tprivate async chat(\n\t\tmessages: Prompt,\n\t\toptions?: OllamaRequestOptions,\n\t): Promise<OllamaChatResponse> {\n\t\tconst result = await this.ollama.chat({\n\t\t\tmodel: this.modelName,\n\t\t\tmessages,\n\t\t\tstream: false,\n\t\t\toptions,\n\t\t});\n\n\t\tif (!isOllamaChatResponse(result)) {\n\t\t\tthrow new Error(\"Invalid response: \" + JSON.stringify(result));\n\t\t}\n\n\t\treturn result;\n\t}\n}\n","sourceCodeStart":66,"sourceCodeEnd":90,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/apps/desktop/src/lib/ai/ollamaClient.ts#L66-L90","documentation":"OllamaClient.chat() validates the Ollama /api/chat response with the isOllamaChatResponse type guard; a response that does not match the expected shape (message, etc.) throws this error with the JSON-serialized payload. It protects callers from malformed or unexpected server output.","triggerScenarios":"Ollama returns a 200 response whose body lacks the expected chat response fields — e.g. an { error: ... } object, a proxy/auth HTML page, or a schema change in a new Ollama version.","commonSituations":"Outdated or newer Ollama version with changed response schema; response intercepted by a proxy or auth portal; pointing the Ollama host at the wrong port/path so an error JSON is returned; model failed to load and Ollama returned an error object with 200.","solutions":["Inspect the JSON in the error message to see what Ollama actually returned.","Update or align the Ollama server version with the schema this client expects.","Verify the Ollama host URL points at the correct /api endpoint.","Check Ollama server logs for model load failures."],"exampleFix":"// before\nconst client = new OllamaClient('http://localhost:8080'); // proxy returning error JSON\n// after\nconst client = new OllamaClient('http://localhost:11434');","handlingStrategy":"type-guard","validationCode":"const raw = await fetch(ollamaHost + '/api/tags');\nif (!raw.ok) throw new Error('Ollama host not reachable/healthy');","typeGuard":"function isOllamaChatResponse(r: unknown): r is OllamaChatResponse {\n  return typeof r === 'object' && r !== null && 'message' in r && typeof (r as any).message?.content === 'string';\n}","tryCatchPattern":"try {\n  const resp = await client.chat(params);\n  // use resp\n} catch (e) {\n  if (e.message.startsWith('Invalid response:')) {\n    const actual = e.message.slice('Invalid response:'.length);\n    // log actual payload; check for Ollama version/schema drift or { error } object\n  } else {\n    throw e;\n  }\n}","preventionTips":["Pin/verify the Ollama server version against the client's expected schema.","Point the client at the correct Ollama port (11434), not a proxy.","Log unexpected payloads to detect upstream schema changes early."],"tags":["validation","response","ollama"],"backgroundTag":"unexpected-response-shape","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}