Keep LLM-written text free of numbers your code didn't compute: fact placeholders, a strict validator, one retry, then drop the sentence
If an LLM writes a wrong number confidently, users stop trusting every other number on the screen. factguard enforces one rule: code computes every number, the LLM only interprets.
It works with any LLM and any SDK, and has no dependencies. The design comes from a production AI analyst for marketing teams; the write-up is Stopping an LLM from making up numbers in an AI analyst.
pip install factguard Quick start from datetime import date from factguard import Facts, guard facts = Facts(currency="INR") facts.add("spend", 61_240, "money", label="Ad spend this week") facts.add("roas", 1.8, "ratio", label="ROAS this week") facts.add("roas_prev", 2.4, "ratio", label="ROAS last week") system_prompt = facts.prompt() # the rules, plus "- {{fact:spend}} = INR 61.2K (...)" def generate(retry): # Call your LLM here. On a retry, show it retry.draft and retry.feedback. ... week = (date(2026, 9, 22), date(2026, 9, 28)) result = guard(generate, facts, windows=[week], user_text="Why did ROAS drop?") result.text # "ROAS fell from 2.4x to 1.8x while spend held at INR 61.2K." result.dropped # sentences removed because they still broke the rules result.attempts # 1 or 2 examples/anthropic_analyst.py is a complete example using Claude.
Without windows, every date and duration fails. That is deliberate: the model shouldn't do date arithmetic. Resolve "last month" in code and pass the window you actually queried. Pass both windows when you compare two periods.
Formats: money renders as INR 61.2K or $1.2M (pass Formatter(compact=False) for 61,240). percent takes fractions, so 0.034 renders as 3.4%. count renders as 12,480, ratio as 2.3x, and text as-is. To use your own formatting, pass any Callable[[Fact], str] as formatter=.
validate(text, facts, **rules) returns a Report with .ok, .violations (kind, token, start, end) and .feedback, a retry message that names each offending token.
guard(generate, facts, retries=1, **rules) runs the whole loop. generate(None) should return a first draft, and generate(retry) a corrected one. Use aguard for async code.
StreamGuard(facts, **rules) checks streamed text one sentence at a time. A sentence already on screen can't be rewritten, so a failing sentence is dropped instead of retried:
sg = StreamGuard(facts, windows=[week]) for chunk in llm_stream: for sentence in sg.feed(chunk): ui.append(sentence) for sentence in sg.close(): ui.append(sentence) Evals. Check every recorded answer for zero ungrounded numbers, not just for the right answer:
from factguard.testing import assert_grounded def test_recorded_answer(): assert_grounded(answer, facts, windows=[week]) Or from the shell or CI:
factguard check answer.txt --facts facts.json --window 2026-09-22:2026-09-28 factguard render answer.txt --facts facts.json Trade-offs The prose gets stiffer. Without "nearly double" or "about a third", the model writes more literal sentences. Some sentences get dropped, and a retry adds latency. If users have to trust the numbers before the interpretation is worth anything, that trade is worth it.
Placeholders don't stop the model from citing the wrong fact, so keep fact labels clear and test with recorded questions. And number_words is a heuristic: it is off by default because words like "half" are often harmless.
See CONTRIBUTING.md. Bug reports with a sentence that was wrongly flagged or wrongly let through are especially useful.
All 2 files in this release have provenance metadata.
ai-agents analytics grounding guardrails hallucination llm
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.
All 2 files in this release have been uploaded with provenance metadata.
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.




