# OpenAI connect with elastic search datasource

**URL:** <https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901>\
**Category:** Elasticsearch\
**Created:** [November 24, 2023, 5:47am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901 "2023-11-24T05:47:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saurabh\_Agrawal2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_agrawal2/32/123927_2.png) [@Saurabh\_Agrawal2](https://discuss.elastic.co/u/Saurabh_Agrawal2)\
**Post date:** [November 24, 2023, 5:47am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/1 "2023-11-24T05:47:32Z")

</div>

I am trying call openAI with my custom index created on Elastic search but when I try to run it I am getting following error:

```auto
openai.BadRequestError: Error code: 400 - {'error': {'requestid': 'aaa-bbb-404d-8a12-3da23608f42c', 'code': 400, 'message': 'Validation error at #/dataSources/0/Elasticsearch/parameters: Value error, Invalid credentials configuration. Please use either key and keyId together, or just apiKey.'}}

```

The code I have used for this:

```auto
client.chat.completions.create(
        messages=message_text,
        model=deployment_id,
        extra_body={
            "dataSources": [
                {
                    "type": "Elasticsearch",
                    "parameters": {
                        "apiKey": "my_apiKey",
                        "endpoint": "my_endpoint",
                        "indexName": "my_indexName",
                    },
                }
            ]
        }
    )

```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 24, 2023, 10:25am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/2 "2023-11-24T10:25:31Z")

</div>

Hi @Saurabh_Agrawal2,

Welcome! I assume you are using the Chat Completion API from OpenAI? Which version of the library are you using, and is it for Python or JavaScript? Also are you using the [legacy](https://platform.openai.com/docs/api-reference/completions) or [latest chat completion API](https://platform.openai.com/docs/api-reference/chat/create) or another call?

Normally when I've done this myself in JavaScript I've used the Elasticsearch client to pull the required documents and pass via the prompt messages rather than pass the connection details to OpenAI. But for your error I would double check the connection details are correct and that the API key has the correct entitlements to the index as it looks like OpenAI can't access with the provided connection settings.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Saurabh\_Agrawal2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_agrawal2/32/123927_2.png) [@Saurabh\_Agrawal2](https://discuss.elastic.co/u/Saurabh_Agrawal2)\
**Post date:** [November 24, 2023, 11:07am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/3 "2023-11-24T11:07:47Z")

</div>

Hi @carly.richmond,  
Thanks for looking into it.

Here I am using Python, also this client is OpenAI client. I am trying to edit this question but not able to do so.

Here are the details:  
`Openai python version: 1.3.2`

code snippet:

```auto
import openai
client = openai.AzureOpenAI(
    base_url="my_base_url",
    api_key='my_api-key',
    api_version='2023-08-01-preview'
)
client.chat.completions.create(
        messages=message_text,
        model=deployment_id,
        extra_body={
            "dataSources": [
                {
                    "type": "Elasticsearch",
                    "parameters": {
                        "apiKey": "my_apiKey",
                        "endpoint": "my_endpoint",
                        "indexName": "my_indexName",
                    },
                }
            ]
        }
    )

```

If possible for you kindly update above question's code.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 24, 2023, 5:04pm UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/4 "2023-11-24T17:04:05Z")

</div>

Hi @Saurabh_Agrawal2,

Thanks for providing the additional snippet and confirming you're using the AzureOpenAI library. Is the `dataSources` setting compatible with other sources aside from AzureCognitiveSearch? That's the only option I seem to find examples for when I search for this setting.

---

<div class="post-metadata">

**Author:** ![Saurabh\_Agrawal2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh_agrawal2/32/123927_2.png) [@Saurabh\_Agrawal2](https://discuss.elastic.co/u/Saurabh_Agrawal2)\
**Post date:** [November 28, 2023, 4:57am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/5 "2023-11-28T04:57:41Z")

</div>

> [@carly.richmond](#):
>
> when

Hi @carly.richmond,

Yes I can see if update datasource type to some unsupported option then I get error:

`openai.BadRequestError: Error code: 400 - {'error': {'requestid': 'abc-xyx-12bg-aa88-b7abc9a415fb', 'code': 400, 'message': "Validation error at #/dataSources/0: Input tag 'test_Elasticsearch' found using 'type' does not match any of the expected tags: 'AzureCognitiveSearch', 'Elasticsearch', 'AzureCosmosDB', 'Pinecone', 'AzureMLIndex'"}}`

Which clearly states it supports Elasticsearch.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 28, 2023, 9:59am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/6 "2023-11-28T09:59:18Z")

</div>

Thanks for confirming @Saurabh_Agrawal2. I would recommend double checking your key, endpoint and index name are correct, and that the API key has sufficient permissions on the index you are trying to access.

Aside from that, as it's an Azure API, it might be worth trying the [Azure forums](https://azure.microsoft.com/en-gb/support/community) if the key looks to have sufficient permissions to see if the bad request error can be resolved.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 26, 2023, 9:59am UTC](https://discuss.elastic.co/t/openai-connect-with-elastic-search-datasource/347901/7 "2023-12-26T09:59:55Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
