# How to actually create a custom Query Expectations

**URL:** https://discourse.greatexpectations.io/t/how-to-actually-create-a-custom-query-expectations/1521
**Category:** GX Core Support
**Tags:** how-to, types-of-expectation
**Created:** [November 25, 2023, 2:58pm UTC](https://discourse.greatexpectations.io/t/how-to-actually-create-a-custom-query-expectations/1521 "2023-11-25T14:58:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mant](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@mant](https://discourse.greatexpectations.io/u/mant)
#### Post date: [November 25, 2023, 2:58pm UTC](https://discourse.greatexpectations.io/t/how-to-actually-create-a-custom-query-expectations/1521/1 "2023-11-25T14:58:38Z")

</div>

After trying to replicate the tutorials on creating a custom Query expectation, copying the already implemented ones, I find myself unable to import any Query Expectation into my GX suite. Even just adding the already shipped ones in my GX suite (like “expect\_queried\_column\_value\_frequency\_to\_meet\_threshold”) just yields the Error

‘ValueError: Unable to determine “domain\_type” of this “ExpectationConfiguration” object from “kwargs” and heuristics.’

I am using GX 18.3,Python 3.11 with Pyspark on a small data frame. My Code otherwise works if I don’t use the any Query Metrics.

---

<div class="post-metadata">

### Author: ![HaebichanGX](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.greatexpectations.io/haebichangx/32/211_2.png) [@HaebichanGX](https://discourse.greatexpectations.io/u/HaebichanGX)
#### Post date: [November 27, 2023, 5:22pm UTC](https://discourse.greatexpectations.io/t/how-to-actually-create-a-custom-query-expectations/1521/2 "2023-11-27T17:22:49Z")

</div>

Hi @mant to get custom expectations to get imported, you need to follow few steps.

1. Create your context using this code:

```auto
import great_expectations as gx
from great_expectations.core.expectation_configuration import ExpectationConfiguration
import pandas as pd
import os 
from great_expectations.data_context import FileDataContext

base = os.getcwd()

path_to_empty_folder = base + "/my_gx_project/"

context = FileDataContext.create(project_root_dir=path_to_empty_folder)

```

1. Put your custom expectations code inside great\_expectations → plugins → expectations
2. Then run `from my_gx_project.great_expectations.plugins.expectations import your_custom_expectation`. This is if your GX notebook is in the same root as the project folder (I called it “my\_gx\_project”).

Remember that context needs to be called before importing custom expectations.

---

<div class="post-metadata">

### Author: ![mant](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@mant](https://discourse.greatexpectations.io/u/mant)
#### Post date: [December 8, 2023, 7:46pm UTC](https://discourse.greatexpectations.io/t/how-to-actually-create-a-custom-query-expectations/1521/3 "2023-12-08T19:46:28Z")

</div>

Thanks for your answer, but this is not my problem.

I cannot add QUERY expectations to my suite. I followed the tutorial on creating CUSTOM QUERY EXPECTATIONS. I add them to my plugins folder and then import them. Then I get the error

````auto
Traceback (most recent call last):
  File "/Users/christoph/PycharmProjects/great_expectations_test/gx_cli_test_pyspark.py", line 33, in <module>
    my_suite.show_expectations_by_expectation_type()
  File "/Users/christoph/PycharmProjects/great_expectations_test/.venv/lib/python3.11/site-packages/great_expectations/core/expectation_suite.py", line 798, in show_expectations_by_expectation_type
    self.get_grouped_and_ordered_expectations_by_expectation_type()
  File "/Users/christoph/PycharmProjects/great_expectations_test/.venv/lib/python3.11/site-packages/great_expectations/core/expectation_suite.py", line 861, in get_grouped_and_ordered_expectations_by_expectation_type
    self.get_table_expectations(),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/christoph/PycharmProjects/great_expectations_test/.venv/lib/python3.11/site-packages/great_expectations/core/expectation_suite.py", line 885, in get_table_expectations
    expectation_configurations: List[ExpectationConfiguration] = list(
                                                                 ^^^^^
  File "/Users/christoph/PycharmProjects/great_expectations_test/.venv/lib/python3.11/site-packages/great_expectations/core/expectation_suite.py", line 887, in <lambda>
    lambda element: element.get_domain_type() == MetricDomainTypes.TABLE,
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/christoph/PycharmProjects/great_expectations_test/.venv/lib/python3.11/site-packages/great_expectations/core/expectation_configuration.py", line 1514, in get_domain_type
    raise ValueError(
ValueError: Unable to determine "domain_type" of this "ExpectationConfiguration" object from "kwargs" and heuristics.```

This happens with ALL QUERY expectations in Great Expectations. 
This is what I add to my Suite.json file 

````

{  
“expectation\_type”: “expect\_queried\_column\_list\_to\_be\_unique”,  
“kwargs”: {  
“template\_dict”: {  
“column\_list”: “id”  
}  
},  
“meta”: {  
“notes”: {  
“format”: “markdown”,  
“content”: “Some clever comment about this expectation. **Markdown** `Supported`”  
}  
}  
}

```auto

```
