Skip to content

feat(flags): expose a flag's evaluation runtime through the SDK - #977

Draft
posthog[bot] wants to merge 2 commits into
mainfrom
posthog-self-driving/featflags-expose-evaluation-runtime-in-89758e
Draft

posthog[bot] wants to merge 2 commits into
mainfrom
posthog-self-driving/featflags-expose-evaluation-runtime-in-89758e

Conversation

@posthog

@posthog posthog Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

  • A customer proxying frontend flag requests through their own backend has to call /api/feature_flag/local_evaluation by hand to learn a flag's evaluation runtime. That endpoint is a private serializer contract, so they are pinned to a payload shape the team reserves the right to change.
  • The SDK already receives the value. _update_flag_state parks the raw local-evaluation dicts on client.feature_flags, and each dict carries evaluation_runtime — but that property is untyped, undocumented, and has no filter, so nothing about it is safe to rely on.
  • The gap was only ever on the SDK side: MinimalFeatureFlagSerializer has shipped the field for a while, and EvaluationFeatureFlagSerializer extends it for both SDK-facing shapes. No backend change is needed.

Note

This adds public API, so per CONTRIBUTING.md the shape needs a maintainer's agreement before it merges. It is a proposal — say the word and the naming or the semantics change. The two design calls worth a look are in "Agent context" below.

posthog-js#5050 mirrors the same surface in posthog-node. The naming and the semantics should match across the two SDKs, so it is worth reviewing them together.

💚 How did you test it?

What How
The new read path 13 cases in TestEvaluationRuntime (posthog/test/test_feature_flags.py): each runtime value, a definition with evaluation_runtime: null, a legacy definition with the key absent, an unknown flag key, a client with nothing loaded yet, and a bad runtime argument
The value survives a real fetch One case loads through load_feature_flags() from a mocked GetResponse, so the assertion runs against the fetch path rather than a hand-set client.feature_flags
End to end, no mocks Ran a throwaway http.server serving a local-evaluation body in the true EvaluationFeatureFlagSerializer shape, pointed a Posthog(host=...) at it, and read the runtimes back over a real HTTP request
CI-aligned checks ruff format --check, ruff check, mypy (clean, no new baseline entries), python -W error -c "import posthog", and pytest — 2738 passed, 1 skipped
Output of the end-to-end run
definitions loaded: ['web-banner', 'batch-job', 'shared-copy']
  web-banner: FeatureFlagEvaluationRuntime.CLIENT
  batch-job: FeatureFlagEvaluationRuntime.SERVER
  shared-copy: FeatureFlagEvaluationRuntime.ALL
  missing: None
client can evaluate: ['web-banner', 'shared-copy']
server can evaluate: ['batch-job', 'shared-copy']

No UI, so no demo. AsyncClient has no local-evaluation surface at all, so there was nothing to mirror there.

📝 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: Fully autonomous

Written by Claude Opus 5 running as a PostHog self-driving task, from an inbox report. No human drove the work, so the PR is left unassigned for the owning team to triage.

Two design calls a reviewer should weigh:

  • A missing runtime reports ALL rather than None. The column is nullable and older definitions omit the key, and all is the server-side default, so coercing keeps callers from writing the same fallback everywhere. None is reserved for "no definition loaded for this key", which is a genuinely different answer. The alternative — Optional all the way down — was rejected because it makes the filter method ambiguous about null runtimes.
  • ALL matches symmetrically. A flag set to all is returned for both CLIENT and SERVER, which is what the customer's use case needs in one call. The cost is that asking for ALL returns every loaded flag rather than only the flags configured as all; that is documented on the method. Exact-equality filtering was tried first and rejected as it forced the caller to union two calls.

Also considered and not done: a filter on get_all_flags() / evaluate_flags(). That changes evaluation behavior rather than adding a read path, which is a much larger surface to agree on, and the report asked for a read path.


Created with PostHog Desktop from this inbox report.

The local-evaluation payload already carries `evaluation_runtime` per flag, but
only on the untyped `client.feature_flags` dicts. There was no typed accessor, no
filter and no documentation, so a backend that serves flags to its own frontend
had to call `/api/feature_flag/local_evaluation` directly to read the value.

Adds `FeatureFlagEvaluationRuntime` and two read methods on the client, both
served from the definitions local evaluation already holds:

- `get_feature_flag_evaluation_runtime(key)`
- `get_feature_flag_keys_by_evaluation_runtime(runtime)`

A definition with no runtime reports `ALL`, the default PostHog applies, and a
flag set to `ALL` matches both the client and the server runtime.

Generated-By: PostHog Desktop
Task-Id: f3696295-459a-4066-bb52-5eea7ee16f37
@github-actions

github-actions Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-21 17:46:09 UTC
Duration: 256428ms

