Skip to content

fix: send non-finite float properties as null instead of invalid JSON - #1084

Closed
toby-m121 wants to merge 1 commit into
PostHog:mainfrom
toby-m121:fix/non-finite-float-json
Closed

toby-m121 wants to merge 1 commit into
PostHog:mainfrom
toby-m121:fix/non-finite-float-json

Conversation

@toby-m121

Copy link
Copy Markdown

💡 Motivation and Context

Closes #1078.

Event properties containing non-finite floats (inf, -inf, nan) were serialized with json.dumps' default allow_nan=True, producing the bare tokens Infinity/NaN. Those aren't valid JSON, so strict parsers downstream reject the whole batch and every event in it is lost.

This change makes utils.clean() coerce non-finite floats to None (sent as null), matching posthog-js, where JSON.stringify does the same. I went with coercion over raising because raising would drop the caller's entire event over one bad property.

  • clean() returns None for any float that isn't math.isfinite.
  • The Decimal branch now runs its converted float back through clean(). Previously it returned float(item) directly, so Decimal("Infinity") would skip the new check and still serialize as Infinity.

Since clean() already recurses through dicts, lists, tuples, sets, dataclasses and pydantic models, nested values are covered too.

💚 How did you test it?

  • Added test_clean_non_finite_floats (parameterized: inf, -inf, nan, Decimal("Infinity"), plus finite float/Decimal passthrough) and test_clean_non_finite_floats_nested.
  • Ran the reproduction from the issue against clean() + json.dumps(..., cls=DatetimeSerializer), including a nested Decimal("Infinity"): the payload is now {"score": null, "ratio": null, "ok": 1.5, ...} and passes a strict json.loads(..., parse_constant=...) that rejects Infinity/NaN.
  • Full suite: pytest --timeout=30 → 4706 passed, 16 skipped.
  • ruff format --check ., ruff check ., the mypy baseline check, and python -W error -c "import posthog" all pass.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

@toby-m121
toby-m121 requested a review from a team as a code owner October 19, 2026 11:18
@toby-m121 toby-m121 closed this by deleting the head repository Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Event properties with inf/nan floats produce invalid JSON payloads that strict-parsing ingestion rejects

1 participant