Skip to content

chore: Include Features, Segments in evaluation context schema #5867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 18, 2025

Conversation

khvn26
Copy link
Member

@khvn26 khvn26 commented Jul 30, 2025

Thanks for submitting a PR! Please check the boxes below:

  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

Closes #5808.

This PR adds:

  • An EvaluationResult schema that includes flag and used segment information.
  • segments and features keys to the EvaluationContext schema to have all data required for evaluation in a single DTO.

The design goal was to have a structure that is extensible, simple enough to parse and read through, and that provides all required engine data, excluding any data unrelated to engine.

Some new vocabulary is added here that, IMO, make sense for evaluation:

  • String "key" fields are preferred to numeric id fields to more clearly state their purpose.
  • A "variants" key is used to represent multivariate feature states, where each variant includes a "weight" indicating its distribution percentage. I considered using a unified "values" array for all feature states — including non-multivariate ones (with a single { "weight": 100 } entry) — but opted against it to avoid ambiguous semantics around control values and the need to validate against empty arrays.

Both objects will be used by the new engine interface to be introduced in Flagsmith/flagsmith-engine#234.

An example EvaluationContext object:

{
  "environment": {
    "key": "abcde",
    "name": "Test Environment"
  },
  "features": {
    "feature_1": {
      "key": "1",
      "name": "feature_1",
      "enabled": true,
      "value": "foobar"
    },
    "feature_2": {
      "key": "2",
      "name": "feature_2",
      "enabled": false,
      "value": "control",
      "variants": [
        {
          "value": "a",
          "weight": 51.0
        },
        {
          "value": "b",
          "weight": 49.0
        }
      ]
    }
  },
  "segments": {
    "test_segment": {
      "key": "1",
      "name": "test_segment",
      "rules": [
        {
          "type": "ALL",
          "sub_rules": [
           {
          "type": "ANY",
          "conditions": [
            {
              "path": "$.identity.identifier",
              "operator": "IN",
              "value": "id1,id2"
            },
            {
              "path": "some-trait",
              "operator": "EQUALS",
              "value": "foobar"
            }
          ]
        }
          ]
        }
      ]
    }
  }
}

An example EvaluationResult object:

{
  "context": {...},
  "flags": [
    {
      "enabled": false,
      "value": "foobar",
      "feature": {
        "key": "1",
        "name": "feature_1"
      }
    },
    {
      "enabled": true,
      "value": null,
      "feature": {
        "key": "2",
        "name": "feature_2"
      }
    }
  ],
  "segments": [
    {
      "key": "1",
      "name": "test_segment"
    }
  ]
}

How did you test this code?

N/A

Copy link

vercel bot commented Jul 30, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
docs Ignored Ignored Preview Aug 15, 2025 7:24pm
flagsmith-frontend-preview Ignored Ignored Preview Aug 15, 2025 7:24pm
flagsmith-frontend-staging Ignored Ignored Preview Aug 15, 2025 7:24pm

@github-actions github-actions bot added the chore label Jul 30, 2025
@github-actions github-actions bot added chore and removed chore labels Jul 31, 2025
@github-actions github-actions bot added chore and removed chore labels Jul 31, 2025
@github-actions github-actions bot added chore and removed chore labels Jul 31, 2025
@khvn26 khvn26 marked this pull request as ready for review July 31, 2025 19:19
@khvn26 khvn26 requested a review from a team as a code owner July 31, 2025 19:19
@khvn26 khvn26 requested review from gagantrivedi and removed request for a team July 31, 2025 19:19
@github-actions github-actions bot added chore and removed chore labels Jul 31, 2025
Copy link
Contributor

github-actions bot commented Jul 31, 2025

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-api-test:pr-5867 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-e2e:pr-5867 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-5867 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-5867 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-5867 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-5867 Finished ✅ Results

Copy link
Contributor

@Zaimwa9 Zaimwa9 left a comment

Choose a reason for hiding this comment

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

Looks solid overall. One piece feels missing to make the evaluation fully representative imo - but let me know if it's really useful.

Right now we have:

  • the flag being evaluated
  • the identity under evaluation
  • the segments that can override
  • the final evaluation result

What could be missing in the result is the why do I have those values. We know which segment overid but we don't have necessarily its values (especially thinking with multi-variate where the possibilities are wider).
Do you think it would make sense to surface the override override configuration (blue 40%/red60% instead of blue80%/Green20%) ?

@github-actions github-actions bot added chore and removed chore labels Aug 7, 2025
@khvn26 khvn26 requested a review from emyller August 8, 2025 11:50
gagantrivedi
gagantrivedi previously approved these changes Aug 8, 2025
emyller
emyller previously approved these changes Aug 8, 2025
@khvn26 khvn26 dismissed stale reviews from emyller, gagantrivedi, and Zaimwa9 via c67d7b8 August 11, 2025 11:36
@github-actions github-actions bot added chore and removed chore labels Aug 11, 2025
@github-actions github-actions bot added chore and removed chore labels Aug 11, 2025
@github-actions github-actions bot added chore and removed chore labels Aug 11, 2025
@khvn26
Copy link
Member Author

khvn26 commented Aug 12, 2025

I had to add some stuff while finalising https://github.com/Flagsmith/flagsmith-engine/pull/239/files:

  • I brought back FeatureContext.name in c67d7b8 to simplify serialisation of FlagResults.
  • Added FeatureContext.feature_key in 04103f3. Feature (not feature state) ids are used to correctly apply overrides, and are surfaced in FlagResult objects to maintain backwards compatibility. Additionally, I removed nesting from FlagResult.
  • Fixed incorrect priority field documentation in f50fb30.

emyller
emyller previously approved these changes Aug 12, 2025
@khvn26 khvn26 force-pushed the chore/features-contexts-in-eval-context-schema branch from 6ec4624 to 015c11d Compare August 15, 2025 19:23
@github-actions github-actions bot added chore and removed chore labels Aug 15, 2025
@khvn26 khvn26 merged commit a4915ce into main Aug 18, 2025
25 checks passed
@khvn26 khvn26 deleted the chore/features-contexts-in-eval-context-schema branch August 18, 2025 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include Features and Segments in the Evaluation context schema
4 participants