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

# Failed Rows Sample | OpenMetadata Data Quality

> See which rows made a data quality test fail. Learn what the failed rows sample is, and how to fill it through the API for dbt tests and other test results you push from outside OpenMetadata.

# Failed Rows Sample

When a test fails, you often need to see the rows that caused the failure. The **failed rows sample** is a small table of those rows. It appears on the test case page, so you can inspect bad data without leaving OpenMetadata.

The failed rows sample is part of OpenMetadata. It is not limited to Collate.

## Where You See It

OpenMetadata fills the sample on its own for tests that it runs itself, such as tests you create in the UI or in a data quality workflow. Open the test case page and stay on the **Test Case Results** tab. Scroll to the **Sample Data** section. It shows the failed rows, and it highlights the column that failed the test.

![Sample Data section on the test case page showing failed rows with the failing last\_name column highlighted](https://mintcdn.com/openmetadata/dWYpA1HoPpkCF-Ox/public/images/how-to-guides/data-quality-observability/quality/failed-rows-sample.png?fit=max\&auto=format\&n=dWYpA1HoPpkCF-Ox\&q=85\&s=fe1dbdb27efad460ec242e40185a01e9)

Only tests that find failed rows, such as **Column Values To Be Unique** or **Column Values To Be Not Null**, show this section. Tests that return a single measured value, such as **Table Column Count To Equal**, don't.

OpenMetadata keeps the sample when a test fails, and when a test passes within its failure threshold but still finds failed rows. For more information, see [Failed Rows Samples](https://docs.open-metadata.org/v2.1.x/how-to-guides/data-quality-observability/quality/failure-thresholds.md#failed-rows-samples).

## dbt and Externally Run Tests

OpenMetadata doesn't fill the sample for tests it only imports, such as dbt tests. The imported result tells OpenMetadata that a test failed, but not which rows. You can add those rows yourself through the API.

Use this for any result that comes from outside OpenMetadata.

### Before You Start

* The test case must already exist in OpenMetadata. For dbt, run the dbt workflow so the tests are imported. dbt tests appear in OpenMetadata under the name of the dbt test.
* The test case's **latest result** must have failed rows. This means the result is `Failed`, or it's `Success` with at least one failed row. If it isn't, the API returns `Failed rows can only be added to a test case whose latest result has failed rows.`
* Your user or bot needs the permission to edit tests. To read the sample, it needs the permission to view failed rows samples.

### Add the Sample

Send a `PUT` request to the test case:

```
PUT /api/v1/dataQuality/testCases/{id}/failedRowsSample?validate=false
```

Send a request body like the following:

```json theme={null}
{
  "columns": ["col1", "col2"],
  "rows": [["a", 1], ["b", 2]]
}
```

| Part | Description |
| :- | :- |
| `{id}` | The **id** of the test case, not its name. |
| `columns` | The column names of the sample. |
| `rows` | The rows. Each row must have one value for every column. |
| `validate` | Defaults to `true`, which checks that every name in `columns` is a column of the table the test runs on. Set it to `false` when the sample comes from another table, such as a dbt failures table, with its own column names. |

The API has no limit on the number of rows. Send a small sample, because OpenMetadata stores what you send and shows it in the UI.

To get the test case id, look the test case up by its fully qualified name:

```
GET /api/v1/dataQuality/testCases/name/{fqn}
```

The `id` is in the response.

To read or remove a sample, use `GET` or `DELETE` on the same `/api/v1/dataQuality/testCases/{id}/failedRowsSample` path.

## Example: dbt Failed Rows

dbt can store the rows that fail a test in a table in your warehouse. Turn it on with `store_failures` in your dbt project. dbt then records that table's name in `run_results.json`, in the `relation_name` field of each test result.

A common setup adds one step after the dbt run and its import into OpenMetadata. For example, an Airflow task that:

1. Reads `run_results.json` and picks the tests with the status `fail`.
2. Reads a sample of rows from each failed test's `relation_name` table.
3. Finds the matching OpenMetadata test case and calls the API above.

The following Python script shows the idea. Replace the host, token, and the `TEST_CASE_FQNS` map with your own values, and swap `read_failed_rows` for a query against your warehouse.

```python theme={null}
import json
import requests

OM_HOST = "https://your-openmetadata-host/api/v1"
HEADERS = {"Authorization": "Bearer <bot-token>"}
SAMPLE_SIZE = 50

# dbt test unique_id -> fully qualified name of the test case in OpenMetadata
TEST_CASE_FQNS = {
    "test.my_project.not_null_orders_id.abc123": "service.db.schema.orders.id.not_null_orders_id",
}


def read_failed_rows(relation_name, limit):
    """Return (columns, rows) from the dbt store_failures table."""
    raise NotImplementedError("Query your warehouse here")


with open("target/run_results.json") as f:
    results = json.load(f)["results"]

for result in results:
    fqn = TEST_CASE_FQNS.get(result["unique_id"])
    if result["status"] != "fail" or not fqn or not result.get("relation_name"):
        continue

    columns, rows = read_failed_rows(result["relation_name"], SAMPLE_SIZE)

    test_case = requests.get(
        f"{OM_HOST}/dataQuality/testCases/name/{fqn}", headers=HEADERS
    )
    test_case.raise_for_status()

    response = requests.put(
        f"{OM_HOST}/dataQuality/testCases/{test_case.json()['id']}/failedRowsSample",
        params={"validate": "false"},
        json={"columns": columns, "rows": rows},
        headers=HEADERS,
    )
    response.raise_for_status()
```

> [!NOTE]
>
> Run this step only after the dbt workflow has imported the latest results. If the test case's latest result in OpenMetadata is still an older passing result, the API rejects the sample.

> [!WARNING]
>
> Failed rows can contain sensitive data. The sample shows the values you send. Review who can open the test case before you send rows from tables that hold personal data.

## Related

* [Failure Thresholds](https://docs.open-metadata.org/v2.1.x/how-to-guides/data-quality-observability/quality/failure-thresholds.md)
* [dbt Integration](https://docs.open-metadata.org/v2.1.x/connectors/database/dbt.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.