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
+2 -4
View File
@@ -190,10 +190,8 @@ def part_of(frag: dict) -> str:
def parse_stop_table(out_text: str) -> bool:
"""True when the extractor's output names this table as not relevant for
the question. The extractor may write STOP_TABLE for a fragment's table
after seeing its identity (caption/header) — fixed code then skips all
remaining fragments of THAT table (never other tables)."""
"""LEGACY textual STOP_TABLE detector — kept only for tests. Production
code reads the typed `stop: bool` field of ExtractSignature."""
return "STOP_TABLE" in (out_text or "").upper()[:80]