Flagsmith: Inconsistent Identity Propagation in `FlagsmithClientProvider` on Context Updates
Description
When using Flagsmith as an OpenFeature provider, updating the context does not always trigger an identity API call. This results in:
- Missing traits in the
/identitiesAPI request - Incorrect segmentation behavior
The issue arises when the provider is set before the context.
Steps to Reproduce
✅ Working Flow
This sequence correctly triggers the /identities endpoint with the expected traits:
const flagsmithClient = new FlagsmithClient();
await OpenFeature.setContext({ targetingKey: 'id', traitA: 'abc' });
OpenFeature.setProvider(flagsmithClient);
❌ Non-Working Flow
In this case, the identity API call is not made after setting the context, resulting in empty traits:
const flagsmithClient = new FlagsmithClient();
OpenFeature.setProvider(flagsmithClient);
await OpenFeature.setContext({ targetingKey: 'id', traitA: 'abc' });
Observed Behavior
- Without a
targetingKey, segmentation defaults to environment-level settings. - Features that should be disabled remain enabled, with a "STATIC" reason.
- Updating the context after setting the provider does not trigger a re-fetch with the correct identity data.
Expected Behavior
- Whenever context is updated, the provider should correctly propagate identity and traits and fetch correct state of flags for target user.
Possible Cause
In FlagsmithClientProvider, when the context updates, the provider calls:
this._client.getFlags();
However, this does not include the new identity information. Updating this call to:
this._client.identify(identity, context);
resolves the issue.
🔍 [Problematic line in the source code](https://github.com/open-feature/js-sdk-contrib/blob/main/libs/providers/flagsmith-client/src/lib/flagsmith-client-provider.ts#L56).
Environment
- OpenFeature JS SDK with Flagsmith as the provider.
Would a maintainer please assign this issue to me? Thanks
I think I ran into this problem as well with the React SDK.
Using flagsmith.setContext(...) to identify the user seems to trigger a fetch but does not use the correct context so the result is the default flags. Going back to using flagsmith.identify(...) seems to work even though it seems labeled as deprecated.