✅ 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 517ms
Endpoint And Method.Does Not Use Legacy Endpoints 510ms
Required Headers.Has Authorization Bearer Header 510ms
Required Headers.Has Content Type Json 511ms
Required Headers.Has Posthog Sdk Info Format 511ms
Required Headers.Has Posthog Attempt Header 510ms
Required Headers.Has Posthog Request Id 510ms
Required Headers.Has Posthog Request Timestamp 510ms
Required Headers.Has User Agent 510ms
Body Format.Body Has Created At And Batch 510ms
Body Format.No Api Key In Body 509ms
Body Format.No Sent At In Body 510ms
Event Format.Event Has Required Root Fields 510ms
Event Format.Event Uuid Is Valid 510ms
Event Format.Event Timestamp Is Rfc3339 510ms
Event Format.Distinct Id Is String 515ms
Event Format.Distinct Id At Root Not Properties 510ms
Event Format.Custom Properties Preserved 510ms
Event Format.Set Properties Preserved 510ms
Event Format.Set Once Properties Preserved 510ms
Event Format.Groups Properties Preserved 510ms
Event Format.Sdk Generates Uuid If Not Provided 510ms
Event Format.Event Has Required Root Fields Batch 513ms
Event Format.Event Uuid Is Valid Batch 514ms
Event Format.Event Timestamp Is Rfc3339 Batch 513ms
Event Format.Distinct Id Is String Batch 514ms
Event Format.Distinct Id At Root Not Properties Batch 513ms
Event Format.Custom Properties Preserved Batch 513ms
Event Format.Set Properties Preserved Batch 514ms
Event Format.Set Once Properties Preserved Batch 513ms
Event Format.Groups Properties Preserved Batch 513ms
Event Format.Sdk Generates Uuid If Not Provided Batch 514ms
Batch Behavior.Multiple Events In Single Batch 517ms
Batch Behavior.Batch Envelope Smoke 515ms
Batch Behavior.Flush With No Events Sends Nothing 506ms
Batch Behavior.Flush At Triggers Batch 1011ms
Batch Behavior.Created At Reflects Batch Creation Time 511ms
Deduplication.Generates Unique Uuids 517ms
Deduplication.Different Events Same Content Different Uuids 513ms
Deduplication.Preserves Uuid On Retry 6518ms
Deduplication.Preserves Timestamp On Retry 6519ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6523ms
Deduplication.No Duplicate Events In Batch 518ms
Header Behavior On Retry.Attempt Header Starts At One 511ms
Header Behavior On Retry.Attempt Header Increments On Retry 13522ms
Header Behavior On Retry.Request Id Preserved On Retry 6520ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3020ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6516ms
Response Format Validation.Success Response Has Uuid Keyed Results 511ms
Response Format Validation.Success Response Has Ok For Each Event 513ms
Response Format Validation.Success No Retry After When All Ok 512ms
Response Format Validation.Success Retry After Present When Retry Events 1516ms
Response Format Validation.Success No Retry After When Drop Only 513ms
Response Format Validation.Response Echoes Request Id 510ms
Retry Behavior.Retries On 408 6519ms
Retry Behavior.Retries On 500 6520ms
Retry Behavior.Retries On 503 8522ms
Retry Behavior.Retries On 504 6519ms
Retry Behavior.Retryable Errors Have Retry After 3516ms
Retry Behavior.Respects Retry After On Retryable Error 11523ms
Retry Behavior.Does Not Retry On 400 2512ms
Retry Behavior.Does Not Retry On 401 2514ms
Retry Behavior.Does Not Retry On 402 2511ms
Retry Behavior.Does Not Retry On 413 2513ms
Retry Behavior.Does Not Retry On 415 2514ms
Retry Behavior.Non Retryable Errors Have No Retry After 2515ms
Retry Behavior.Implements Backoff 22535ms
Retry Behavior.Max Retries Respected 22536ms
Partial Batch Handling.Handles 200 Full Success 2512ms
Partial Batch Handling.Handles 200 With All Ok 3515ms
Partial Batch Handling.Does Not Retry Dropped Events 3516ms
Partial Batch Handling.Does Not Retry Limited Events 3517ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6522ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6517ms
Partial Batch Handling.Retries Only Retry Events From Partial 6523ms
Partial Batch Handling.Partial Retry Preserves Uuids 6521ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6518ms
Partial Batch Handling.Partial Retry Request Id Preserved 6517ms
Partial Batch Handling.Respects Retry After On Partial 8521ms
Partial Batch Handling.Unknown Result Treated As Terminal 3515ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3521ms
Compression.Sends Gzip Content Encoding 511ms
Compression.No Content Encoding When Disabled 510ms
Compression.Compressed Body Is Decompressible 510ms
Error Handling.Does Not Retry On Unknown 4Xx 2512ms
Event Options.Cookieless Mode Override 510ms
Event Options.Disable Skew Correction Override 510ms
Event Options.Process Person Profile Override 510ms
Event Options.Product Tour Id Override 512ms
Event Options.Unset Options Omitted 510ms
Event Options.Options Override In Batch 514ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 512ms
Geoip And Historical Migration.Historical Migration Set In Body 510ms
Geoip And Historical Migration.Historical Migration Absent By Default 511ms

Feature_Flags Tests

17/17 tests passed

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

Both read methods now go through `feature_flags_by_key`, which drops the
redundant missing-key guard. The dict is built by iterating the definition list,
so load order is unchanged.

Generated-By: PostHog Desktop
Task-Id: f3696295-459a-4066-bb52-5eea7ee16f37
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