perf(json): keep array scan linear on malformed output

This commit is contained in:
Yif-Yang
2026-07-14 08:46:54 +00:00
parent 1b64b000fb
commit 48e4c66827
2 changed files with 41 additions and 25 deletions
+15
View File
@@ -105,6 +105,21 @@ class TestTopLevelBracketArrays:
text = "Explanation uses {placeholder, final answer [1, 2]"
assert _top_level_bracket_arrays(text) == ["[1, 2]"]
def test_many_unmatched_braces_scan_linearly(self) -> None:
class CountingText(str):
def __new__(cls, value: str):
instance = super().__new__(cls, value)
instance.reads = 0
return instance
def __getitem__(self, key):
self.reads += 1
return super().__getitem__(key)
text = CountingText("{" * 1_000 + " final answer [1, 2]")
assert _top_level_bracket_arrays(text) == ["[1, 2]"]
assert text.reads < len(text) * 4
class TestExtractJsonTolerantFallback:
"""extract_json — json_repair fallback for malformed non-OpenAI output."""