fix(llm): keep answers too deeply nested to parse whole in prefilled mode

Signed-off-by: FenjuFu <fufenjupku@gmail.com>
This commit is contained in:
FenjuFu
2026-10-05 01:19:14 +08:00
parent d351fc5502
commit 586d4b91cc
2 changed files with 7 additions and 4 deletions
+6 -3
View File
@@ -55,14 +55,17 @@ def _is_json_document(content: str) -> bool:
Reasoning followed by a closing tag is never valid JSON, so a structured
answer that carries the tag inside a string value is left whole. Input
nested too deeply to parse is not treated as JSON rather than failing the
request.
nested too deeply for the parser to finish is kept whole as well: reasoning
does not open with that many brackets, so cutting it would only risk
truncating a real answer.
"""
stripped = content.strip()
if not stripped.startswith(("{", "[")):
return False
try:
json.loads(stripped)
except (ValueError, RecursionError):
except RecursionError:
return True
except ValueError:
return False
return True
+1 -1
View File
@@ -70,7 +70,7 @@ def test_strip_reasoning_prefilled_survives_deeply_nested_json() -> None:
# Deep enough for json.loads to raise RecursionError on every supported Python.
depth = 100_000
content = "[" * depth + '"</think>"' + "]" * depth
assert strip_reasoning(content, prefilled=True) == '"' + "]" * depth
assert strip_reasoning(content, prefilled=True) == content
def _openai_provider(content: str, **kwargs: Any) -> OpenAIProvider: