Repository navigation
feat(providers): repair double-encoded structured output from LLMs #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /** | ||
| * JSON structural delimiters that may legitimately follow a closing string | ||
| * quote. Used to distinguish a real closing quote from an unescaped internal | ||
| * quote when repairing model output. | ||
| */ | ||
| const JSON_STRUCTURAL_DELIMITERS = new Set([',', '}', ']', ':']); | ||
|
|
||
| function isJsonWhitespace(char: string): boolean { | ||
| return char === ' ' || char === '\t' || char === '\n' || char === '\r'; | ||
| } | ||
|
|
||
| /** Escapes literal control characters as JSON escape sequences. */ | ||
| function escapeControlChars(value: string): string { | ||
| // eslint-disable-next-line no-control-regex -- control characters are the intended match | ||
| return value.replace(/[\x00-\x1f]/g, char => { | ||
| switch (char) { | ||
| case '\n': return '\\n'; | ||
| case '\t': return '\\t'; | ||
| case '\r': return '\\r'; | ||
| case '\b': return '\\b'; | ||
| case '\f': return '\\f'; | ||
| default: | ||
| return '\\u' + char.charCodeAt(0).toString(16).padStart(4, '0'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Repairs a JSON string that contains unescaped internal quotes inside string | ||
| * values. Walks the text with a lightweight state machine: a quote inside a | ||
| * string value that is followed (after optional whitespace) by a structural | ||
| * delimiter is treated as the closing quote; any other quote is escaped as | ||
| * `\"`. Literal control characters encountered inside a string value are | ||
| * escaped too, so inter-token whitespace stays valid. | ||
| */ | ||
| function escapeInternalQuotes(value: string): string { | ||
| let out = ''; | ||
| let inString = false; | ||
| for (let i = 0; i < value.length; i++) { | ||
| const char = value[i]!; | ||
| if (!inString) { | ||
| out += char; | ||
| if (char === '"') { | ||
| inString = true; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // Preserve existing escape sequences verbatim. | ||
| if (char === '\\' && i + 1 < value.length) { | ||
| out += char + value[i + 1]!; | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| if (char === '"') { | ||
| let j = i + 1; | ||
| while (j < value.length && isJsonWhitespace(value[j]!)) { | ||
| j++; | ||
| } | ||
| const isClosingQuote = j >= value.length || JSON_STRUCTURAL_DELIMITERS.has(value[j]!); | ||
| if (isClosingQuote) { | ||
| out += '"'; | ||
| inString = false; | ||
| } else { | ||
| out += '\\"'; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| const codePoint = char.charCodeAt(0); | ||
| if (codePoint < 0x20) { | ||
| out += escapeControlChars(char); | ||
| continue; | ||
| } | ||
|
|
||
| out += char; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** Tries several repair strategies on a JSON-ish string; returns the parsed value or undefined. */ | ||
| function repairJsonString(value: string): unknown { | ||
| const attempts = [value, escapeControlChars(value), escapeInternalQuotes(value)]; | ||
| for (const candidate of attempts) { | ||
| try { | ||
| return JSON.parse(candidate); | ||
| } catch { | ||
| // try next strategy | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function looksLikeJson(value: string): boolean { | ||
| const trimmed = value.trimStart(); | ||
| const startsJson = trimmed.startsWith('[') || trimmed.startsWith('{'); | ||
| const endsJson = trimmed.endsWith(']') || trimmed.endsWith('}'); | ||
| return startsJson && endsJson; | ||
| } | ||
|
|
||
| /** Recursively repairs any string values that look like double encoded JSON. */ | ||
| export function repairNestedJson(value: unknown): unknown { | ||
| if (Array.isArray(value)) { | ||
| return value.map(repairNestedJson); | ||
| } | ||
| if (value !== null && typeof value === 'object') { | ||
| const result: Record<string, unknown> = {}; | ||
| for (const [key, child] of Object.entries(value as Record<string, unknown>)) { | ||
| result[key] = repairNestedJson(child); | ||
| } | ||
| return result; | ||
| } | ||
| if (typeof value === 'string' && looksLikeJson(value)) { | ||
| const repaired = repairJsonString(value); | ||
| if (repaired !== undefined) { | ||
| return repairNestedJson(repaired); | ||
| } | ||
| } | ||
| return value; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.