> For the complete documentation index, see [llms.txt](https://docs.sdv.dev/sdv/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/sdv/modeling/constraint-augmented-generation-cag/constraints-api.md).

# Constraints API

Apply constraints to any SDV synthesizer.

## Constraints API

The constraint API below applies to any SDV synthesizer object.

### get\_constraints

Use this function to inspect all of the constraints your synthesizer contains and save them externally as a JSON file.

**Parameters**:

* `output_filepath`: A string containing the location of a filepath. If provided, this function saves the constraints that are currently added to the synthesizer as a JSON file. Later, these constraints can be set using the [`set_constraints` function](#set_constraints).
  * (default) `None`: Do not save the constraints in a file.

**Output** A list of the constraint objects that you have supplied to your synthesizer. If the filepath is provided, then the function also saves the constraints as a JSON file.

```python
constraints = synthesizer.get_constraints(
    output_filepath='synthesizer_constraints.json'
)
```

### get\_metadata

Adding constraints to your synthesizer may internally modify the metadata. Use this function to get the modified metadata after the constraints have been added.

**Parameters**

* `version`: The version of metadata to get. Supply `'modified'` to get the modified version of the metadata (after applying the constraints).

**Output** A Metadata object with the modified metadata

```python
modified_metadata = synthesizer.get_metadata(version='modified')
```

### add\_constraints

Use this function to add constraints to your synthesizer.

**Parameters**

* (required) `constraints`: A list of constraint objects or an auto-detected `ConstraintList` object to add to your synthesizer. You can supply [**predefined constraints**](/sdv/modeling/constraint-augmented-generation-cag/predefined-constraints.md) or [**auto-detected constraints**](/sdv/modeling/constraint-augmented-generation-cag/auto-detect-constraints.md). Call this function multiple times to continue adding constraints.

**Output** (None)

```python
# create predefined constraints and add them to the synthesizer
from sdv.cag import FixedIncrements

salary_constraint = FixedIncrements(
    table_name='Users',
    column_name='salary',
    increment_value=1000
)

synthesizer.add_constraints([salary_constraint])

# auto-detect constraints and add them to the synthesizer

detected_constraints = synthesizer.detect_constraints(data)
synthesizer.add_constraints(
    constraints=detected_constraints
)
```

### load\_constraints

Use this function to load constraints that were previously saved to a file using the [`get_constraints` function](#get_constraints).

**Parameters**:

* (required) `filepath`: A string with the JSON filepath that contains all the constraint information. This file can be created using the [`get_constraints` function](#get_constraints).

**Returns:** A list of constraint objects. You can add this list directly to your synthesizer using the [`add_constraints` function](#add_constraints).

```python
from sdv.utils import load_constraints

constraints = load_constraints(filepath='synthesizer_constraints.json')
synthesizer.add_constraints(constraints)
```

### set\_constraints

*Deprecated. To add constraints from a file, use* [*load\_constraints*](#load_constraints) *to load in the constraints from the file, and then* [*add\_constraints*](#add_constraints) *to add them.*

### ❖ detect\_constraints

Use this method with an SDV synthesizer in order to detect constraints. *All single- and multi-table SDV synthesizers are supported except for DayZSynthesizer.*

```python
synthesizer = HSASynthesizer(metadata)
constraints = synthesizer.detect_constraints(data=my_datset)
```

*For API and parameters, see the* [*Auto-Detect Constraints*](/sdv/modeling/constraint-augmented-generation-cag/auto-detect-constraints.md) *guide.*

{% hint style="info" %}
❖ **SDV Enterprise Bundle**. This feature is available as part of the **CAG Bundle**, an optional add-on to SDV Enterprise. For more information, please visit the [CAG Bundle](/sdv/explore/sdv-bundles/cag.md) page.
{% endhint %}

## JSON Representation

Each constraint can be represented in a JSON file format. This is useful when you want to save all the constraints to be used for a synthesizer, and add them all at once for your workflow.

To create this file format, we recommend adding the constraints to a synthesizer first and then using the [`get_constraints` function](#get_constraints). This function renders the JSON file in the required format. Alternatively, you can follow the format below.

```json
[{
    "class_name": "DenormalizedTable",
    "parameters": {
        "table_name": "Transactions-Users",
        "denormalized_primary_key": "User ID",
        "denormalized_column_names": ["User Birthdate"]
    }
},{
    "class_name": "FixedIncrements",
    "parameters": {
        "table_name": "Users",
        "column_name": "salary",
        "increment_value": 1000
    }
},{
    ...
}]
```

### JSON Spec Details

The constraints JSON file should be a list of dictionaries. Each dictionary represents a constraint that should be added to the synthesizer. The constraints should be listed in the order in which they should be added.

Each constraint dictionary should include 2 keywords:

* `"class_name"`: A string containing the name of the single-table or multi-table constraint class. This is the class name that you'd typically import from the `sdv.cag` module.
* `"parameters"`: A dictionary that contains key/value pairs that correspond to the parameters of that constraint. Each constraint has different parameters that are possible. They typically include information that identifies where in your dataset the constraint should be added, for example the table name, column names, etc. For more information, browse the [Predefined Constraints](/sdv/modeling/constraint-augmented-generation-cag/predefined-constraints.md).


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sdv.dev/sdv/modeling/constraint-augmented-generation-cag/constraints-api.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
