can1357/oh-my-pi · error · AIError.ValidationError

`context.messages` must be an array

Error message

`context.messages` must be an array

What it means

After confirming `context` is a plain object, parseRequest requires `context.messages` to be an array. This is the mandatory core of the conversation payload; without a messages array the server cannot build a prompt. A missing, non-array, or null `messages` field raises this ValidationError.

Source

Thrown at packages/ai/src/providers/pi-native-server.ts:125

	let modelId: string | undefined;
	if (typeof obj.modelId === "string" && obj.modelId.length > 0) {
		modelId = obj.modelId;
	} else if (typeof obj.model === "string" && obj.model.length > 0) {
		modelId = obj.model;
	} else if (typeof obj.model === "object" && obj.model !== null) {
		const m = obj.model as Record<string, unknown>;
		if (typeof m.id === "string" && m.id.length > 0) modelId = m.id;
	}
	if (!modelId) throw new AIError.ValidationError("Missing `modelId` (or `model.id`) field");

	const context = obj.context;
	if (typeof context !== "object" || context === null || Array.isArray(context)) {
		throw new AIError.ValidationError("Missing `context` object");
	}
	const ctxObj = context as Record<string, unknown>;
	if (!Array.isArray(ctxObj.messages)) {
		throw new AIError.ValidationError("`context.messages` must be an array");
	}
	if (ctxObj.systemPrompt !== undefined && !Array.isArray(ctxObj.systemPrompt)) {
		throw new AIError.ValidationError("`context.systemPrompt` must be an array of strings when present");
	}
	if (ctxObj.tools !== undefined && !Array.isArray(ctxObj.tools)) {
		throw new AIError.ValidationError("`context.tools` must be an array when present");
	}

	const options: SimpleStreamOptions = {};
	const rawOpts = obj.options;
	if (typeof rawOpts === "object" && rawOpts !== null && !Array.isArray(rawOpts)) {
		const optsBag = options as Record<string, unknown>;
		for (const [k, v] of Object.entries(rawOpts)) {
			if (v === undefined || v === null) continue;
			if (!ALLOWED_OPTION_KEYS.has(k as keyof SimpleStreamOptions)) continue;
			optsBag[k] = v;
		}
	}

View on GitHub (pinned to 9690622007)

Solutions

  1. Set `context.messages` to an array of message objects
  2. If sending a single message, wrap it: `messages:[{role:"user",content:...}]`
  3. Validate the payload client-side before POSTing

Example fix

// before
{"context":{"messages":{"role":"user","content":"hi"}}}
// after
{"context":{"messages":[{"role":"user","content":"hi"}]}}
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(body?.context?.messages)) throw new Error('context.messages must be an array');

Type guard

function hasMessagesArray(ctx: unknown): ctx is { messages: unknown[] } {
  return typeof ctx === 'object' && ctx !== null && Array.isArray((ctx as { messages?: unknown }).messages);
}

Try / catch

try { const req = parseRequest(body); } catch (e) { if (e instanceof AIError.ValidationError && String(e.message).includes('`context.messages`')) return respond400('context.messages must be an array'); throw e; }

Prevention

When it happens

Trigger: Sending `{"model":...,"context":{}}`, `context:{"messages":null}`, or `context:{"messages":"hello"}` to the pi-native server endpoint.

Common situations: Clients that serialize only the last message as a string; templates with a placeholder left unfilled; SDK misuse passing a single message object instead of an array.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/43f320982506e599. Report an issue: GitHub.