Skip to content

Commit d827d6d

Browse files
andehenclaude
andcommitted
feat: add device_id to flags request payload
Add device_id parameter to all feature flag methods, similar to how distinct_id is handled. The device_id is included in the flags request payload sent to the server. - Add device_id parameter to Client methods and module-level functions - Add context support via set_context_device_id() for automatic fallback - Add tests for explicit device_id and context-based device_id - Bump version to 7.6.0 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d3609c2 commit d827d6d

6 files changed

Lines changed: 273 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 7.6.0 - 2026-01-12
2+
3+
feat: add device_id to flags request payload
4+
5+
Add device_id parameter to all feature flag methods, allowing the server to track device identifiers for flag evaluation. The device_id can be passed explicitly or set via context using `set_context_device_id()`.
6+
17
# 7.5.1 - 2026-01-07
28

39
fix: avoid return from finally block to fix Python 3.14 SyntaxWarning (#361) - thanks @jodal

posthog/__init__.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from posthog.contexts import (
2424
set_code_variables_mask_patterns_context as inner_set_code_variables_mask_patterns_context,
2525
)
26+
from posthog.contexts import (
27+
set_context_device_id as inner_set_context_device_id,
28+
)
2629
from posthog.contexts import (
2730
set_context_session as inner_set_context_session,
2831
)
@@ -133,6 +136,26 @@ def set_context_session(session_id: str):
133136
return inner_set_context_session(session_id)
134137

135138

139+
def set_context_device_id(device_id: str):
140+
"""
141+
Set the device ID for the current context, associating all feature flag requests
142+
in this or child contexts with the given device ID.
143+
144+
Args:
145+
device_id: The device ID to associate with the current context and its children
146+
147+
Examples:
148+
```python
149+
from posthog import set_context_device_id
150+
set_context_device_id("device_123")
151+
```
152+
153+
Category:
154+
Contexts
155+
"""
156+
return inner_set_context_device_id(device_id)
157+
158+
136159
def identify_context(distinct_id: str):
137160
"""
138161
Identify the current context with a distinct ID.
@@ -483,6 +506,7 @@ def feature_enabled(
483506
only_evaluate_locally=False, # type: bool
484507
send_feature_flag_events=True, # type: bool
485508
disable_geoip=None, # type: Optional[bool]
509+
device_id=None, # type: Optional[str]
486510
):
487511
# type: (...) -> bool
488512
"""
@@ -522,6 +546,7 @@ def feature_enabled(
522546
only_evaluate_locally=only_evaluate_locally,
523547
send_feature_flag_events=send_feature_flag_events,
524548
disable_geoip=disable_geoip,
549+
device_id=device_id,
525550
)
526551

527552

@@ -534,6 +559,7 @@ def get_feature_flag(
534559
only_evaluate_locally=False, # type: bool
535560
send_feature_flag_events=True, # type: bool
536561
disable_geoip=None, # type: Optional[bool]
562+
device_id=None, # type: Optional[str]
537563
) -> Optional[FeatureFlag]:
538564
"""
539565
Get feature flag variant for users. Used with experiments.
@@ -572,6 +598,7 @@ def get_feature_flag(
572598
only_evaluate_locally=only_evaluate_locally,
573599
send_feature_flag_events=send_feature_flag_events,
574600
disable_geoip=disable_geoip,
601+
device_id=device_id,
575602
)
576603

577604

@@ -582,6 +609,7 @@ def get_all_flags(
582609
group_properties=None, # type: Optional[dict]
583610
only_evaluate_locally=False, # type: bool
584611
disable_geoip=None, # type: Optional[bool]
612+
device_id=None, # type: Optional[str]
585613
) -> Optional[dict[str, FeatureFlag]]:
586614
"""
587615
Get all flags for a given user.
@@ -614,6 +642,7 @@ def get_all_flags(
614642
group_properties=group_properties or {},
615643
only_evaluate_locally=only_evaluate_locally,
616644
disable_geoip=disable_geoip,
645+
device_id=device_id,
617646
)
618647

619648

@@ -626,6 +655,7 @@ def get_feature_flag_result(
626655
only_evaluate_locally=False,
627656
send_feature_flag_events=True,
628657
disable_geoip=None, # type: Optional[bool]
658+
device_id=None, # type: Optional[str]
629659
):
630660
# type: (...) -> Optional[FeatureFlagResult]
631661
"""
@@ -657,6 +687,7 @@ def get_feature_flag_result(
657687
only_evaluate_locally=only_evaluate_locally,
658688
send_feature_flag_events=send_feature_flag_events,
659689
disable_geoip=disable_geoip,
690+
device_id=device_id,
660691
)
661692

662693

@@ -670,6 +701,7 @@ def get_feature_flag_payload(
670701
only_evaluate_locally=False,
671702
send_feature_flag_events=True,
672703
disable_geoip=None, # type: Optional[bool]
704+
device_id=None, # type: Optional[str]
673705
) -> Optional[str]:
674706
return _proxy(
675707
"get_feature_flag_payload",
@@ -682,6 +714,7 @@ def get_feature_flag_payload(
682714
only_evaluate_locally=only_evaluate_locally,
683715
send_feature_flag_events=send_feature_flag_events,
684716
disable_geoip=disable_geoip,
717+
device_id=device_id,
685718
)
686719

687720

@@ -712,6 +745,7 @@ def get_all_flags_and_payloads(
712745
group_properties=None, # type: Optional[dict]
713746
only_evaluate_locally=False,
714747
disable_geoip=None, # type: Optional[bool]
748+
device_id=None, # type: Optional[str]
715749
) -> FlagsAndPayloads:
716750
return _proxy(
717751
"get_all_flags_and_payloads",
@@ -721,6 +755,7 @@ def get_all_flags_and_payloads(
721755
group_properties=group_properties or {},
722756
only_evaluate_locally=only_evaluate_locally,
723757
disable_geoip=disable_geoip,
758+
device_id=device_id,
724759
)
725760

726761

0 commit comments

Comments
 (0)