Skip to content

fix: send flag id and version on locally evaluated $feature_flag_called - #1149

Draft
andehen wants to merge 1 commit into
mainfrom
fix/local-eval-feature-flag-id
Draft

andehen wants to merge 1 commit into
mainfrom
fix/local-eval-feature-flag-id

Conversation

@andehen

@andehen andehen commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

$feature_flag_called events for locally evaluated flags had no $feature_flag_id and no $feature_flag_version. _capture_feature_flag_called read both only from the remote /flags response metadata (flag_details.metadata), and flag_details is None when the value came from local evaluation. This affected get_feature_flag(), feature_enabled() and get_feature_flag_result(), and the get_feature_flag_payload(..., send_feature_flag_events=True) path that goes through the same code.

The single-flag path now passes the id and version of the local flag definition into _capture_feature_flag_called. It reads the definition from the same definitions snapshot that produced the value, instead of re-reading feature_flags_by_key. has_experiment now also comes from that snapshot.

evaluate_flags() already set $feature_flag_id for locally evaluated flags, but always set version=None because of a comment saying local definitions do not carry a version. The local evaluation definitions do include version, so the snapshot record now takes it from the definition.

The async client does not evaluate locally, so it does not change.

💚 How did you test it?

  • TestCaptureCalls.test_locally_evaluated_flag_called_event_carries_definition_id_and_version in posthog/test/test_feature_flags.py, parametrized over get_feature_flag, feature_enabled and get_feature_flag_result. A local definition with id 4211 and version 7 must produce an event with both. All 3 fail on main (KeyError: '$feature_flag_id').
  • TestEvaluateFlagsLocalFlagIdentity.test_flag_called_event_carries_definition_id_and_version in posthog/test/test_evaluate_flags.py covers the same for evaluate_flags(), and checks that a definition without version sends no $feature_flag_version. It fails on main (KeyError: '$feature_flag_version').
  • Existing exact-shape assertions on locally evaluated events in test_feature_flags.py, test_feature_flag_result.py and test_feature_flag_called_minimization.py now include $feature_flag_id. $feature_flag_id was already in the minimal-event allowlist.
  • pytest posthog/test: 5829 passed, 25 skipped, 3 failed. The 3 failures are the forked-child tests in test_metrics_autocapture.py. They fail the same way on a clean main checkout in a full run and pass when that file runs alone.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed. (No docs change needed.)
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Changeset file added: .sampo/changesets/local-flag-called-id-version.md (pypi/posthog: patch).

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code wrote the change and the tests for the PR author, who directed the work. It found every path that sends $feature_flag_called for a locally evaluated flag, then wrote tests that fail on main before it made the fix. It passes the local id and version as explicit keyword arguments rather than building a synthetic FeatureFlag, so the remote metadata handling stays the same.

🤖 Generated with Claude Code

Locally evaluated flags in get_feature_flag(), feature_enabled() and
get_feature_flag_result() sent $feature_flag_called without
$feature_flag_id or $feature_flag_version, because both were read only
from remote /flags metadata. Read them from the local flag definition
that produced the value. evaluate_flags() already sent the id for local
flags but dropped the definition's version; send it too.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-10-29T15:20:18.463590+00:00
Duration: 259294ms

✅ All Tests Passed!

121/121 tests passed


Capture_V1 Tests

✅ 95/95 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 514ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 510ms
Required Headers.Has Authorization Bearer Header ✅ 508ms
Required Headers.Has Content Type Json ✅ 508ms
Required Headers.Has Posthog Sdk Info Format ✅ 508ms
Required Headers.Has Posthog Attempt Header ✅ 508ms
Required Headers.Has Posthog Request Id ✅ 508ms
Required Headers.Has Posthog Request Timestamp ✅ 508ms
Required Headers.Has User Agent ✅ 509ms
Body Format.Body Has Created At And Batch ✅ 509ms
Body Format.No Api Key In Body ✅ 508ms
Body Format.No Sent At In Body ✅ 508ms
Event Format.Event Has Required Root Fields ✅ 510ms
Event Format.Event Uuid Is Valid ✅ 508ms
Event Format.Event Timestamp Is Rfc3339 ✅ 508ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 518ms
Event Format.Distinct Id Is String ✅ 509ms
Event Format.Distinct Id At Root Not Properties ✅ 508ms
Event Format.Custom Properties Preserved ✅ 508ms
Event Format.Set Properties Preserved ✅ 508ms
Event Format.Set Once Properties Preserved ✅ 508ms
Event Format.Groups Properties Preserved ✅ 508ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 508ms
Event Format.Event Has Required Root Fields Batch ✅ 511ms
Event Format.Event Uuid Is Valid Batch ✅ 511ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 510ms
Event Format.Distinct Id Is String Batch ✅ 510ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 511ms
Event Format.Custom Properties Preserved Batch ✅ 510ms
Event Format.Set Properties Preserved Batch ✅ 511ms
Event Format.Set Once Properties Preserved Batch ✅ 510ms
Event Format.Groups Properties Preserved Batch ✅ 511ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 511ms
Batch Behavior.Multiple Events In Single Batch ✅ 513ms
Batch Behavior.Batch Envelope Smoke ✅ 511ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 509ms
Batch Behavior.Flush At Triggers Batch ✅ 1012ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 508ms
Deduplication.Generates Unique Uuids ✅ 513ms
Deduplication.Different Events Same Content Different Uuids ✅ 511ms
Deduplication.Preserves Uuid On Retry ✅ 6516ms
Deduplication.Preserves Timestamp On Retry ✅ 6517ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 6520ms
Deduplication.No Duplicate Events In Batch ✅ 513ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 508ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 13519ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 6513ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 3017ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 6514ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 509ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 512ms
Response Format Validation.Success No Retry After When All Ok ✅ 510ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 1512ms
Response Format Validation.Success No Retry After When Drop Only ✅ 510ms
Response Format Validation.Response Echoes Request Id ✅ 508ms
Retry Behavior.Retries On 408 ✅ 6513ms
Retry Behavior.Retries On 500 ✅ 6513ms
Retry Behavior.Retries On 503 ✅ 8520ms
Retry Behavior.Retries On 504 ✅ 6518ms
Retry Behavior.Retryable Errors Have Retry After ✅ 3512ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 11521ms
Retry Behavior.Does Not Retry On 400 ✅ 2511ms
Retry Behavior.Does Not Retry On 401 ✅ 2512ms
Retry Behavior.Does Not Retry On 402 ✅ 2511ms
Retry Behavior.Does Not Retry On 413 ✅ 2512ms
Retry Behavior.Does Not Retry On 415 ✅ 2512ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2511ms
Retry Behavior.Implements Backoff ✅ 22521ms
Retry Behavior.Max Retries Respected ✅ 22534ms
Partial Batch Handling.Handles 200 Full Success ✅ 2511ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3515ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3510ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3514ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 6518ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 6518ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 6515ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 6518ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 6519ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 6514ms
Partial Batch Handling.Respects Retry After On Partial ✅ 8518ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3514ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3515ms
Compression.Sends Gzip Content Encoding ✅ 509ms
Compression.No Content Encoding When Disabled ✅ 508ms
Compression.Compressed Body Is Decompressible ✅ 509ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2510ms
Event Options.Cookieless Mode Override ✅ 509ms
Event Options.Disable Skew Correction Override ✅ 508ms
Event Options.Process Person Profile Override ✅ 508ms
Event Options.Product Tour Id Override ✅ 508ms
Event Options.Unset Options Omitted ✅ 509ms
Event Options.Options Override In Batch ✅ 510ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 508ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 508ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 508ms

Capture_Ai Tests

✅ 5/5 tests passed

View Details
Test Status Duration
Routing.Capture Ai Posts To Ai Endpoint ✅ 508ms
Routing.Capture Does Not Reroute Ai Named Events ✅ 507ms
Identity.Capture Ai Event Has Uuid ✅ 508ms
Identity.Capture Ai Keeps Supplied Uuid ✅ 509ms
Timestamp Format.Non Utc Event Timestamp Is Converted To Utc ✅ 508ms

Feature_Flags Tests

✅ 17/17 tests passed

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

Feature_Flags_Local_Evaluation Tests

✅ 4/4 tests passed

View Details
Test Status Duration
Versioned Boolean Matching.Matching Version Missing ✅ 48ms
Versioned Boolean Matching.Matching Version 1 ✅ 45ms
Versioned Boolean Matching.Matching Version 2 ✅ 46ms
Versioned Boolean Matching.Version Only Reload 1 2 1 2 Missing ✅ 25ms

andehen added a commit that referenced this pull request Oct 30, 2026
Typed details of a locally evaluated flag left the flag version out of
flag_metadata. The local flag definition carries the version, but the
evaluate_flags() snapshot record set it to None. The record now takes the
version from the definition, and the details add it to flag_metadata when it
is present. The record change is the same one #1149
makes, so $feature_flag_called for a snapshot read of a locally evaluated
flag also carries $feature_flag_version.

This branch has not been deployed

No deployments
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.

1 participant