Tolerate common malformed VLM inventory JSON

This commit is contained in:
2026-11-03 11:21:29 +00:00
parent f770609af8
commit 09529db9d6
2 changed files with 102 additions and 1 deletions
+88 -1
View File
@@ -45,7 +45,19 @@ def _parse_json(inventory_json: str) -> dict[str, Any]:
try:
value = json.loads(text)
except json.JSONDecodeError as exc:
raise ValueError(f"inventory_json is invalid JSON at line {exc.lineno}, column {exc.colno}: {exc.msg}") from exc
repaired = _repair_vlm_json(text)
if repaired is None:
raise ValueError(
f"inventory_json is invalid JSON at line {exc.lineno}, column {exc.colno}: {exc.msg}. "
"Check that the VLM returned a complete JSON object with quoted strings."
) from exc
try:
value = json.loads(repaired)
except json.JSONDecodeError as repaired_exc:
raise ValueError(
f"inventory_json is invalid JSON at line {exc.lineno}, column {exc.colno}: {exc.msg}. "
f"Automatic VLM repair also failed at line {repaired_exc.lineno}, column {repaired_exc.colno}."
) from exc
if not isinstance(value, dict):
raise ValueError("inventory_json must be a JSON object with an 'items' list")
items = value.get("items", [])
@@ -54,6 +66,81 @@ def _parse_json(inventory_json: str) -> dict[str, Any]:
return value
def _repair_vlm_json(text: str) -> str | None:
"""Repair only safe, common VLM JSON damage.
VLMs frequently emit literal newlines in descriptions, leave a quote
unescaped inside prose, or stop before writing the final closing braces.
This scanner repairs those cases without changing field names or values.
It deliberately does not guess missing commas, keys, or inventory items.
"""
start = text.find("{")
if start < 0:
return None
source = text[start:]
output: list[str] = []
stack: list[str] = []
in_string = False
escaped = False
root_closed = False
index = 0
while index < len(source):
char = source[index]
if root_closed:
index += 1
continue
if in_string:
if escaped:
output.append(char)
escaped = False
elif char == "\\":
output.append(char)
escaped = True
elif char == '"':
lookahead = index + 1
while lookahead < len(source) and source[lookahead].isspace():
lookahead += 1
# A quote followed by normal prose is part of a VLM string;
# JSON string terminators are followed by structure.
if lookahead == len(source) or source[lookahead] in ",]}:":
output.append(char)
in_string = False
else:
output.append('\\"')
elif char in "\r\n":
output.append("\\n")
elif ord(char) < 32:
output.append(f"\\u{ord(char):04x}")
else:
output.append(char)
else:
if char == '"':
in_string = True
output.append(char)
elif char in "[{":
stack.append(char)
output.append(char)
elif char in "]}":
if stack and ((char == "]" and stack[-1] == "[") or (char == "}" and stack[-1] == "{")):
stack.pop()
output.append(char)
if not stack:
root_closed = True
else:
# Ignore trailing prose or an unmatched closing token.
pass
else:
output.append(char)
index += 1
if in_string:
output.append('"')
while stack:
output.append("]" if stack.pop() == "[" else "}")
repaired = re.sub(r",\s*([}\]])", r"\1", "".join(output)).strip()
return repaired if repaired.startswith("{") else None
def normalize_inventory(inventory_json: str) -> list[dict[str, Any]]:
"""Parse and validate inventory entries without inventing entries."""
@@ -27,6 +27,20 @@ class ClothingLayoutPlannerTests(unittest.TestCase):
}) + "\n```")
self.assertEqual(items[0]["description"], "blue linen")
def test_repairs_literal_newlines_and_unescaped_prose_quotes(self):
raw = '{"items":[{"id":"shirt","name":"shirt","description":"Blue shirt\nwith a "soft" collar"}]}'
items = PLANNER.normalize_inventory(raw)
self.assertEqual(items[0]["description"], 'Blue shirt\nwith a "soft" collar')
def test_repairs_truncated_vlm_json_by_closing_open_containers(self):
raw = '{"items":[{"id":"shoe","name":"shoe","description":"Black leather'
items = PLANNER.normalize_inventory(raw)
self.assertEqual(items[0]["description"], "Black leather")
def test_ignores_vlm_prose_after_json_object(self):
raw = inventory({"id": "hat", "name": "hat"}) + "\nHere is the inventory."
self.assertEqual(PLANNER.normalize_inventory(raw)[0]["id"], "hat")
def test_quantities_and_pairs_create_deterministic_instances(self):
plan = PLANNER.plan_layout(inventory(
{"id": "sock", "name": "sock", "quantity": 3},