gitbutlerapp/gitbutler · error

Invalid prompt: must be a non-empty array

Error message

Invalid prompt: must be a non-empty array

What it means

LMStudioClient.evaluate() validates its `prompt` argument before sending anything to the LM Studio server. It requires a non-empty array of Prompt messages; anything falsy, non-array, or an empty array is rejected. This guards against calling the chat completions endpoint with no messages.

Solutions

  1. Ensure the prompt is an array of message objects with role and content before calling evaluate().
  2. Check that your prompt-building logic never returns an empty array; add a fallback or early return.
  3. If a single message, wrap it: [{ role: 'user', content: '...' }].

Example fix

// before
await client.evaluate('hello');
// after
await client.evaluate([{ role: 'User', content: 'hello' }]);
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(prompt) || prompt.length === 0) {
  throw new Error('prompt must be a non-empty array of messages');
}
await client.evaluate(prompt);

Type guard

function isValidPrompt(p: unknown): p is Prompt {
  return Array.isArray(p) && p.length > 0 && p.every(m => m && typeof m.role === 'string' && typeof m.content === 'string');
}

Try / catch

try {
  await client.evaluate(prompt);
} catch (e) {
  if (e.message.includes('Invalid prompt')) {
    // fall back to a default prompt or surface a user-facing validation error
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling evaluate() with null/undefined, with a non-array value (e.g. a plain string), or with an empty array [].

Common situations: Prompt-building code that returns [] when no messages were collected; passing a single string instead of an array of messages; uninitialized prompt variable; a caller that filters messages and ends up with an empty list.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at apps/desktop/src/lib/ai/lmStudioClient.ts:40

	private baseUrl: string;
	private modelName: string;

	constructor(endpoint: string, modelName: string) {
		this.modelName = modelName;
		// Format the base URL to ensure it ends with /v1
		this.baseUrl = endpoint.endsWith("/v1")
			? endpoint
			: endpoint.endsWith("/")
				? `${endpoint}v1`
				: `${endpoint}/v1`;
	}

	async evaluate(prompt: Prompt, options?: AIEvalOptions): Promise<string> {
		try {
			// Validate input
			if (!prompt || !Array.isArray(prompt) || prompt.length === 0) {
				throw new Error("Invalid prompt: must be a non-empty array");
			}

			// Format messages for the API
			const messages = prompt.map((msg) => ({
				role: msg.role.toLowerCase(),
				content: msg.content,
			}));

			// Determine if we should stream the response
			const shouldStream = options?.onToken !== undefined;

			// Make request to the LM Studio API
			const response = await fetch(`${this.baseUrl}/chat/completions`, {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
				},
				body: JSON.stringify({

View on GitHub (pinned to 58e5313667)