Fragile JSON extraction with silent fallback in prompt_to_card_pipeline.py #9

Open
opened 2026-03-19 17:30:28 +00:00 by llabeyrie · 0 comments
Owner

Description

_extract_json_from_output() in prompt_to_card_pipeline.py (lines 56-81) has fragile parsing logic:

try:
    parsed = json.loads(stripped)
    ...
except json.JSONDecodeError:
    pass                          # silently ignored!

# Fallback: find last { ... } substring
last_open = stripped.rfind("{")
last_close = stripped.rfind("}")

Problems

  1. The except json.JSONDecodeError: pass silently discards the actual parse error
  2. The fallback uses naive rfind("{") / rfind("}") which breaks on nested JSON or JSON containing } in string values
  3. Debug print statements on stdout (issue #3) will corrupt the output this function tries to parse — these two bugs compound each other
  4. No logging of which parsing path succeeded

Fix

  • Log the JSONDecodeError before falling back
  • Use a more robust extraction method (e.g., regex for top-level JSON objects)
  • Ensure stdout is clean of debug prints before parsing (depends on issue #3)
## Description `_extract_json_from_output()` in `prompt_to_card_pipeline.py` (lines 56-81) has fragile parsing logic: ```python try: parsed = json.loads(stripped) ... except json.JSONDecodeError: pass # silently ignored! # Fallback: find last { ... } substring last_open = stripped.rfind("{") last_close = stripped.rfind("}") ``` ### Problems 1. The `except json.JSONDecodeError: pass` silently discards the actual parse error 2. The fallback uses naive `rfind("{")` / `rfind("}")` which breaks on nested JSON or JSON containing `}` in string values 3. Debug print statements on stdout (issue #3) will corrupt the output this function tries to parse — **these two bugs compound each other** 4. No logging of which parsing path succeeded ### Fix - Log the JSONDecodeError before falling back - Use a more robust extraction method (e.g., regex for top-level JSON objects) - Ensure stdout is clean of debug prints before parsing (depends on issue #3)
llabeyrie added the bugpriority: medium labels 2026-03-19 17:31:44 +00:00
Sign in to join this conversation.