Sub topic 2
Cleaning Model Output Before Showing It
This chapter assumes you understand large language models, can read Python or TypeScript, and have used an IDE and version-control diffs.
Cleaning model output before showing it
A model may return text that does not fit at the cursor. It might repeat a closing bracket already present in the suffix, add Markdown fences, or continue past the logical end of a statement. Post-processing is the step that makes raw output safe to display.
A common operation is overlap trimming. Compare the end of the completion with the beginning of the suffix. Remove the repeated part, but keep the suffix itself unchanged.
def trim_overlap(completion: str, suffix: str) -> str:
for size in range(min(len(completion), len(suffix)), 0, -1):
if completion.endswith(suffix[:size]):
return completion[:-size]
return completion
print(trim_overlap("foo(a, b)", ")"))Also reject empty output, obviously unrelated text, and output that exceeds a practical size limit. Syntax parsing can provide a stronger check than string rules, but it adds latency.
Accepting, rejecting and partially accepting suggestions
Ghost text should remain easy to ignore. The user can:
- Accept the complete suggestion.
- Reject it by continuing to type or pressing Escape.
- Accept only the next token, line, or word.
Partial acceptance reduces the cost of a wrong tail. After each acceptance, request fresh context rather than assuming the remaining suggestion is still valid.
Writing a completion provider in an editor extension
An editor provider receives the document, cursor position, and a cancellation token. It extracts the prefix and suffix, calls the model, cleans the result, and returns an inline item.
async function provideInlineCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
) {
const start = new vscode.Position(0, 0);
const end = document.positionAt(document.getText().length);
const prefix = document.getText(new vscode.Range(start, position));
const suffix = document.getText(new vscode.Range(position, end));
const text = await fetchCompletion(prefix, suffix, token);
if (token.isCancellationRequested || !text) return [];
return [
new vscode.InlineCompletionItem(
trimOverlap(text, suffix),
new vscode.Range(position, position)
),
];
}The provider should also debounce requests and abort work when the document changes.
Using recent edits as context
Recent edit history gives the model information that the current file may not contain. For example, renaming w to width in a function definition suggests that nearby keyword arguments should change too. Send a bounded history: changed ranges, edit type, and a small amount of surrounding code. Avoid sending unrelated files or sensitive text.
Next edit suggestions: predicting the edit
An inline completion predicts text at the cursor. A next edit suggestion predicts both an edit location and the replacement content, often based on recent edits.
Next edit suggestions are useful for coordinated changes, such as updating a renamed parameter at its call sites. They require stronger location prediction and clearer diff presentation. Review the target range, surrounding code, and proposed change before accepting it.
Exercise
Why should a provider trim overlap before returning ghost text?
Show answer
The model may repeat text already present after the cursor, which would create duplicated brackets, tokens, or lines when accepted.