Overview
ze.choose() enables A/B testing by making weighted random selections between variants (models, prompts, parameters, etc.), timeboxing each experiment, and automatically tracking the chosen variant on the active span/trace/session for downstream analytics.
Key features:
- Weighted random selection between variants
- Experiment timeboxing via
duration_days - Automatic tracking of choices within spans, traces, or sessions
- Consistency caching — same entity always gets the same variant
- Built-in validation of weights, variant keys, and defaults
- Automatic fallback to a default variant once an experiment completes
Basic Usage
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name | str | Yes | Name of the A/B test (e.g., “model_selection”, “prompt_variant”) |
variants | Dict[str, Any] | Yes | Dictionary mapping variant keys to their values |
weights | Dict[str, float] | Yes | Dictionary mapping variant keys to selection probabilities (must sum to ~1.0) |
duration_days | int | Yes | Number of days the experiment should run; must be > 0 |
default_variant | str | No | Variant key to use automatically once the experiment ends (defaults to the first key if omitted) |
Returns
Returns the value from the selected variant (not the key).Experiment Lifecycle & Defaults
duration_daystimeboxes the experiment. Once the backend marks it completed,ze.choose()automatically serves thedefault_variant.- If
default_variantis omitted, the first key invariantsbecomes the fallback. - When an experiment is still active, the same entity (span/trace/session) receives a cached, consistent variant choice.
Tracking Signals
Attach success metrics to the same span whereze.choose() runs so dashboards can correlate outcomes with variant performance:
Complete Example
Important Notes
- Context Required: Must be called within an active
ze.span(), trace, or session - Consistency: Same entity (span/trace/session) always receives the same variant while the test runs
- Weight Validation: Weights should sum to 1.0 (warns if not within 0.95-1.05)
- Duration Required:
duration_daysmust be > 0; experiments stop after this window - Fallback Behavior: Once the backend reports the test as completed,
default_variantis used automatically - Signal Analytics: Use
ze.set_signal()on the same span to compare variant impact in the dashboard - Key Matching: Variant keys and weight keys must match exactly