Certus: understanding LLM certainty
Certus allows you to estimate confidence in a LLM response, both as a whole and in each part. It does this by parsing the log-probabilities from your response into a tree of nodes.
We build this tree from an ordered collection of certus.nodes.core.Token instances and gathering them up recursively
into a tree matching the structure of the response. Each Token is considered a leaf node in the tree, and higher-up
nodes in the tree are of other types.
Installation
The most convenient way to install Certus is to do so from PyPI:
python -m pip install certus
Developers
If you are planning to do some development work on Certus, please install the package from source and use uv:
git clone https://github.com/daffidwilde/certus
cd certus
uv sync --dev
Usage
Extracting token nodes from a response
To map your LLM response to the collection of leaf nodes, use the certus.interface module:
>>> import certus as ct
>>> from google.genai import types
>>>
>>> data = "certus"
>>> logprobs = types.LogprobsResult( # taken from `response.candidates[0].logprobsResult`
... chosen_candidates=[
... types.LogprobsResultCandidate(log_probability=0.0, token='"', token_id=24),
... types.LogprobsResultCandidate(log_probability=-0.0123, token="certus", token_id=42),
... types.LogprobsResultCandidate(log_probability=0.0, token='"', token_id=24),
... ]
... )
>>> tokens = ct.interface.from_google(logprobs)
>>> tokens
[Token(value='"', logprob=0.0, start=0), Token(value='certus', logprob=-0.0123, start=1), Token(value='"', logprob=0.0, start=7)]
This list of token nodes is ready to be parsed into a tree.
Building a tree
Consider this piece of JSON-friendly data:
>>> import certus as ct
>>>
>>> data = {
... "name": "Henry Wilde",
... "age": 29,
... "longest_walk_km": 160.9,
... "pets": [
... {
... "name": "Billie",
... "species": "cat",
... "favourite_foods": [
... "fish",
... "oat milk",
... {
... "name": "chicken",
... "preparation": "boiled",
... "when_sick": True,
... },
... ],
... },
... ],
... }
>>>
Let's say this data came from a gpt-4o response. We can tokenise this dictionary using tiktoken and simulate some
log-probabilities to go with them. From there, we can create a collection of Token leaf nodes ready for parsing;
details to do this are hidden below.
Simulating data tokens
>>> import json
>>> import random
>>>
>>> import tiktoken
>>>
>>> def tokenise_string(string: str, encoder: tiktoken.Encoding) -> list[str]:
... encoded = encoder.encode(string)
... return [encoder.decode_single_token_bytes(e).decode() for e in encoded]
>>>
>>> encoder = tiktoken.encoding_for_model("gpt-4o")
>>> data_tokenised = tokenise_string(json.dumps(data), encoder)
>>>
>>> random.seed(0)
>>> tokens, position = [], 0
>>> for t in data_tokenised:
... tokens.append(ct.nodes.Token(t, -round(random.expovariate(1e4), 6), position))
... position += len(t)
>>>
>>> assert json.loads("".join(t.value for t in tokens)) == data
>>>
Now, we can parse this dictionary response and token nodes into a single Object node using the
certus.parsers.parse_json() function:
>>> parsed = ct.parsers.parse_json(data, tokens)
>>> parsed # doctest:+SKIP
Object(
fields={
'name': Composite(children=[Token(value=' "', logprob=-3e-05, start=8), Token(value='Henry', logprob=-7.2e-05, start=10), Token(value=' Wilde', logprob=-5.2e-05, start=15), Token(value='",', logprob=-0.000153, start=21)]),
'age': Token(value='29', logprob=-7e-05, start=31),
'longest_walk_km': Composite(children=[Token(value='160', logprob=-0.000131, start=54), Token(value='.', logprob=-0.000229, start=57), Token(value='9', logprob=-0.000115, start=58)]),
'pets': Array(
elements=[
Object(
fields={
'name': Composite(children=[Token(value=' "', logprob=-0.0002, start=78), Token(value='Bill', logprob=-3e-05, start=80), Token(value='ie', logprob=-0.000163, start=84), Token(value='",', logprob=-8e-05, start=86)]),
'species': Composite(children=[Token(value=' "', logprob=-0.000174, start=99), Token(value='cat', logprob=-0.00011, start=101), Token(value='",', logprob=-0.0, start=104)]),
'favourite_foods': Array(
elements=[
Composite(children=[Token(value=' ["', logprob=-8.4e-05, start=125), Token(value='fish', logprob=-2.7e-05, start=128), Token(value='",', logprob=-0.000343, start=132)]),
Composite(children=[Token(value=' "', logprob=-0.000163, start=134), Token(value='o', logprob=-5.9e-05, start=136), Token(value='at', logprob=-8e-06, start=137), Token(value=' milk', logprob=-3.9e-05, start=139), Token(value='",', logprob=-7.1e-05, start=144)]),
Object(
fields={
'name': Composite(children=[Token(value=' "', logprob=-0.000123, start=155), Token(value='ch', logprob=-7.9e-05, start=157), Token(value='icken', logprob=-0.000168, start=159), Token(value='",', logprob=-7.8e-05, start=164)]),
'preparation': Composite(children=[Token(value=' "', logprob=-9.1e-05, start=181), Token(value='bo', logprob=-4.9e-05, start=183), Token(value='iled', logprob=-8.6e-05, start=185), Token(value='",', logprob=-3.4e-05, start=189)]),
'when_sick': Token(value=' true', logprob=-9e-06, start=204)
}
)
]
)
}
)
]
)
}
)
That's a lot of information, but you should be able to see a few node types here:
certus.nodes.core.Composite: a collection ofTokennodescertus.nodes.struct.Array: a collection of node elements, which behaves like alistcertus.nodes.struct.Object: a mapping of keys to nodes, which behaves like adict
We can leverage the list/dict-like properties of our Object node to look at the confidence in its various
components:
>>> parsed.confidence # the whole response
0.9999025047529705
>>> for key, value in parsed.items():
... print(key.ljust(16), value.confidence)
name 0.9999232529452059
age 0.9999300024499428
longest_walk_km 0.9998416792007273
pets 0.9999055044649844
>>>
>>> parsed["pets"][0]["favourite_foods"][-1]["name"].confidence # Billie's last favourite food
0.9998880062717659
Metadata
Release files for certus 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| certus-0.0.2.tar.gz | 129.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| certus-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 140.5 kB
Release files / certus-0.0.2.tar.gz
| Download URL | certus-0.0.2.tar.gz |
|---|---|
| Size | 129.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0705ffa43819e42308c5f15168b7e1c9f1f3c45a46b79cd51233de658fbc969e
|
|
BLAKE2b-256 checksum How to use checksums |
99f74520a38c67a3c68c9ec238f80185d83bfe4d5add245986bdb120af25838e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.15
|
Release files / certus-0.0.2-py3-none-any.whl
| Download URL | certus-0.0.2-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60cc4185afe26de511f2fd487fff1d7953a7f79ad49666dad8962f2e13675fe9
|
|
BLAKE2b-256 checksum How to use checksums |
bf02a3ec8b7dc9e1c8c6ae50f1591d4a7fad56692b6c2ce90958842cc5949363
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.15
|
Security for certus 0.0.2
Provenance
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.