Skip to content

feat(flags): typed flag value accessors - #1147

Draft
andehen wants to merge 47 commits into
mainfrom
feat/flags-typed-accessors
Draft

andehen wants to merge 47 commits into
mainfrom
feat/flags-typed-accessors

Conversation

@andehen

@andehen andehen commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Since 7.83.0 (#1031) the SDK reads the /flags?v=4 response and keeps each flag's typed value, reason and metadata, but has no accessors for reading them. Callers still cannot read a number or object flag value as such, or see why a flag returned what it did.

Changes

The typed accessor API, split out of #1031 so that change could ship without new public methods:

  • get_boolean_value, get_string_value, get_number_value, get_integer_value, get_float_value and get_object_value on the evaluate_flags() snapshot, each with a _details form.
  • Every typed accessor, in both forms, takes the keyword-only send_feature_flag_events option, with the same meaning as on is_enabled() and get_flag() (feat: add a per-read send_feature_flag_events option to snapshot reads #1148). With send_feature_flag_events=False, the read sends no $feature_flag_called and leaves the dedupe cache alone, so a later read that sends events still reports the flag. The key still counts as accessed for only_accessed(). The value form passes the option to its details form.
  • FlagEvaluationDetails, FlagEvaluationReason and FlagEvaluationErrorCode, with OpenFeature reasons and error codes. The accessors never coerce: they return the caller default when the flag is missing, failed, unreadable, has no value, or has a value of another type.
  • A read of the wrong type (TYPE_MISMATCH, any of the 12 methods, either client) returns the caller default with the same details as before, but sends no $feature_flag_called and takes no dedupe slot. With feat(flags): send $experiment_exposure for v2 experiment splits #1154 that also means no $experiment_exposure. The next read of the right type, or a get_flag() or is_enabled() read, sends the event as a first read. The client logs the details' error_message as an error once per (flag key, requested type), in a cache bounded like the flag-event dedupe cache. The read still counts as accessed for only_accessed(), as before. A false or null value read through a non-boolean accessor, missing, failed and unreadable flags, and send_feature_flag_events=False keep their behaviour.
  • Numbers are binary64. get_number_value/get_number_details return an integer beyond 2**53 - 1 in magnitude as the nearest float, so Python returns what JavaScript does; an integer within range stays an int. get_float_value returns every number as the nearest float. An integer beyond the range of a double reads as an infinity, with its sign, through both. The integer accessors keep TYPE_MISMATCH beyond 2**53 - 1.
  • Details of a locally evaluated flag carry the local definition's id and version in flag_metadata, the same values the snapshot record has taken from the definition since fix: send flag id and version on locally evaluated $feature_flag_called #1149.
  • error_code and error_message on FeatureFlag, including the cause for a flag local evaluation could not resolve.
  • Makes public the fields 7.83.0 keeps private until this API ships: value and failed on FeatureFlag, and config_version, rule_type, rule_id, variant_key and forced_variant on FlagMetadata.

This waits for the cross-SDK accessor spec in PostHog/sdk-specs#104. Names and semantics may change to match it. #1031 and #1148 are on main, so the diff here is only this change.

Testing

  • test_flags_v4_response.py, for each of the 12 typed methods on the sync and the async client:
    • With send_feature_flag_events=False, the read returns the flag's value, sends no $feature_flag_called and marks the key in only_accessed(). A later read with the default then sends exactly one event.
    • With the default, a read sends exactly one event, and a repeated read with send_feature_flag_events=True sends no second one.
    • Mutation check: these tests fail if the shared resolver ignores the option, if one value form does not pass it to its details form, or if one details form does not pass it to the resolver.
  • Type mismatches, for each of the 12 typed methods on the sync and the async client: a read of the wrong type returns the default with TYPE_MISMATCH, sends no $feature_flag_called, still marks the key in only_accessed(), and logs its error_message once although read twice. A later right-type read (value and details form), get_flag() or is_enabled() then sends exactly one event, on both clients. Two wrong types on one key log twice, a second snapshot from the same client logs nothing new, a read with send_feature_flag_events=False logs nothing new, and another client logs again. A null or false value read as another type, and a missing, failed or unreadable flag, still send their event and log nothing.
  • Numbers: the number accessor at ±(253 − 1) returns that int, and at ±253 and ±(253 + 1) the float ±253. The integer accessor returns TYPE_MISMATCH at ±253 and ±(253 + 1), and the float accessor returns the nearest float at all six. ±10**400 reads as ±infinity through the number and float accessors.
  • test_locally_evaluated_details_carry_the_definition_version: local boolean (value and details forms) and multivariate details carry the definition's version, a definition without one leaves it out, and the $feature_flag_called event reports the same version. The test fails without either half of the change.
  • test_flags_released_behaviour.py: results recorded with 7.82.1 still match once the fields this PR makes public are removed from each flag and its metadata. A separate test checks those fields' values, including PARSE_ERROR for an unreadable record and GENERAL for one the server marked failed.
  • Checked before both PRs were brought up to date with main: with feat(flags): send $experiment_exposure for v2 experiment splits #1154 merged in locally (not pushed), a typed read with send_feature_flag_events=False on a v2 experiment split sends neither $feature_flag_called nor $experiment_exposure, as docs(flags): specify SDK-sent $experiment_exposure for config version 2 flags sdk-specs#162 asks. A later default read sends each event once. This holds for all 12 methods on both clients. The flag test modules also pass on the combined tree.
  • With feat(flags): send $experiment_exposure for v2 experiment splits #1154 merged in locally at 119a4c6 (not pushed), a typed read of the wrong type on a v2 experiment split sends neither $feature_flag_called nor $experiment_exposure, for each typed method on both clients, and the next right-type or get_flag()/is_enabled() read sends both. The flag test modules passed on the combined tree. The current heads of this PR and feat(flags): send $experiment_exposure for v2 experiment splits #1154 conflict only in test_flags_released_behaviour.py, where each adjusts the 7.82.1 comparison for its own change; whichever lands second combines both.
  • pytest posthog/test: 6,253 passed, 25 skipped, with test_metrics_autocapture.py deselected (its forked-child tests crash on macOS in a full run, on main too, and pass alone).
  • ruff format --check, ruff check, mypy (baseline filter) and the public API snapshot check pass.

🤖 Generated with Claude Code

andehen added 12 commits October 7, 2026 10:33
Request /flags?v=3 from the sync and async clients and keep each flag's
typed value, structured reason and metadata on the parsed record.

- A record with a value member is a v3 record. A record without one comes
  from an older server: its value is variant, or enabled without one, and
  its config version is 1.
- A known field with the wrong JSON type fails only that record with
  PARSE_ERROR; unknown fields are ignored.
- Legacy getters, callbacks and $feature_flag_called keep reading the
  legacy rendering, now derived from value in one place: a string is the
  variant, null is disabled, and a number or object is enabled with the
  value as a JSON-encoded payload.
- evaluate_flags() snapshots gain get_boolean/string/number/object_value
  and _details accessors. They never coerce and return the caller default
  for missing, failed, null and mismatched values.
A v3 record for a flag in the new format carries no experiment ID: an
experiment is linked to its rule on the server, by rule ID. A complete
experiment split is config version 2, reason experiment_split, rule type
experiment, a rule ID and a variant key.

- Remove experiment_id from FlagMetadata. An experiment_id key in a record
  is now ignored like any other unknown metadata key, whatever its type.
- The vendored harness 1.13.1 fixtures still carry the field. Two reader
  cases that depend on it are overridden in the test until the fixtures are
  re-pinned to a contract without it.
A v3 record carries no holdout ID: holdouts in the new format are local to
their rule and have no ID, and older records never sent one. A holdout
result is identified by its rule ID and the reason holdout.

- Remove holdout_id from FlagMetadata. A holdout_id key in a record is now
  ignored like any other unknown metadata key, whatever its type.
- The vendored harness 1.13.1 fixtures still send the field. No reader case
  depends on it; the producer-response test skips it, like experiment_id,
  until the fixtures are re-pinned to a contract without it.
Clients already send v=3 and arbitrary v values, so the server selects
the typed record only for an exact v=4. Request v=4 and name the record
after it in tests, docstrings and the changeset. A server that answers
v=4 with the older record (enabled/variant, no value) is still read the
same way.
Align the evaluate_flags() typed accessors with the typed flag values
API proposed in PostHog/sdk-specs#104.

- FlagEvaluationDetails has an OpenFeature reason (FlagEvaluationReason)
  and a flag_metadata dict of the record's id, version, config_version,
  reason_code, condition_index, rule_type, rule_id and variant_key. It no
  longer carries the structured reason or the metadata object, so the
  payload stays on the payload accessors.
- variant is set only when the accessor returns the flag's value.
- Locally evaluated flags read with config_version 1 and a reason derived
  from the value, or DISABLED for an inactive flag.
- Each client logs once when /flags sends records without typed values,
  and a TYPE_MISMATCH message says why the value was not returned.
- Add get_integer_value/details and get_float_value/details. Object values
  are returned as copies.
- Re-vendor the rules v2 contract fixtures from posthog-sdk-test-harness
  1.22.0 (contract 7.0.0). A config version 1 record may carry an integer
  metadata.experiment_id, so its type is checked; it is not kept.
The adapter sends /flags?v=4, so it declares flags_response_v4. A harness
that knows the capability runs flags_request_uses_v4_query_param in place
of the ?v=2 check; older harness releases ignore it.
# Conflicts:
#	posthog/client.py
#	posthog/feature_flag_evaluations.py
Keep reading the /flags?v=4 response and retaining each flag's typed value,
reason and metadata, with the existing getters derived from the value. Drop
the public typed accessors (get_boolean_value/_details and the string,
number, integer, float and object forms), FlagEvaluationDetails,
FlagEvaluationReason and FlagEvaluationErrorCode, and the error code and
message fields on FeatureFlag that only they read. They return in a
separate change once the cross-SDK accessor API is agreed.
Add get_boolean_value, get_string_value, get_number_value,
get_integer_value, get_float_value and get_object_value to the
evaluate_flags() snapshot, each with a _details form that returns a
FlagEvaluationDetails with an OpenFeature reason, error_code and
flag_metadata. The accessors never coerce: they return the caller default
when the flag is missing, failed, has no value, or has a value of another
type.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed 3f8c315: 2 Should fix.

This is the only automatic review while this pull request is a draft. Comment @posthog review to review new changes sooner. Otherwise, a review of the new changes runs when you mark it ready for review.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

Comment thread posthog/types.py Outdated
Comment on lines +611 to +613
"object": lambda value: (
copy.deepcopy(value) if isinstance(value, dict) else _NOT_ACCEPTED
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Handle recursion failures when copying object flag values

A valid JSON object nested 600 levels deep passes FeatureFlag.from_json, but copy.deepcopy exceeds Python's default recursion limit. Both get_object_value and get_object_details then propagate RecursionError into application code instead of returning a safe result, defeating the accessor's fallback behavior. Use an iterative copy or handle copy failures with the caller default.

Comment thread posthog/types.py
Comment on lines +745 to +747
id=metadata.id,
version=metadata.version,
config_version=metadata.config_version,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Preserve absent metadata fields in evaluation details

When a typed record omits metadata.id or metadata.version, or sets either to null, parsing replaces it with 0. _resolve_flag_details then exposes that fabricated value in flag_metadata, violating the documented presence contract and giving consumers an identifier or version the server never supplied. Preserve field absence through parsing and omit those entries.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-11-03T13:13:58.861124+00:00
Duration: 260104ms

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

Capture_Ai Tests

✅ 5/5 tests passed

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

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 V4 Query Param ✅ 10ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 10ms
Request Payload.Flags Request Omits Authorization Header ✅ 9ms
Request Payload.Token In Flags Body Matches Init ✅ 9ms
Request Payload.Groups Round Trip ✅ 10ms
Request Payload.Groups Default To Empty Object ✅ 9ms
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 ✅ 10ms
Request Lifecycle.No Flags Request On Init Alone ✅ 3ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 510ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 13ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 10ms
Retry Behavior.Retries Flags On 502 ✅ 312ms
Retry Behavior.Retries Flags On 504 ✅ 314ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 512ms

Feature_Flags_Local_Evaluation Tests

✅ 4/4 tests passed

View Details
Test Status Duration
Versioned Boolean Matching.Matching Version Missing ✅ 68ms
Versioned Boolean Matching.Matching Version 1 ✅ 69ms
Versioned Boolean Matching.Matching Version 2 ✅ 68ms
Versioned Boolean Matching.Version Only Reload 1 2 1 2 Missing ✅ 39ms

@andehen andehen mentioned this pull request Oct 29, 2026
5 tasks done
The integer accessors reject numbers beyond 2**53 - 1 in magnitude, such
as 1e21, so every SDK returns the same result for the same flag. A test
also pins that a throwing before_send does not break a typed read.
A record without a typed value now has its known fields type-checked like a
/flags?v=4 record, so a wrong-typed field fails only that flag instead of the
whole response. A key member of the wrong type fails its record; the map key
stays the flag key. An unreadable record keeps the field that failed it, apart
from a record the server marks failed.
A record without metadata.id or metadata.version no longer reads them as 0.
Events already left out a falsy ID and version, so their properties do not
change.
A /flags?v=4 record with a number or object value exposes that value as its
payload, even when the record also sends a payload. Other payloads are kept as
sent.
# Conflicts:
#	posthog/test/test_flags_v4_response.py
#	posthog/types.py
#	references/public_api_snapshot.txt
The object accessors copy the value so a caller cannot change the snapshot. A
value nested deeply enough made that copy raise RecursionError; it now returns
the caller default with PARSE_ERROR, so a typed read never throws.
A value nested too deeply for json.dumps used to abort the whole
response. Also test an empty-string reason, and the async client with
unreadable and sparse records beside a healthy one. The changeset now
says which results can differ from before.
…t/flags-typed-accessors

# Conflicts:
#	posthog/test/test_flags_v4_response.py
get_flags_decision() returns FeatureFlag and FlagMetadata, so public fields
could be adopted before the typed flag API is settled. The typed accessors
make them public.
#1031 keeps the v4 value and rule metadata fields private. The typed
accessors make them public, so this branch keeps them public and the
changeset lists them.
…d accessors

Every typed value and details accessor on the evaluate_flags() snapshot now
takes the keyword-only send_feature_flag_events option that is_enabled() and
get_flag() take. With send_feature_flag_events=False the read sends no
$feature_flag_called and leaves the dedupe cache alone, so a later read that
sends events still reports the flag. The key still counts as accessed for
only_accessed(). The value form passes the option to its details form, so
both forms behave the same.
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.
…ccessor

Numbers are IEEE-754 doubles in every PostHog SDK. get_number_value and
get_number_details returned an integer beyond 2**53 - 1 in magnitude
exactly, so Python could return a value JavaScript cannot. They now
return the nearest float. Integers within range keep their int type,
the integer accessors keep TYPE_MISMATCH beyond the range, and the
float accessors are unchanged. An integer too large for any double is
a TYPE_MISMATCH, as in the float accessor.
A typed accessor that resolves to TYPE_MISMATCH returned the caller's
default and still sent $feature_flag_called, which took the dedupe slot,
so the later read of the right type sent nothing. A read of the wrong
type is a bug in the calling code, not a use of the flag. It now sends
no event and takes no dedupe slot, so the next read of the right type,
or a get_flag() or is_enabled() read, reports the flag.

The client logs the error_message once per flag key and requested
type, in a cache bounded like the flag-event dedupe. The read still
counts as accessed for only_accessed(). A false or null value read as
another type, missing, failed and unreadable flags, and reads with
send_feature_flag_events=False are unchanged.
The float accessor returned TYPE_MISMATCH for an integer too large to
convert to a float, and the number accessor followed it. JavaScript
parses such a number as an infinity, the nearest IEEE-754 double. Both
accessors now return that infinity, with its sign, so every PostHog SDK
reads the same value. The integer accessors keep TYPE_MISMATCH.
… state

Move the client's record of logged type mismatches next to
_logged_untyped_flags_response, the other once-per-client flag log
state, in both clients.
…ields

The typed value, failure flags and rule metadata were declared as dataclass
fields, so repr, equality, hashing, dataclasses.asdict and the generated SDK
reference showed them. Keep them as instance attributes with class-level
defaults instead, import FeatureFlag privately in the snapshot module, and
say in the changeset that the details stay internal.
A /flags record with a known field of the wrong type reads as disabled.
The single-flag getter cached that result, replacing the last good value,
so a later failed request fell back to False. An unreadable record now
leaves the fallback cache alone and falls back to its value, like a failed
request does; without a cached value it still reads as disabled.
FeatureFlag.from_json delegated to record parsers that always built a
FeatureFlag, so a subclass calling it got a base instance. The parsers
now take the class to build, for v4, older and unreadable records alike.
A snapshot kept each remotely evaluated flag twice: a flattened record and
the parsed /flags record, with the payload both as its JSON text and
decoded. The snapshot record now reads its fields from the parsed record
and decodes the payload on its first read, then keeps it. Private record
details that equal their defaults are no longer stored on each instance.

Retained memory of evaluate_flags() for a /flags?v=4 response of 5,000
boolean flags (tracemalloc, Python 3.14, including JSON decoding):

  no payloads:              3.64 MiB -> 2.87 MiB
  1 KiB JSON payload each: 28.10 MiB -> 7.95 MiB

A payload that is read is decoded and kept as before, so reading every
payload retains about what it did before this change.
The parity tests compare this SDK's v2 and v4 readers with each other, so
a change shared by both passes. These tests compare both with results
recorded once with posthog 7.82.1 for the same flags: the legacy getters,
get_flags_decision, snapshot reads, $feature_flag_called properties and
fallback-cache histories. Where a result differs from 7.82.1 by decision,
the test says why: an absent flag ID or version reads as None, not 0, and
a record with a field of the wrong type fails only itself.
A single-flag read of a record that cannot be read used to fail the whole
request, so its $feature_flag_called event carried
$feature_flag_error: unknown_error and no request id or evaluated-at. The
read now succeeds for the other records, but this one still reports the
same error and no request details, so a value served from the fallback
cache stays distinguishable from a fresh one.
Base automatically changed from feat/flags-v3-response to main November 3, 2026 12:59
…t/flags-typed-accessors

# Conflicts:
#	posthog/feature_flag_evaluations.py
#	posthog/test/test_feature_flag.py
#	posthog/test/test_flags_v4_response.py
#	posthog/types.py
#	references/public_api_snapshot.txt
…ssors

# Conflicts:
#	posthog/async_client.py
#	posthog/client.py
#	posthog/feature_flag_evaluations.py
#	posthog/test/test_flags_released_behaviour.py
#	posthog/test/test_flags_v4_response.py
#	posthog/types.py
#	references/public_api_snapshot.txt

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