Skip to content

fix(errors): emit $exception_list in canonical order (caught-first, root cause last)#728

Draft
cat-ph wants to merge 4 commits into
mainfrom
cat/canonical-exception-list-order
Draft

fix(errors): emit $exception_list in canonical order (caught-first, root cause last)#728
cat-ph wants to merge 4 commits into
mainfrom
cat/canonical-exception-list-order

Conversation

@cat-ph

@cat-ph cat-ph commented Jul 3, 2026

Copy link
Copy Markdown

Problem

posthog-python is the only SDK that emits $exception_list with the root cause first. Every other SDK with exception-chain support puts the caught/outermost exception at index 0. exceptions_from_error_tuple() walked the chain in caught-first order and then called .reverse(), flipping it to root-cause-first.

This is the Python side of the cross-SDK exception-ordering standardization: PostHog/sdk-specs#11.

Canonical order: $exception_list[0] = the caught/outermost exception; each cause is appended after its wrapper in unwrap order; the root cause is last.

Change

  • Removed the .reverse() in exceptions_from_error_tuple() (posthog/exception_utils.py). Both build paths already produce the list in canonical order:
    • plain chained exceptions: walk_exception_chain yields caught-first, following __cause__ / __context__;
    • ExceptionGroup: exceptions_from_error keeps the group (parent) before its contained exceptions.
  • Added an explanatory comment documenting the canonical order and why we do not reverse.
  • Frame order within each stacktrace is untouched (already bottom-up / canonical).

No code in the SDK or the Django integration derives values from $exception_list positions (e.g. no issue-name/type/message picking off [0]), so there is no downstream code change needed in this repo.

Tests

Added regression tests in posthog/test/test_exception_capture.py:

  • explicit cause (raise B from A) -> asserts [0] is the caught B and the last entry is root cause A;
  • implicit context chain (exception raised while handling another) -> same ordering;
  • ExceptionGroup (3.11+) -> asserts the group is first, members follow.

uv run pytest posthog/test/test_exception_capture.py posthog/test/test_client.py -> 145 passed (re-run after rebase onto current main). Ruff lint + format clean.

Coordination

BREAKING wire-order change. The ordering of $exception_list on the wire flips. Merge/release only after the pipeline normalization gate (cymbal) is live, so the pipeline can normalize incoming order based on $lib_version. Shipping this as a MINOR release (changeset included) so the pipeline can gate on the version. This flip should be paired with fingerprint aliasing on the pipeline side, since exception/frame fingerprints are order-sensitive.

User-visible effects

Anything downstream that reads $exception_list[0] (issue fingerprinting / naming, primary exception type + message) will now see the caught/outermost exception instead of the root cause. That is the intended, canonical behavior and matches the other SDKs — but it is a visible change for existing Python-sourced issues.

