> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fiddler.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Segment

> Data segment for targeted monitoring and cohort analysis.

Data segment for targeted monitoring and cohort analysis.

Segment defines subsets of model data based on specific criteria using SQL-like
expressions. Segments enable cohort analysis, A/B testing evaluation, targeted
monitoring of specific populations, and fairness analysis across different groups.

## Example

```python theme={null}
# High-value customer segment
high_value_segment = Segment(
    name="high_value_customers",
    model_id=model.id,
    definition="customer_lifetime_value > 10000 and account_age_days > 365",
    description="Customers with high LTV and established accounts"
).create()

# Geographic segment
west_coast_segment = Segment(
    name="west_coast_users",
    model_id=model.id,
    definition="state == 'CA' or state == 'OR' or state == 'WA'",
    description="Users from West Coast states"
).create()

# Risk-based segment
high_risk_segment = Segment(
    name="high_risk_applications",
    model_id=model.id,
    definition="credit_score < 600 or debt_to_income > 0.4",
    description="Loan applications with elevated risk factors"
).create()

# Age-based demographic segment
young_adults_segment = Segment(
    name="young_adults",
    model_id=model.id,
    definition="age >= 18 and age <= 35",
    description="Young adult demographic (18-35 years)"
).create()

# Use segment in alert rule for targeted monitoring
segment_alert = AlertRule(
    name="high_value_drift_alert",
    model_id=model.id,
    metric_id="drift_score",
    priority=Priority.HIGH,
    compare_to=CompareTo.BASELINE,
    condition=AlertCondition.GT,
    bin_size=BinSize.HOUR,
    critical_threshold=0.7,
    baseline_id=baseline.id,
    segment_id=high_value_segment.id
).create()
```

<Info>
  Segments are evaluated during data processing and can be used with any
  monitoring metric. Complex segment definitions may impact performance,
  so optimize for efficiency. Segments are particularly useful for fairness
  monitoring and business-critical cohort analysis.
</Info>

## create()

Create a new Segment on the Fiddler platform.

Registers this Segment with the Fiddler platform. The expression
must have a name, model\_id, and definition specified before calling create().

### Returns

<ResponseField type="Segment">
  The same Segment instance with updated server-side attributes
  (id, created\_at, etc.).
</ResponseField>

### Raises

* **ApiError** – If there's an error communicating with the Fiddler API.
* **Conflict** – If a Segment with the same name already exists for this model.

## delete()

Delete this Segment from the Fiddler platform.

Permanently removes the Segment. This action cannot be undone.
Any alert rules or monitors using this Segment must be deleted first.

### Raises

* **NotFound** – If the Segment no longer exists.
* **ApiError** – If there's an error communicating with the Fiddler API.
* **Conflict** – If the Segment is still being used by alert rules or monitors.

## *classmethod* from\_name()

Retrieve a Segment by name and model.

Fetches a Segment from the Fiddler platform using its name
and associated model ID.

### Parameters

<ParamField path="name" type="str" required={true}>
  The name of the Segment to retrieve.
</ParamField>

<ParamField path="model_id" type="UUID | str" required={true}>
  UUID or string identifier of the associated Model.
</ParamField>

### Returns

<ResponseField type="Segment">
  The Segment instance for the provided parameters.
</ResponseField>

### Raises

* **NotFound** – If no Segment exists with the specified name and model.
* **ApiError** – If there's an error communicating with the Fiddler API.

## *classmethod* get()

Retrieve a Segment by its unique identifier.

Fetches a Segment from the Fiddler platform using its UUID.

### Parameters

<ParamField path="id_" type="UUID | str" required={true}>
  The unique identifier (UUID) of the Segment to retrieve.
  Can be provided as a UUID object or string representation.
</ParamField>

### Returns

<ResponseField type="Segment">
  The Segment instance with all its configuration and metadata.
</ResponseField>

### Raises

* **NotFound** – If no Segment exists with the specified ID.
* **ApiError** – If there's an error communicating with the Fiddler API.

## *classmethod* get\_organization\_id()

Get the organization UUID from the global connection.

### Returns

<ResponseField type="UUID">
  Unique identifier of the organization associated with the current connection.
</ResponseField>

## *classmethod* get\_organization\_name()

Get the organization name from the global connection.

### Returns

<ResponseField type="str">
  Name of the organization associated with the current connection.
</ResponseField>

## *classmethod* list()

List all Segment instances for a model.

Retrieves all Segment instances associated with a specific model.

### Parameters

<ParamField path="model_id" type="UUID | str" required={true}>
  UUID or string identifier of the Model.
</ParamField>

### Yields

Segment instances for each Segment in the model.

### Raises

**ApiError** – If there's an error communicating with the Fiddler API.

### Returns

`Iterator[Segment]`

## **init**()

Construct a segment instance.
