knowledge: typed Fact extraction via dspy native structured output

His direction: stop explaining string formats to the LLM and parsing
them back with Python — that's exactly what dspy does natively and
better. The pipe format ('subject | relation | object | quote') was
fragile (run-L verbose values, pipe-in-quote ambiguity, STOP_TABLE
string protocol).

THE CHANGE (extractor only, PR-10 scope):
- Fact(BaseModel) with subject/relation/object/quote OutputFields
- ExtractSignature.facts: list[Fact] — the adapter renders the JSON
  schema into the prompt itself (verified in a live run's prompt log)
  and parses responses back into typed Fact objects
- STOP_TABLE string protocol -> stop: bool OutputField
- expert.py consumes typed objects; the cand dict is built at the
  check_candidate boundary only (checks.py unchanged)
- parse_facts/parse_stop_table kept as LEGACY for existing tests only

VERIFIED:
- dspy 3.4.1 pattern E2E'd first with a raw-text engine: list[Fact]
  parses back as Fact objects, schema rendered in prompt (tmp probes)
- full suite 12/12 green (74 tests) — tablefrags.py updated to script
  typed Fact objects
- live E2E run (ollama-cloud glm-5.3-flash, Italy rivers): correct
  Po/Adige/Tiber answer; prompt log shows the native JSON schema;
  table rows ingested cleanly (22 rows, quotes verbatim)

NEXT (his direction, not this PR): apply the same typed-output law to
ALL floop signatures — INIT subquestions, ROUTE engine choice,
AnswerFromKG, SelectTable, SkimPromote, Translate, VerifyCount.
This commit is contained in:
2026-10-22 14:21:13 +02:00
parent 5c97fbf50a
commit 109885773c
4 changed files with 44 additions and 29 deletions
+9 -5
View File
@@ -24,9 +24,9 @@ import dspy
from .checks import check_candidate, quote_containment
from .chunker import chunk_page
from .skim import find_quantities, skim_article
from .extract import (AnswerFromKG, ExtractSignature, SelectTableSignature,
from .extract import (AnswerFromKG, ExtractSignature, Fact, SelectTableSignature,
SkimPromoteSignature, TranslateSignature,
VerifyCountSignature, parse_facts)
VerifyCountSignature)
from .store import KnowledgeStore, norm_name
from .wiki import (WikipediaClient, WikiPage, WikiTable, resolve_convert,
tables_as_chunks, format_table_frag, parse_stop_table)
@@ -964,8 +964,10 @@ class KnowledgeExpert:
if charge_llm:
charge_llm()
raw = self.extract(question=question, heading=chunk.heading_path, text=chunk.text)
for cand in parse_facts(raw.facts):
for cand in (raw.facts or []):
self._checked += 1
cand = {"subject": cand.subject, "relation": cand.relation,
"object": cand.object, "quote": cand.quote}
ok, reason = check_candidate(cand, page.text)
if not ok:
self._log(f"candidate rejected ({reason}): {cand.get('subject', '')[:40]}")
@@ -1001,17 +1003,19 @@ class KnowledgeExpert:
charge_llm()
heading = f"== Tables == (table {frag['table_idx']}, part {frag['part']}/{frag['of']})"
raw = self.extract(question=question, heading=heading, text=text)
if parse_stop_table(raw.facts):
if raw.stop:
stopped.add(frag["table_idx"])
self._log(f"extractor STOP_TABLE: table {frag['table_idx']} "
f"({frag['caption'][:40]!r}) not relevant — "
f"skipping {frag['of'] - frag['part']} remaining parts",
think=True)
continue
for cand in parse_facts(raw.facts):
for cand in (raw.facts or []):
self._checked += 1
# containment against prose + FULL table text: the fragment is
# rendered from the same rows, so quotes verify honestly
cand = {"subject": cand.subject, "relation": cand.relation,
"object": cand.object, "quote": cand.quote}
ok, reason = check_candidate(cand, containment)
if not ok:
self._log(f"frag candidate rejected ({reason}): "