@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "chore: use pypi/posthog changeset key" | Re-trigger Greptile

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-24 16:06:57 UTC
Duration: 338862ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 518ms
Endpoint And Method.Does Not Use Legacy Endpoints 1009ms
Required Headers.Has Authorization Bearer Header 1009ms
Required Headers.Has Content Type Json 1008ms
Required Headers.Has Posthog Sdk Info Format 1009ms
Required Headers.Has Posthog Attempt Header 1009ms
Required Headers.Has Posthog Request Id 1009ms
Required Headers.Has Posthog Request Timestamp 1009ms
Required Headers.Has User Agent 1009ms
Body Format.Body Has Created At And Batch 1009ms
Body Format.No Api Key In Body 1008ms
Body Format.No Sent At In Body 1009ms
Event Format.Event Has Required Root Fields 1009ms
Event Format.Event Uuid Is Valid 1009ms
Event Format.Event Timestamp Is Rfc3339 1009ms
Event Format.Distinct Id Is String 1008ms
Event Format.Distinct Id At Root Not Properties 1009ms
Event Format.Custom Properties Preserved 1009ms
Event Format.Set Properties Preserved 1009ms
Event Format.Set Once Properties Preserved 1009ms
Event Format.Groups Properties Preserved 1009ms
Event Format.Sdk Generates Uuid If Not Provided 1008ms
Event Format.Event Has Required Root Fields Batch 1013ms
Event Format.Event Uuid Is Valid Batch 1013ms
Event Format.Event Timestamp Is Rfc3339 Batch 1012ms
Event Format.Distinct Id Is String Batch 1013ms
Event Format.Distinct Id At Root Not Properties Batch 1012ms
Event Format.Custom Properties Preserved Batch 1012ms
Event Format.Set Properties Preserved Batch 1012ms
Event Format.Set Once Properties Preserved Batch 1012ms
Event Format.Groups Properties Preserved Batch 1012ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1012ms
Batch Behavior.Multiple Events In Single Batch 1507ms
Batch Behavior.Batch Envelope Smoke 1014ms
Batch Behavior.Flush With No Events Sends Nothing 1006ms
Batch Behavior.Flush At Triggers Batch 1508ms
Batch Behavior.Created At Reflects Batch Creation Time 1011ms
Deduplication.Generates Unique Uuids 1507ms
Deduplication.Different Events Same Content Different Uuids 1507ms
Deduplication.Preserves Uuid On Retry 7513ms
Deduplication.Preserves Timestamp On Retry 7511ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7517ms
Deduplication.No Duplicate Events In Batch 1504ms
Header Behavior On Retry.Attempt Header Starts At One 1008ms
Header Behavior On Retry.Attempt Header Increments On Retry 14517ms
Header Behavior On Retry.Request Id Preserved On Retry 7512ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3520ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7510ms
Response Format Validation.Success Response Has Uuid Keyed Results 1011ms
Response Format Validation.Success Response Has Ok For Each Event 1507ms
Response Format Validation.Success No Retry After When All Ok 1507ms
Response Format Validation.Success Retry After Present When Retry Events 2511ms
Response Format Validation.Success No Retry After When Drop Only 1507ms
Response Format Validation.Response Echoes Request Id 1010ms
Retry Behavior.Retries On 408 7511ms
Retry Behavior.Retries On 500 7518ms
Retry Behavior.Retries On 503 9515ms
Retry Behavior.Retries On 504 7509ms
Retry Behavior.Retryable Errors Have Retry After 4514ms
Retry Behavior.Respects Retry After On Retryable Error 12517ms
Retry Behavior.Does Not Retry On 400 3504ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Does Not Retry On 402 3509ms
Retry Behavior.Does Not Retry On 413 3508ms
Retry Behavior.Does Not Retry On 415 3508ms
Retry Behavior.Non Retryable Errors Have No Retry After 3508ms
Retry Behavior.Implements Backoff 23523ms
Retry Behavior.Max Retries Respected 23531ms
Partial Batch Handling.Handles 200 Full Success 3002ms
Partial Batch Handling.Handles 200 With All Ok 4509ms
Partial Batch Handling.Does Not Retry Dropped Events 4509ms
Partial Batch Handling.Does Not Retry Limited Events 4511ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7514ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7513ms
Partial Batch Handling.Retries Only Retry Events From Partial 7511ms
Partial Batch Handling.Partial Retry Preserves Uuids 7510ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7514ms
Partial Batch Handling.Partial Retry Request Id Preserved 7516ms
Partial Batch Handling.Respects Retry After On Partial 9511ms
Partial Batch Handling.Unknown Result Treated As Terminal 4507ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4507ms
Compression.Sends Gzip Content Encoding 1008ms
Compression.No Content Encoding When Disabled 1010ms
Compression.Compressed Body Is Decompressible 1008ms
Error Handling.Does Not Retry On Unknown 4Xx 3509ms
Event Options.Cookieless Mode Override 1008ms
Event Options.Disable Skew Correction Override 1009ms
Event Options.Process Person Profile Override 1008ms
Event Options.Product Tour Id Override 1009ms
Event Options.Unset Options Omitted 1009ms
Event Options.Options Override In Batch 1012ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1009ms
Geoip And Historical Migration.Historical Migration Set In Body 1008ms
Geoip And Historical Migration.Historical Migration Absent By Default 1008ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1006ms
Request Payload.Flags Request Uses V2 Query Param 1008ms
Request Payload.Flags Request Hits Flags Path Not Decide 1007ms
Request Payload.Flags Request Omits Authorization Header 1007ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1007ms
Request Payload.Groups Default To Empty Object 1007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1007ms
Request Payload.Disable Geoip Omitted Defaults To False 1006ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1006ms
Request Lifecycle.No Flags Request On Init Alone 503ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1011ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1002ms
Retry Behavior.Retries Flags On 502 1007ms
Retry Behavior.Retries Flags On 504 1007ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

@github-actions

Copy link
Copy Markdown
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@github-actions github-actions Bot added the stale label Jul 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed due to lack of activity. Feel free to reopen if it's still relevant.

@github-actions github-actions Bot closed this Jul 20, 2026
@cat-ph cat-ph reopened this Jul 24, 2026
cat-ph added 2 commits July 24, 2026 15:50
$exception_list[0] is now the caught/outermost exception, with each cause
appended after its wrapper in unwrap order and the root cause last.
Previously the list was reversed so the root cause came first, which is the
opposite of every other SDK. This aligns posthog-python with the cross-SDK
exception ordering spec (PostHog/sdk-specs#11).

Frame order within each stacktrace is unchanged (already bottom-up).

Adds regression tests for explicit cause, implicit context, and
ExceptionGroup ordering.
cat-ph added 2 commits July 24, 2026 18:49
…rder

The MCP integration and rate-limit type keying landed after this branch was written, both reading $exception_list[0] with root-cause-first assumptions. Rate limiting now keys on the outermost exception type (matching server-side issue naming under canonical order) and the fastmcp test asserts both canonical ends.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant