gitbutlerapp/gitbutler · error

Invalid response: " + JSON.stringify(result)

Error message

Invalid response: " + JSON.stringify(result)

What it means

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.

Solutions

  1. Inspect the JSON in the error message to see what Ollama actually returned.
  2. Update or align the Ollama server version with the schema this client expects.
  3. Verify the Ollama host URL points at the correct /api endpoint.
  4. Check Ollama server logs for model load failures.

Example fix

// before
const client = new OllamaClient('http://localhost:8080'); // proxy returning error JSON
// after
const client = new OllamaClient('http://localhost:11434');
Defensive patterns

Strategy: type-guard

Validate before calling

const raw = await fetch(ollamaHost + '/api/tags');
if (!raw.ok) throw new Error('Ollama host not reachable/healthy');

Type guard

function isOllamaChatResponse(r: unknown): r is OllamaChatResponse {
  return typeof r === 'object' && r !== null && 'message' in r && typeof (r as any).message?.content === 'string';
}

Try / catch

try {
  const resp = await client.chat(params);
  // use resp
} catch (e) {
  if (e.message.startsWith('Invalid response:')) {
    const actual = e.message.slice('Invalid response:'.length);
    // log actual payload; check for Ollama version/schema drift or { error } object
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/bb5a153b1f2ebc93. Report an issue: GitHub.

Appendix: source

Thrown at apps/desktop/src/lib/ai/ollamaClient.ts:84

	 * Sends a chat message to the LLM model and returns the response.
	 *
	 * @param messages - An array of LLMChatMessage objects representing the chat messages.
	 * @param options - Optional LLMRequestOptions object for specifying additional options.
	 * @returns A Promise that resolves to an LLMResponse object representing the response from the LLM model.
	 */
	private async chat(
		messages: Prompt,
		options?: OllamaRequestOptions,
	): Promise<OllamaChatResponse> {
		const result = await this.ollama.chat({
			model: this.modelName,
			messages,
			stream: false,
			options,
		});

		if (!isOllamaChatResponse(result)) {
			throw new Error("Invalid response: " + JSON.stringify(result));
		}

		return result;
	}
}

View on GitHub (pinned to 58e5313667)