Skip to content

feat: Support registering global traits at client initialization - #268

Closed
gagantrivedi wants to merge 1 commit into
mainfrom
openhands/issue-237
Closed

gagantrivedi wants to merge 1 commit into
mainfrom
openhands/issue-237

Conversation

@gagantrivedi

Copy link
Copy Markdown
Member

What changed

Adds a default_traits argument to Flagsmith.__init__, so traits that never change for the life of a client — app name, developer branch, deployment region — can be registered once instead of being passed on every evaluation call.

flagsmith = Flagsmith(
    environment_key="<your API key>",
    default_traits={
        "app_name": "my-app",
        "branch": {"value": "main", "transient": True},
    },
)

# "app_name" and "branch" are included automatically
flagsmith.get_identity_flags("identifier", traits={"num_orders": 10})

Default traits are merged in at a single choke point (Flagsmith._merge_traits) and apply to:

  • get_identity_flags — both the remote API path (sent in the identities request body) and local evaluation (placed in the evaluation context)
  • get_identity_segments
  • get_experiment_flag — via the two calls it delegates to, so traits are merged exactly once
  • track_event and track_exposure_event

get_environment_flags is unchanged: it evaluates without an identity and deliberately strips segments, so traits have nothing to act on there.

Semantics

  • Per-call traits win. A trait passed to an individual call overrides a default trait with the same key.
  • Transient envelopes work the same. A default trait can be given as {"value": ..., "transient": True} to keep it from being persisted against the identity, exactly as for per-call traits.
  • Backwards compatible. The new argument is keyword-defaultable and appended to the end of the signature; omitting it leaves default_traits as {} and every existing code path behaves as before.

What a reviewer should check

  • The argument name. The issue says "global traits"; I used default_traits to match the existing default_flag_handler naming and because "default" conveys that per-call traits override it. Happy to rename to global_traits if you prefer the issue's wording.
  • Whether merged default traits should be sent for persistence on the remote identities endpoint by default, or be transient unless opted in. The current behaviour persists them unless the caller envelopes them, which matches how per-call traits already work.
  • That skipping get_environment_flags is the right call.

Testing

Nine new tests in tests/test_flagsmith.py cover the remote API body, local-evaluation context, identity segments, both event-tracking methods, get_experiment_flag, per-call precedence on each path, transient default traits, and the empty default.

145 passed in 0.97s

mypy flagsmith tests sdk-spec, black, isort and flake8 are all clean.

Closes #237

This pull request was opened by an AI agent (OpenHands).

Add a `default_traits` argument to `Flagsmith.__init__` so traits that
never change for the life of a client (app name, developer branch, and
so on) can be registered once instead of being supplied on every call.

Default traits are merged into `get_identity_flags`,
`get_identity_segments`, `get_experiment_flag`, `track_event` and
`track_exposure_event`. Traits passed to an individual call take
precedence on key conflicts, and default traits accept the same
`{"value": ..., "transient": True}` envelope as per-call traits.

Closes #237

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@gagantrivedi
gagantrivedi requested a review from a team as a code owner October 21, 2026 16:57
@gagantrivedi
gagantrivedi requested review from emyller and removed request for a team October 21, 2026 16:57
@gagantrivedi gagantrivedi added the openhands-review Requests an independent OpenHands review of this pull request label Oct 21, 2026
@gagantrivedi gagantrivedi changed the title [#237] Support registering global traits at client initialization feat: Support registering global traits at client initialization Oct 21, 2026
@gagantrivedi
gagantrivedi marked this pull request as draft October 21, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

openhands-review Requests an independent OpenHands review of this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support registering global traits at client initialization

2 participants