Use yaml.safe_load for frontmatter parsing in generate_content (#151)
The naive line-by-line parser broke when the LLM output multi-line YAML lists (e.g., tags with '- item' syntax instead of inline [item1, item2]). Replaced with yaml.safe_load which handles all valid YAML formats. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Juan Manuel Servera committed
May 21, 2026 at 14:24 UTC
a95cf25b2e73ae198240757034752ff77f7c4744
1 file changed
+8
-8
scripts/generate_content.py
+8
-8
@@ -5,6 +5,8 @@ import csv
5
import re
6
from pathlib import Path
7
8
+import yaml
9
+
10
from scripts.topic_paths import analyzed_dir
11
12
FRONTMATTER_PATTERN = re.compile(r"^---\n(.*?)\n---\n(.*)\Z", re.DOTALL)
@@ -102,14 +104,12 @@ def parse_frontmatter(document: str) -> tuple[dict[str, object], str]:
104
raise GenerationError("Summary is missing YAML frontmatter.")
105
106
frontmatter_text, body = match.groups()
105
- frontmatter: dict[str, object] = {}
106
- for line in frontmatter_text.splitlines():
107
- if not line.strip():
108
- continue
109
- if ":" not in line:
110
- raise GenerationError(f"Malformed frontmatter line: {line}")
111
- key, raw_value = line.split(":", 1)
112
- frontmatter[key.strip()] = parse_scalar(raw_value)
107
+ try:
108
+ frontmatter = yaml.safe_load(frontmatter_text)
109
+ except yaml.YAMLError as exc:
110
+ raise GenerationError(f"Invalid YAML frontmatter: {exc}") from exc
111
+ if not isinstance(frontmatter, dict):
112
+ raise GenerationError("Frontmatter must be a YAML mapping.")
113
114
missing = REQUIRED_ANALYSIS_FIELDS.difference(frontmatter)
115
if missing: