Skip to content

Conversation

dmarticus
Copy link
Contributor

@dmarticus dmarticus commented Jul 28, 2025

Summary

This PR adds support for filtering feature flags by specific keys when using send_feature_flags in the Python SDK, matching the functionality recently added to the Node.js SDK in
PostHog/posthog-js-lite#576.

Changes

  • Added flag_keys parameter to SendFeatureFlagsOptions TypedDict
  • Updated get_all_flags and get_all_flags_and_payloads methods to accept and pass through flag_keys
  • Modified _get_all_flags_and_payloads_locally to filter flags by the provided keys during local evaluation
  • Added filtering for remote evaluation results when flag_keys is specified
  • Updated the _parse_send_feature_flags method to handle the new parameter

Usage

posthog.capture(
    "event-with-specific-flags",
    distinct_id="user123",
    properties={"foo": "bar"},
    send_feature_flags={
        "only_evaluate_locally": True,
        "flag_keys": ["my-important-flag", "another-flag"],  # Only evaluate these two flags
        "person_properties": {"plan": "premium"},
    },
)

Testing

Added comprehensive test coverage for:

  • Local evaluation with flag_keys filtering
  • Remote evaluation with flag_keys filtering
  • Proper handling of flag_keys in the parse method

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR adds support for filtering feature flags by specific keys when using send_feature_flags in the Python SDK, bringing it into parity with the Node.js SDK. The enhancement allows developers to specify which feature flags should be evaluated and included in capture events rather than evaluating all available flags.

The implementation adds a flag_keys parameter to the SendFeatureFlagsOptions TypedDict in types.py, enabling users to pass a list of specific flag keys to evaluate. The core logic is updated in client.py where:

  • The get_all_flags and get_all_flags_and_payloads methods now accept and pass through the flag_keys parameter
  • Local evaluation in _get_all_flags_and_payloads_locally filters flags during evaluation using a set-based approach for efficient lookup
  • Remote evaluation results are filtered post-response when flag_keys is provided
  • The _parse_send_feature_flags method handles the new parameter extraction

This change integrates seamlessly with the existing feature flag evaluation pipeline, supporting both local and remote evaluation modes. The filtering occurs at the appropriate points - during local evaluation to avoid unnecessary computation, and after remote evaluation to reduce payload size. The implementation maintains backward compatibility as flag_keys is optional and defaults to None.

Confidence score: 4/5

• This PR is safe to merge with proper functionality and comprehensive test coverage
• The implementation follows established patterns and maintains backward compatibility well
• The test coverage in test_client.py needs minor attention for edge cases around empty flag_keys lists

4 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

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