Skip to content

fix: Validate custom event UUIDs#679

Merged
marandaneto merged 3 commits into
mainfrom
fix/validate-event-uuid-153
Jun 19, 2026
Merged

fix: Validate custom event UUIDs#679
marandaneto merged 3 commits into
mainfrom
fix/validate-event-uuid-153

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

Fixes #153.

Invalid custom event UUIDs were previously passed through to ingestion, which could surface as a generic backend 400 response. This validates user-provided event UUIDs client-side so invalid values fail before upload with a descriptive error/log message, while valid UUID strings and uuid.UUID objects remain supported.

💚 How did you test it?

  • uv run --extra test pytest -q posthog/test/test_client.py
  • uv run --extra dev ruff check posthog/client.py posthog/args.py posthog/test/test_client.py
  • uv run pytest -q posthog/test/test_client.py

📝 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)

A coding agent implemented the focused fix requested for issue #153 in an isolated worktree. The change is intentionally scoped to client-side UUID validation before events are queued or uploaded; it preserves generated UUID behavior when no UUID is supplied and accepts both valid UUID strings and uuid.UUID values.

@marandaneto marandaneto self-assigned this Jun 19, 2026
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
posthog/client.py:120-124
The error message in the falsy-string branch doesn't include the actual value, while the second branch does. When `uuid=""` is passed, the caller sees a generic message with no indication of what was supplied, making it harder to debug than the `"not-a-uuid"` case.

```suggestion
    stringified = stringify_id(value)
    if not stringified:
        raise ValueError(
            f"Invalid event uuid {value!r}. Expected a valid UUID string or uuid.UUID instance."
        )
```

### Issue 2 of 2
posthog/test/test_client.py:234-263
Per the team's review standards, parameterised tests are always preferred. `test_capture_with_invalid_uuid_logs_and_does_not_send` and `test_capture_with_invalid_uuid_raises_in_debug` each exercise only a single invalid input (`"not-a-uuid"`). A `@parameterized.expand` over a range of bad values (e.g. `""`, `"not-a-uuid"`, `"1234"`, `123`) would give more coverage and follows the existing pattern already used elsewhere in this file.

Reviews (1): Last reviewed commit: "fix: validate custom event UUIDs" | Re-trigger Greptile

Comment thread posthog/client.py
Comment thread posthog/test/test_client.py Outdated
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-19 12:53:44 UTC
Duration: 540109ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 516ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10008ms
Format Validation.Custom Properties Preserved 10007ms
Format Validation.Event Has Timestamp 10008ms
Retry Behavior.Retries On 503 18024ms
Retry Behavior.Does Not Retry On 400 12004ms
Retry Behavior.Does Not Retry On 401 10008ms
Retry Behavior.Respects Retry After Header 16013ms
Retry Behavior.Implements Backoff 32016ms
Retry Behavior.Retries On 500 16014ms
Retry Behavior.Retries On 502 16011ms
Retry Behavior.Retries On 504 16010ms
Retry Behavior.Max Retries Respected 32024ms
Deduplication.Generates Unique Uuids 9996ms
Deduplication.Preserves Uuid On Retry 16013ms
Deduplication.Preserves Uuid And Timestamp On Retry 23021ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16005ms
Deduplication.No Duplicate Events In Batch 10002ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12010ms
Error Handling.Does Not Retry On 413 10007ms
Error Handling.Retries On 408 14013ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9502ms
Request Payload.Flags Request Uses V2 Query Param 10007ms
Request Payload.Flags Request Hits Flags Path Not Decide 10007ms
Request Payload.Flags Request Omits Authorization Header 10008ms
Request Payload.Token In Flags Body Matches Init 10007ms
Request Payload.Groups Round Trip 10006ms
Request Payload.Groups Default To Empty Object 10008ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10007ms
Request Payload.Disable Geoip Omitted Defaults To False 10007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10006ms
Request Lifecycle.No Flags Request On Init Alone 5004ms
Request Lifecycle.No Flags Request On Normal Capture 10508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9511ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10509ms

@marandaneto marandaneto force-pushed the fix/validate-event-uuid-153 branch from a823e3a to 1e284ba Compare June 19, 2026 11:12
Comment thread posthog/client.py Outdated
@marandaneto marandaneto marked this pull request as ready for review June 19, 2026 12:45
@marandaneto marandaneto requested a review from a team as a code owner June 19, 2026 12:46
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
posthog/args.py:65-67
The phrase "ignored" implies silent behavior, but invalid UUIDs are actually logged at ERROR level (`self.log.error(...)`) before the fallback UUID is generated. ERROR-level logs can trigger production alerting. This same wording appears in `__init__.py` (three times) and `client.py` (five docstrings). Replacing "ignored" with "trigger an error log and" more accurately describes what callers can expect.

```suggestion
            UUID is returned, so you can correlate it with actions in your app. If provided,
            it must be a valid UUID string or uuid.UUID instance; invalid values trigger an
            error log and are replaced with a newly generated UUID.
```

### Issue 2 of 2
posthog/client.py:1117-1124
The `group_identify` signature was updated to `Optional[Union[str, UUID]]` in this PR, but the `alias` method's `uuid` parameter is still untyped. Since the docstring for `alias.uuid` was updated to document the same UUID validation behavior, the type annotation should match for consistency.

```suggestion
    def alias(
        self,
        previous_id: str,
        distinct_id: Optional[str],
        timestamp=None,
        uuid: Optional[Union[str, UUID]] = None,
        disable_geoip=None,
    ):
```

Reviews (2): Last reviewed commit: "docs: document custom UUID fallback beha..." | Re-trigger Greptile

@marandaneto marandaneto merged commit 143d488 into main Jun 19, 2026
32 checks passed
@marandaneto marandaneto deleted the fix/validate-event-uuid-153 branch June 19, 2026 14:29
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.

Non-descriptive error for incorrect uuid

2 participants