earendil-works/pi · error

grammar tool input for property

Error message

grammar tool input for property "${inputProperty}" changed non-monotonically

What it means

Each delta must extend the buffered input (nextInput.startsWith(buffer.input)); a non-prefix delta means the stream went backwards or interleaved two calls. The buffer throws rather than emitting corrupted argument JSON for the grammar tool.

Solutions

  1. Ensure grammar tool input only grows by appending; never rewrite earlier content.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at packages/ai/src/api/constrained-sampling.ts:168 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/6280989538f86ac0. Report an issue: GitHub.

Appendix: source

Thrown at packages/ai/src/api/constrained-sampling.ts:168

	const input = arguments_[inputProperty];
	if (typeof input !== "string") {
		throw new Error(`Grammar tool call "${toolName}" requires argument "${inputProperty}" to be a string.`);
	}
	return input;
}

export function appendGrammarToolInputJsonDelta(
	buffer: GrammarToolInputJsonBuffer,
	inputProperty: string,
	nextInput: string,
	close: boolean,
): string | undefined {
	if (buffer.closed) {
		if (close && nextInput === buffer.input) return undefined;
		throw new Error(`grammar tool input for property "${inputProperty}" changed after it was closed`);
	}
	if (!nextInput.startsWith(buffer.input)) {
		throw new Error(`grammar tool input for property "${inputProperty}" changed non-monotonically`);
	}

	const inputDelta = nextInput.slice(buffer.input.length);
	if (!close && inputDelta.length === 0) return undefined;

	let delta = "";
	if (!buffer.started) {
		delta += `{${JSON.stringify(inputProperty)}:"`;
		buffer.started = true;
	}
	delta += JSON.stringify(inputDelta).slice(1, -1);
	buffer.input = nextInput;

	if (close) {
		delta += '"}';
		buffer.closed = true;
	}
	return delta;

View on GitHub (pinned to 4af9d21d3b)