Skip to content

Commit 022aad4

Browse files
committed
fix(compiler): don't write raw JSON body on empty LLM content
In the parse-succeeded branch of _gen_create/_gen_update/_gen_entity_create/ _gen_entity_update, fall back to "" instead of the raw JSON string when the content field is empty/null. _require_nonempty_content then raises and the page is dropped, rather than writing the JSON envelope as the markdown body. The parse-FAILED (except) branch keeps content=raw as the legitimate non-JSON fallback.
1 parent 3d7c842 commit 022aad4

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

openkb/agent/compiler.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,10 +1479,13 @@ async def _gen_create(concept: dict) -> tuple[str, str, bool, str]:
14791479
try:
14801480
parsed = _parse_json(raw)
14811481
brief = parsed.get("brief", "")
1482-
# ``or raw``: ``.get("content", raw)`` returns None for
1483-
# ``{"content": null}`` (legal under json_object mode).
1484-
content = parsed.get("content") or raw
1482+
# Parse succeeded: do NOT fall back to ``raw`` (the JSON string).
1483+
# An empty/None ``content`` field yields "" so
1484+
# ``_require_nonempty_content`` raises and the page is skipped,
1485+
# rather than writing the raw JSON as the markdown body.
1486+
content = parsed.get("content") or ""
14851487
except (json.JSONDecodeError, ValueError):
1488+
# Parse FAILED: ``raw`` is the legitimate non-JSON body fallback.
14861489
brief, content = "", raw
14871490
_require_nonempty_content(content, name)
14881491
return name, content, False, brief
@@ -1514,8 +1517,10 @@ async def _gen_update(concept: dict) -> tuple[str, str, bool, str]:
15141517
try:
15151518
parsed = _parse_json(raw)
15161519
brief = parsed.get("brief", "")
1517-
content = parsed.get("content") or raw
1520+
# Parse succeeded: do NOT fall back to ``raw`` (the JSON string).
1521+
content = parsed.get("content") or ""
15181522
except (json.JSONDecodeError, ValueError):
1523+
# Parse FAILED: ``raw`` is the legitimate non-JSON body fallback.
15191524
brief, content = "", raw
15201525
_require_nonempty_content(content, name)
15211526
return name, content, True, brief
@@ -1538,8 +1543,10 @@ async def _gen_entity_create(ent: dict) -> tuple[str, str, str, str]:
15381543
parsed = _parse_json(raw)
15391544
brief = parsed.get("brief", "")
15401545
etype_out = parsed.get("type") if parsed.get("type") in _ENTITY_TYPES else etype
1541-
content = parsed.get("content") or raw
1546+
# Parse succeeded: do NOT fall back to ``raw`` (the JSON string).
1547+
content = parsed.get("content") or ""
15421548
except (json.JSONDecodeError, ValueError):
1549+
# Parse FAILED: ``raw`` is the legitimate non-JSON body fallback.
15431550
brief, etype_out, content = "", etype, raw
15441551
_require_nonempty_content(content, name)
15451552
return name, content, brief, etype_out
@@ -1573,8 +1580,10 @@ async def _gen_entity_update(ent: dict) -> tuple[str, str, str, str]:
15731580
parsed = _parse_json(raw)
15741581
brief = parsed.get("brief", "")
15751582
etype_out = parsed.get("type") if parsed.get("type") in _ENTITY_TYPES else etype
1576-
content = parsed.get("content") or raw
1583+
# Parse succeeded: do NOT fall back to ``raw`` (the JSON string).
1584+
content = parsed.get("content") or ""
15771585
except (json.JSONDecodeError, ValueError):
1586+
# Parse FAILED: ``raw`` is the legitimate non-JSON body fallback.
15781587
brief, etype_out, content = "", etype, raw
15791588
_require_nonempty_content(content, name)
15801589
return name, content, brief, etype_out

0 commit comments

Comments
 (0)