fix: sanitize None values from event properties before queueing - #926
Closed
hariom123-dev wants to merge 1 commit into
Closed
fix: sanitize None values from event properties before queueing#926hariom123-dev wants to merge 1 commit into
hariom123-dev wants to merge 1 commit into
Conversation
Member
|
edit: its ok to pass none but we'll strip those during serialization so not really worth passing them |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
Adds defensive sanitization to the message queueing flow to strip
Nonevalues from eventpropertiesbefore they are batched and sent over the network.🔍 Context & Motivation
When developers pass dynamic property dictionaries into
posthog.capture()(e.g., pulling data from an external API or scraper), keys withNonevalues can easily slip in.Currently, these null values are queued, serialized into JSON, and sent to the PostHog API. This causes two minor issues:
{ "optional_field": null }across the wire consumes unnecessary bandwidth and inflates the size of the event payload.🛠️ Changes Implemented
Added a lightweight dictionary comprehension in
posthog/client.pyduring the internal message preparation phase. It checks ifmsg.get("properties")is a dictionary, and if so, it filters out any keys where the value is explicitlyNoneright before themsgis cleaned and appended to the batch.Note: Empty strings,
0, andFalseare preserved, as they are often valid analytics values.✅ Verification
test_capture_sanitizes_none_propertiesinposthog/test/test_client.py. It simulates aclient.capturecall with both valid andNoneproperties, mocking the network request to assert the batched payload only retains the valid keys.📉 Impact