You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cut back incomplete UTF-16 surrogate pairs in JSONCompleter - #442
Fix JSONCompleter.completeString so that when a streamed JSON string ends right after a UTF-16 high surrogate escape (\uD800–\uDBFF) or mid-way through its trailing low surrogate (\uD83D\, \uD83D\u, \uD83D\uDE, \uD83D\uDE0), completion cuts back to the start of the high surrogate escape instead of closing the string with an unpaired high surrogate.
Previously, closing right after a high surrogate produced invalid JSON ("Hi \uD83D") that JSONSerialization / JSONDecoder reject, causing GeneratedContent(json:) to lose structured object decoding and fall back to .string(rawJSON) until the full low surrogate arrived.
Add regression tests in JSONCompleterTests.handlesPartialSurrogatePairs covering every character-by-character prefix of surrogate-pair escapes, lowercase hex, object keys/values, and GeneratedContent(json:).
Verification
swift format lint --strict --recursive . — passed
swift test --disable-index-store --filter "JSONCompleterTests|GeneratedContentJSONTests" — 41/41 tests in 2 suites passed (failed with 21 issues in handlesPartialSurrogatePairs before the fix)
swift test --skip-build --disable-index-store --skip OllamaLanguageModelTests — 1,104/1,104 tests in 113 suites passed
Hi @Pybsama. Thanks for this! One problem before we merge: when the low-surrogate escape after a high surrogate hits a non-hex character before the end of the input, completeString still cuts the string back. For {"a":"\uD83D\uDE","b":1}, the result parses as {"a":""} and silently drops "b":1, instead of falling back to the raw string. Could you cut back only when the scan reaches the end of the input, and add a regression test for that case?
Thanks @mattt for catching that! Updated completeString in e3d3f5d so both short \uXXXX and trailing low-surrogate cut-backs only trigger when the scan reaches json.endIndex, and added regression tests for {"a":"\uD83D\uDE","b":1} and {"a":"\u12","b":1} verifying completer.complete leaves them untouched and GeneratedContent(json:) falls back to .string(...).
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
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.
Summary
JSONCompleter.completeStringso that when a streamed JSON string ends right after a UTF-16 high surrogate escape (\uD800–\uDBFF) or mid-way through its trailing low surrogate (\uD83D\,\uD83D\u,\uD83D\uDE,\uD83D\uDE0), completion cuts back to the start of the high surrogate escape instead of closing the string with an unpaired high surrogate."Hi \uD83D") thatJSONSerialization/JSONDecoderreject, causingGeneratedContent(json:)to lose structured object decoding and fall back to.string(rawJSON)until the full low surrogate arrived.JSONCompleterTests.handlesPartialSurrogatePairscovering every character-by-character prefix of surrogate-pair escapes, lowercase hex, object keys/values, andGeneratedContent(json:).Verification
swift format lint --strict --recursive .— passedswift test --disable-index-store --filter "JSONCompleterTests|GeneratedContentJSONTests"— 41/41 tests in 2 suites passed (failed with 21 issues inhandlesPartialSurrogatePairsbefore the fix)swift test --skip-build --disable-index-store --skip OllamaLanguageModelTests— 1,104/1,104 tests in 113 suites passed