> For the complete documentation index, see [llms.txt](https://docs.sdv.dev/sdmetrics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sdv.dev/sdmetrics/data-metrics/diagnostic/constraintadherence.md).

# ConstraintAdherence

This metric captures whether the synthetic data adheres to the business rules in your dataset, expressed as *constraints*.

{% hint style="info" %}
**What are constraints?** Constraints are a way to describe business rules that appear in your dataset. For more information about how to represent constraints, as well as a list of possible constraints, please see the [SDV documentation](https://docs.sdv.dev/sdv/modeling/constraint-augmented-generation-cag/constraints-api#json-representation).
{% endhint %}

## Data Compatibility

This metric is designed to work on any data column that is described by the constraint (involved in a business rule).

## Score

**(best) 1.0**: All rows in the synthetic data respect the constraint.

**(worst) 0.0**: No rows in the synthetic data respect the constraint.

## How does it work?

This metrics checks each data point in the synthetic data to see if it follows the constraint that is provided. The score the proportion of synthetic data data points that match the constraint definition.

Edge cases:

* If the constraint cannot be checked for some reason (for example, the referenced table or column names don't exist in the data), then the metric will issue a warning and the score will be NaN.
* If the real data does not match the constraint, then the metric will issue a warning but it will continue to check the synthetic data for adherence to the constraint.
* The exact logic checked is based on the constraint, as defined in SDV. For more information, please see the [SDV documentation](https://docs.sdv.dev/sdv/modeling/constraint-augmented-generation-cag/constraints-api#json-representation).
* Note that some constraints may not really have a logic. For example a DenormalizedTable with only the primary key (and no column names) doesn't have anything to check; in this case, the metric returns a score of 1.0.

## Usage

To manually apply this metric, access the `multi_table` module and use the `compute` method.

```python
from sdmetrics.multi_table import ConstraintAdherence

ConstraintAdherence.compute(
    real_data=real_data_dict,
    synthetic_data=synthetic_data_dict,
    metadata=metadata_dict,
    constraint={
      'class_name': 'DenormalizedTable',
      'parameters': {
        'table_name': 'UserTransaction',
        'denormalized_primary_key': 'User ID',
        'denormalized_column_names': ['DateOfBirth', 'FirstName', 'LastName']
      } 
    }
)
```

**Parameters**

* (required) `real_data`: A dictionary mapping the name of each table to a pandas.DataFrame containing the real data for that table.
* (required) `synthetic_data`: A dictionary mapping the name of each table to a pandas.DataFrame containing the synthetic data for that table
* (required) `metadata`: A dictionary describing the format, types of data and relationship between the tables. See [Metadata](/sdmetrics/getting-started/metadata.md) for more details.
* (required) `constraint`: A dictionary that defines the type of constraint and the parameters. This dictionary corresponds to a single constraint only and should follow the [JSON definition from SDV](https://docs.sdv.dev/sdv/modeling/constraint-augmented-generation-cag/constraints-api?select=numerical,datetime,categorical,personal-info,banking,networking,location#json-representation). Typically, the `class_name` parameter is the full name of the constraint class, and the `parameters` dictionary maps each of the constraint parameters (key) to the value.

## FAQs

<details>

<summary>Does this metric use real data?</summary>

If the real data doesn't match the provided constraint, then the metric will provide a warning but it will still continue to check the synthetic data.

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sdv.dev/sdmetrics/data-metrics/diagnostic/constraintadherence.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
