LaunchDarkly importer ANDs all segment rules and included users that LaunchDarkly ORs

Open
#8,721 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@victrvondoom is already working on this.

Since Nov 10, 2026.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
api, backend

Research direction

Read _import_segments in api/integrations/launch_darkly/services.py and the segment depth limit in api/segments/serializers.py; compare the importer test test_process_import_request__valid_segments__creates_segment_per_environment with LaunchDarkly’s _simple_segment_match_context semantics. First check whether the evaluation engine and JSON rules path support the required deeper rule tree. Done means preserving included users, per-rule ANDs, the OR across rules, and exclusions—or reporting an import error when faithful translation is unsupported.

Written by the indexing model from the issue text.

Description

The LaunchDarkly importer merges every rule of a segment, plus its included users, into one ALL root rule. LaunchDarkly evaluates a segment differently, so imported segments match far fewer identities than the source segment.

LaunchDarkly semantics, from its own Python SDK (ldclient/impl/evaluator.py, _simple_segment_match_context):

  1. Context key in included -> in the segment.
  2. Context key in excluded -> not in the segment.
  3. Otherwise in the segment if any rule matches. A rule matches if all of its clauses match.

_import_segments (api/integrations/launch_darkly/services.py, around line 1100) calls _add_clauses_to_segment_rule once per LD rule and _add_users_to_segment_rule for included, all appending to the same root_rule of type ALL. Within a single LD rule, ANDing clauses is correct. Across LD rules and the included list it turns an OR into an AND.

Repro on main at 618f0fbcc, using the existing fixture tests/unit/integrations/launch_darkly/client_responses/get_segments.json, segment dynamic-list-2:

  • rule 1: p1 in [1, 2] AND p2 semVerGreaterThan 1.0.0
  • rule 2: p3 matches foo[0-9]{0,1}
  • included: foo

I ran process_import_request(import_request) (the same call the import task makes), then created identities in the imported Test environment, set traits, and called identity.get_segments():

identity / traits LaunchDarkly Flagsmith segment Dynamic List 2 (Override for test)
foo, no traits (included) in segment not in segment
p3=foo1 only (rule 2) in segment not in segment
p1=1, p2=2.0.0 only (rule 1) in segment not in segment
foo, p1=1, p2=2.0.0, p3=foo1 in segment in segment

Only an identity that satisfies both rules and is in the included list matches. The existing test test_process_import_request__valid_segments__creates_segment_per_environment asserts this structure (the rule clauses and the included-users subrule as ANY_RULE subrules under one ALL root), so it is tested output rather than a quirk of the fixture.

The excluded handling (a NONE subrule under the ALL root) is correct.

A faithful translation needs ANY( included_key, ALL(rule1 clauses), ALL(rule2 clauses), ... ) combined with NONE(excluded), which is three levels of rule nesting. The segments API limits depth with SEGMENT_RULES_MAX_DEPTH = 2 in api/segments/serializers.py. I haven't checked whether the evaluation engine and the JSON rules path from #8245 accept deeper trees. If they do, the importer could emit that structure. If not, importing multi-rule segments as-is silently narrows targeting, and the importer could at least log an import error for any segment with more than one rule or with included users plus rules, like it already does for unmappable operators.

I can send a PR for whichever direction you prefer.

Dominant language
Python
Stars
9.9k
Forks
658
Avg merge
1d 15h
Merged PRs (30d)
168

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Flagsmith/flagsmith

All issues in Flagsmith/flagsmith

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.