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

# Generating SQL in Validators and Filters

Learn how to use AI language models to enable text-to-SQL functionality when composing queries for SQL validators and filters

When configuring Custom SQL validators and SQL filters, you can use external Large Language Model (LLM) providers like Anthropic Claude to compose and correct your SQL queries.

Instead of writing complex SQL queries from scratch, you can leverage LLM credentials in Validio to:

* **Generate SQL automatically** from natural language descriptions of what you need to validate in your data
* **Quickly fix broken SQL** when the query test returns errors and fails

This feature is particularly valuable when working with complex data validation scenarios where manual SQL writing would be time-consuming or error-prone.

# Prerequisites

This feature requires that you have:

* Enabled the **AI Feature > Allow LLM Credentials** setting in your Validio Workspace. See [Configuring Global Settings](https://docs.validio.io/docs/global-settings).
* Configured at least one LLM credential, such as Anthropic, in your namespace. Each credential is connected to one model. See [LLM Credentials](https://docs.validio.io/docs/llm-credentials).
* Granted permissions to run SQL queries in the namespace: `sqlExecution:WRITE`. See [Managing Roles](https://docs.validio.io/docs/managing-roles)

# Generating SQL in Custom Validators

LLM credentials simplifies the configuration of custom SQL validators. Instead of writing the SQL query for the metric calculation from scratch, use the **Generate SQL** button to describe the metric you want to calculate and let the AI model generate the SQL query for you.

<Image align="center" border={false} caption="Selecting the LLM credential with the model to generate the query" src="https://files.readme.io/01b1fb469642095bcf218ef34ddb80c808292ce7a84f687d1bbee191b150b7e8-v73-sqlvalidator-selectllm.png" width="600px" />

<Image align="center" border={false} caption="Generated SQL query with options to accept or reject (or test again)" src="https://files.readme.io/fef04cd18be749b31a6d0b56b8208eff3bd93cc4d59b3df9be4db48c6b401cfe-v73-sqlvalidator-generatedsql.png" width="600px" />

After defining the metric calculation, you can continue configuring the Custom SQL validator. See [Custom SQL Validators](https://docs.validio.io/docs/custom-sql).

# Fixing Errors in SQL Queries

If testing your custom SQL query returns errors, you can use **Fix errors** and select an LLM credential to analyze and correct the errors for you.

<Image align="center" border={false} caption="Selecting the LLM Credential to fix query errors" src="https://files.readme.io/cb7dbab4a25b6bdc47b0c7e6ee4953b86fd6a34c2d4fb821ba0a02f8c66360af-v73-sqlvalidator-fixerrors.png" width="600px" />

Fixing the query may take a few minutes depending on the complexity of the query. After the query is corrected, you can run another test. The changes will not be saved until you click accept. See [Custom SQL Validators](https://docs.validio.io/docs/custom-sql).

# Generating SQL in Filters

The LLM credential can also assist with writing a SQL filter query. In the SQL filter configuration, use **Generate SQL** to describe your filtering requirements, and select the LLM credential to generate the SQL query.

<Image align="center" border={false} caption="Selecting an LLM credential to generate the SQL query for your filtering requirements" src="https://files.readme.io/204c4e0683cd73cf6b9d4be95067af1878314c4da6a486d588d67c05f3a58aad-v73-sqlfilter-selectllm.png" width="600px" />

<Image align="center" border={false} caption="Previewing the generated filter and results" src="https://files.readme.io/a19d5f423142dace5a3c4f1e16badbae0b378fad4b5e2c8ab369088858b7fa97-v73-sqlfilter-generatedsqlpreview.png" width="600px" />

After you accept the SQL query, you can create the SQL filter. For more examples, see [SQL Filter](https://docs.validio.io/docs/filter-types#sql-filter).