Treat a non-finite token count as unrecorded

`float("inf") > 0` is true, so a stored `"inf"` was preserved as if it
were a real count and reached the UI as "∞". Require a finite positive
value, which also covers `nan` explicitly rather than by accident.
This commit is contained in:
arc53-machine committed 2026-09-22 14:01:27 +01:00
1 parent 3954de9b9e
commit db8db62c93
2 files changed
+12 -5

No files matched your search

+8 -4
View File
@@ -1,5 +1,7 @@
"""Source document management chunk management."""
import math
from flask import current_app, jsonify, make_response, request
from flask_restx import fields, Namespace, Resource
@@ -48,22 +50,24 @@ def _has_usable_token_count(metadata: dict) -> bool:
Stores round-trip metadata differently -- pgvector keeps JSON types, the
Mongo backend can hand back strings -- so a numeric string counts as
recorded. Anything else (missing, empty, non-numeric, zero or negative)
does not.
recorded. Anything else (missing, empty, non-numeric, zero, negative, or
non-finite) does not: ``float("inf")`` is greater than zero but is not a
number of tokens, and it reaches ``toLocaleString`` in the UI as "∞".
Args:
metadata: A chunk's metadata mapping.
Returns:
True when ``token_count`` holds a positive number.
True when ``token_count`` holds a finite positive number.
"""
raw = metadata.get("token_count")
if isinstance(raw, bool) or not isinstance(raw, (int, float, str)):
return False
try:
return float(raw) > 0
value = float(raw)
except (TypeError, ValueError):
return False
return math.isfinite(value) and value > 0
def _with_token_counts(chunks: list) -> list:
+4 -1
View File
@@ -199,7 +199,10 @@ class TestGetChunks:
# An already-recorded count is left alone, whatever tokenizer produced it.
assert chunks[2]["metadata"]["token_count"] == 42
@pytest.mark.parametrize("stored", ["", 0, -1, "abc", None, {"n": 1}])
@pytest.mark.parametrize(
"stored",
["", 0, -1, "abc", None, {"n": 1}, "inf", "-inf", "nan", float("inf")],
)
def test_recomputes_unusable_token_count(self, app, pg_conn, stored):
from docsgpt.api.user.sources.chunks import GetChunks