ESRE class - Python elasticsearch.BadRequestError

Course: Elasticsearch Relevance Engine (ESRE) Engineer
Version: 8.11.1
Question: Python Elasticsearch error

Hello, I am trying to work through the ESRE class and am stuck at Lab 3.1-1

When I try to run this code:

#test-elasticsearch.py
from elasticsearch import Elasticsearch

es = Elasticsearch (
  "https://localhost:9200",
  basic_auth = ("elastic", "nonprodpwd"),
  verify_certs = False
)

print(es.info())

I get the following error:

elastic@ip-172-31-38-174:~/apps/langchain-apps$ python test-elasticsearch.py 
Traceback (most recent call last):
  File "test-elasticsearch.py", line 10, in <module>
    print(es.info())
  File "/home/elastic/.local/lib/python3.8/site-packages/elasticsearch/_sync/client/utils.py", line 415, in wrapped
    return api(*args, **kwargs)
  File "/home/elastic/.local/lib/python3.8/site-packages/elasticsearch/_sync/client/__init__.py", line 2991, in info
    return self.perform_request(  # type: ignore[return-value]
  File "/home/elastic/.local/lib/python3.8/site-packages/elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
  File "/home/elastic/.local/lib/python3.8/site-packages/elasticsearch/_sync/client/_base.py", line 351, in _perform_request
    raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
elasticsearch.BadRequestError: BadRequestError(400, 'media_type_header_exception', 'Invalid media-type value on headers [Content-Type, Accept]', Accept version must be either version 8 or 7, but found 9. Accept=application/vnd.elasticsearch+json; compatible-with=9)
elastic@ip-172-31-38-174:~/apps/langchain-apps$

Any help would be appreciated. Thank you! -will

Hello @newbieWill ,

Welcome to the community!!

As per the error "elasticsearch.BadRequestError: BadRequestError(400, 'media_type_header_exception', 'Invalid media-type value on headers [Content-Type, Accept]', Accept version must be either version 8 or 7, but found 9. Accept=application/vnd.elasticsearch+json; compatible-with=9)"

We can perform below steps :

pip uninstall elasticsearch

Once uninstalled install 8.x :

pip install elasticsearch==8.18.1

Now try the same command to connect :

python test-elasticsearch.py

Thanks!!

Thanks - That worked!

Sorry for the delayed response :slight_smile:

1 Like