Recover inventory items from severely malformed VLM JSON
This commit is contained in:
@@ -47,6 +47,9 @@ def _parse_json(inventory_json: str) -> dict[str, Any]:
|
||||
except json.JSONDecodeError as exc:
|
||||
repaired = _repair_vlm_json(text)
|
||||
if repaired is None:
|
||||
salvaged = _salvage_inventory(text)
|
||||
if salvaged is not None:
|
||||
return {"items": salvaged}
|
||||
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."
|
||||
@@ -54,6 +57,9 @@ def _parse_json(inventory_json: str) -> dict[str, Any]:
|
||||
try:
|
||||
value = json.loads(repaired)
|
||||
except json.JSONDecodeError as repaired_exc:
|
||||
salvaged = _salvage_inventory(text)
|
||||
if salvaged is not None:
|
||||
return {"items": salvaged}
|
||||
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}."
|
||||
@@ -66,6 +72,47 @@ def _parse_json(inventory_json: str) -> dict[str, Any]:
|
||||
return value
|
||||
|
||||
|
||||
def _salvage_inventory(text: str) -> list[dict[str, Any]] | None:
|
||||
"""Extract schema fields from item objects when one VLM string is broken."""
|
||||
|
||||
starts = [match.start() for match in re.finditer(r'\{\s*"id"\s*:', text)]
|
||||
if not starts:
|
||||
return None
|
||||
items: list[dict[str, Any]] = []
|
||||
fields = ("id", "name", "quantity", "unit", "size", "aspect_ratio", "description")
|
||||
for index, start in enumerate(starts):
|
||||
chunk = text[start:starts[index + 1] if index + 1 < len(starts) else len(text)]
|
||||
item: dict[str, Any] = {}
|
||||
for field in fields[:-1]:
|
||||
match = re.search(rf'"{field}"\s*:\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|(-?\d+))', chunk, re.DOTALL)
|
||||
if not match:
|
||||
continue
|
||||
raw = match.group(1) if match.group(1) is not None else match.group(2)
|
||||
if field == "quantity":
|
||||
item[field] = int(raw)
|
||||
else:
|
||||
try:
|
||||
item[field] = json.loads(f'"{raw}"')
|
||||
except json.JSONDecodeError:
|
||||
item[field] = raw
|
||||
description_marker = re.search(r'"description"\s*:\s*"', chunk, re.DOTALL)
|
||||
if description_marker:
|
||||
tail = chunk[description_marker.end():]
|
||||
next_field = re.search(r',\s*"(?:id|name|quantity|unit|size|aspect_ratio)"\s*:', tail, re.DOTALL)
|
||||
if next_field:
|
||||
tail = tail[:next_field.start()]
|
||||
tail = tail.strip()
|
||||
# Description is the final accepted field. Remove only structural
|
||||
# suffixes; leave an incomplete final sentence intact.
|
||||
tail = re.sub(r'\s*[,}\]]+\s*$', "", tail)
|
||||
if tail.endswith('"'):
|
||||
tail = tail[:-1]
|
||||
item["description"] = tail.replace('\\n', '\n').replace('\\"', '"').strip()
|
||||
if item.get("id") and item.get("name"):
|
||||
items.append(item)
|
||||
return items or None
|
||||
|
||||
|
||||
def _repair_vlm_json(text: str) -> str | None:
|
||||
"""Repair only safe, common VLM JSON damage.
|
||||
|
||||
|
||||
@@ -41,6 +41,17 @@ class ClothingLayoutPlannerTests(unittest.TestCase):
|
||||
raw = inventory({"id": "hat", "name": "hat"}) + "\nHere is the inventory."
|
||||
self.assertEqual(PLANNER.normalize_inventory(raw)[0]["id"], "hat")
|
||||
|
||||
def test_salvages_schema_items_when_json_repair_cannot_parse(self):
|
||||
raw = '{"items":[{"id":"shirt","name":"shirt","quantity":1,"description":"blue shirt'
|
||||
original_repair = PLANNER._repair_vlm_json
|
||||
PLANNER._repair_vlm_json = lambda _text: '{broken'
|
||||
try:
|
||||
items = PLANNER.normalize_inventory(raw)
|
||||
finally:
|
||||
PLANNER._repair_vlm_json = original_repair
|
||||
self.assertEqual(items[0]["id"], "shirt")
|
||||
self.assertEqual(items[0]["description"], "blue shirt")
|
||||
|
||||
def test_quantities_and_pairs_create_deterministic_instances(self):
|
||||
plan = PLANNER.plan_layout(inventory(
|
||||
{"id": "sock", "name": "sock", "quantity": 3},
|
||||
|
||||
Reference in New Issue
Block a user