From d12b47f8743a0ce29f4bb7806ddc558b49f421d5 Mon Sep 17 00:00:00 2001 From: KoalaTrapLord <265133236+KoalaTrapLord@users.noreply.github.com> Date: Mon, 30 Mar 2026 12:26:28 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20robust=20suggest=5Fcards=20parsing=20?= =?UTF-8?q?=E2=80=94=20CSV=20fallback=20+=20one-shot=20JSON=20prompt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add _parse_card_list_fallback() to recover plain-text CSV output from the LLM when json_mode fails to enforce JSON (e.g. qwen2.5:7b drift) - Harden suggest_cards system prompt with a one-shot JSON example so small models are less likely to return prose in the first place - Log recovered names at WARNING level so the degradation is visible without being silent (previously returned [] with no recovery attempt) - No behaviour changes to any other function --- .../deck_builder/api/ollama_client.py | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/commander_ai_lab/deck_builder/api/ollama_client.py b/src/commander_ai_lab/deck_builder/api/ollama_client.py index bb0f9c3..78ff8f5 100644 --- a/src/commander_ai_lab/deck_builder/api/ollama_client.py +++ b/src/commander_ai_lab/deck_builder/api/ollama_client.py @@ -76,6 +76,26 @@ def _extract_json(raw: str) -> Any: raise json.JSONDecodeError("No valid JSON found in response", text, 0) +def _parse_card_list_fallback(raw: str) -> List[str]: + """Last-resort parser for when the LLM ignores json_mode and returns + a plain comma-separated (or newline-separated) list of card names. + + Strips common junk characters, splits on commas or newlines, and + returns non-empty tokens. Returns an empty list if nothing useful + is found so the caller can decide how to handle it. + """ + # Remove JSON brackets/quotes/backticks/numbering artefacts + cleaned = re.sub(r"[\[\]{}\"`]", "", raw) + # Remove leading bullet / numbering like "1." or "-" + cleaned = re.sub(r"(?m)^\s*[-*\d]+[.)]\s*", "", cleaned) + # Split on commas or newlines + tokens = re.split(r"[,\n]+", cleaned) + names = [t.strip().strip("'\"") for t in tokens if t.strip()] + # Filter out tokens that look like prose (>6 words) or are empty + names = [n for n in names if n and len(n.split()) <= 6] + return names + + def chat( messages: List[Dict[str, str]], model: str = DEFAULT_MODEL, @@ -185,8 +205,16 @@ Only suggest cards that are legal in Commander and match the color identity. Return ONLY a JSON array of card name strings, like: ["Card A", "Card B", ...]""" + # One-shot example in the system prompt anchors small models to JSON output messages = [ - {"role": "system", "content": "You are an expert MTG Commander deck builder. Respond with ONLY a JSON array of strings. No other text."}, + { + "role": "system", + "content": ( + "You are an expert MTG Commander deck builder. " + "You MUST respond with ONLY a JSON array of strings — nothing else. " + 'Example of the required format: ["Sol Ring", "Arcane Signet", "Lightning Greaves"]' + ), + }, {"role": "user", "content": prompt}, ] raw = chat(messages, json_mode=True) @@ -206,7 +234,26 @@ Return ONLY a JSON array of card name strings, like: ["Card A", "Card B", ...]"" return [str(c) for c in result] return [] except json.JSONDecodeError: - logger.error("Failed to parse suggestion response: %s", raw[:500]) + # JSON parse failed — attempt plain-text CSV recovery before giving up + logger.warning( + "suggest_cards: JSON parse failed, attempting CSV fallback. " + "Raw (first 300 chars): %s", + raw[:300], + ) + recovered = _parse_card_list_fallback(raw) + if recovered: + logger.warning( + "suggest_cards: CSV fallback recovered %d name(s) for category '%s'", + len(recovered), + category, + ) + return recovered + logger.error( + "suggest_cards: CSV fallback also failed for category '%s'. " + "Returning empty list. Full raw: %s", + category, + raw[:500], + ) return []