can1357/oh-my-pi · error · SyntaxError

Expected ',' or '}' in object

Error message

Expected ',' or '}' in object

What it means

Thrown by RelaxedJson.#object in strict mode after a value when the next character is neither ',' nor '}' — the object's members are not properly delimited. The relaxed parser already tolerates leading/doubled/trailing commas, so this only fires for genuinely bad delimiters; partial mode returns the object so far instead of throwing.

Source

Thrown at packages/utils/src/json-parse.ts:299

			if (this.#i >= this.#n) {
				if (this.#partial) return out;
				throw new SyntaxError("Expected value after ':'");
			}
			const value = this.#value(true);
			if (value === INCOMPLETE) return out;
			out[key] = value;
			this.#ws();
			const d = this.#i < this.#n ? this.#s[this.#i] : "";
			if (d === ",") {
				this.#i++;
				continue;
			}
			if (d === "}") {
				this.#i++;
				return out;
			}
			if (this.#partial) return out;
			throw new SyntaxError("Expected ',' or '}' in object");
		}
	}

	#array(): unknown[] {
		this.#i++; // consume [
		const out: unknown[] = [];
		for (;;) {
			this.#ws();
			if (this.#i >= this.#n) {
				if (this.#partial) return out;
				throw new SyntaxError("Unterminated array");
			}
			const c = this.#s[this.#i];
			if (c === "]") {
				this.#i++;
				return out;
			}
			if (c === ",") {

View on GitHub (pinned to 9690622007)

Solutions

  1. Insert the missing ',' or '}' in the source JSON at the failing position
  2. Use parseStreamingJson if the buffer may be a partial prefix (it returns the object parsed so far)
  3. Regenerate the JSON from the producer; this malformation pattern from an LLM often repeats, so validate output against a schema before executing the tool call

Example fix

// before
const v = parseJsonWithRepair(`{"a": 1 "b": 2}`); // SyntaxError
// after
const v = parseJsonWithRepair(`{"a": 1, "b": 2}`); // {a: 1, b: 2}
Defensive patterns

Strategy: validation

Validate before calling

import { classifyJsonPrefix } from "@oh-my-pi/pi-utils/json-parse";
// 'invalid' with a missing comma cannot be repaired — detect before parsing
const state = classifyJsonPrefix(text);

Try / catch

try {
  const value = parseJsonWithRepair<T>(text);
} catch (err) {
  if (err instanceof SyntaxError && err.message === "Expected ',' or '}' in object") {
    // reject the tool call / regenerate the payload
  } else throw err;
}

Prevention

When it happens

Trigger: parseJsonWithRepair with '{"a": 1 "b": 2}' (missing comma), '{"a": 1 )}', or any char other than ',' / '}' between members in strict mode.

Common situations: An LLM emitting space-separated key/value pairs without commas; template string interpolation that dropped a comma; concatenation artifacts between two adjacent fields.

Related errors


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