Skip to content

Commit 9784dd4

Browse files
Douglas Blankclaude
andcommitted
fix(admin): coerce datapoint values to float before count aggregation
Non-numeric or mixed-type value/y from Opik SPAN_COUNT and MPM prediction responses could raise TypeError on `counts[...] += ...` and abort collection. Add _as_float() to coerce datapoints (None/blank/non-numeric -> 0.0). Addresses baz-reviewer Type Inconsistency findings. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent dbd7a4f commit 9784dd4

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

cometx/cli/admin_growth_report.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ def _num(value):
121121
return value
122122

123123

124+
def _as_float(value):
125+
"""Coerce a collector datapoint value to a float, treating None/blank and
126+
any non-numeric value (e.g. a stray string from an SDK/REST response) as
127+
0.0 -- keeps count aggregation from crashing on mixed-type payloads."""
128+
if value is None or value == "":
129+
return 0.0
130+
try:
131+
return float(value)
132+
except (TypeError, ValueError):
133+
return 0.0
134+
135+
124136
def _all_time_counts(events, units) -> dict:
125137
"""Bucket `events` by creation time-key, with NO window filtering --
126138
charts always render all-time (Option-A: the window is a shaded band
@@ -1014,9 +1026,9 @@ def _collect_opik(self, workspaces):
10141026
counts: dict = defaultdict(float)
10151027
for result in resp.results or []:
10161028
for dp in result.data or []:
1017-
counts[format_time_key(dp.time, self.units)] += (
1018-
dp.value or 0
1019-
)
1029+
counts[
1030+
format_time_key(dp.time, self.units)
1031+
] += _as_float(dp.value)
10201032

10211033
usage.append(
10221034
UsageMetric(
@@ -1250,7 +1262,7 @@ def _mpm_prediction_counts(self, points):
12501262
t = self._mpm_point_time(x)
12511263
except Exception:
12521264
continue
1253-
counts[format_time_key(t, self.units)] += y or 0
1265+
counts[format_time_key(t, self.units)] += _as_float(y)
12541266
return dict(counts)
12551267

12561268
def _em_experiment_counts(self, experiments):

0 commit comments

Comments
 (0)