Elastic Transport for Python
Elastic Transport for Python provides transport classes and utilities shared among various Python Elastic client libraries. It serves as a low-level HTTP client, powering the official Elasticsearch Python client and other Elastic projects. The library is currently at version 9.2.1 and follows a release cadence aligned with the major and minor versions of the Elastic Stack, with patch numbers incremented for bug fixes within a minor release.
Warnings
- breaking Python 3.8 and 3.9 support was removed in version 9.2.0. Python 3.7 support was dropped in 8.15.0.
- breaking The exception hierarchy changed significantly in version 8.x. `TransportError` now only covers transport-level errors (e.g., connection timeouts), while `ApiError` must be used for API-specific errors (e.g., index not found).
- gotcha When using `HttpxAsyncHttpNode`, 404 responses no longer raise exceptions by default; instead, the response with status 404 is returned. This changes the expected error handling behavior for 'not found' scenarios.
- gotcha Ensure compatibility with `httpx` by installing `httpx v0.28.0+` if using the `httpx` backend. Older versions may cause compatibility issues.
- gotcha The `urllib3` library is the default HTTP client backend. To use `requests`, `aiohttp`, or `httpx`, you must install the respective extra dependencies (e.g., `elastic-transport[requests]`) and explicitly pass the corresponding `NodeConfig` or `node_class` to the `Transport` constructor.
Install
-
pip install elastic-transport -
pip install elastic-transport[requests] -
pip install elastic-transport[aiohttp] -
pip install elastic-transport[httpx]
Imports
- Transport
from elastic_transport import Transport
- RequestsHttpNode
from elastic_transport import RequestsHttpNode
- AiohttpHttpNode
from elastic_transport import AiohttpHttpNode
- HttpxHttpNode
from elastic_transport import HttpxHttpNode
- HttpxAsyncHttpNode
from elastic_transport import HttpxAsyncHttpNode
- ApiError
from elastic_transport import ApiError
- TransportError
from elastic_transport import TransportError
Quickstart
import os
from elastic_transport import Transport, NodeConfig
# Configure nodes - replace with your Elasticsearch host(s)
# For local testing, default to 'http://localhost:9200'
# For cloud, use CLOUD_ID and API_KEY environment variables
cloud_id = os.environ.get('ELASTIC_CLOUD_ID', '')
api_key = os.environ.get('ELASTIC_API_KEY', '')
node_configs = [
NodeConfig("http://localhost:9200")
]
if cloud_id and api_key:
# Use Cloud ID and API Key for Elastic Cloud
transport = Transport(
node_configs=[], # node_configs are derived from cloud_id
cloud_id=cloud_id,
api_key=(api_key,)
)
else:
transport = Transport(node_configs=node_configs)
try:
# Perform a GET request to the root ('/') which often returns cluster info
# The response is a tuple of (ApiResponseMeta, deserialized_body)
meta, body = transport.perform_request("GET", "/")
print("Successfully connected to Elasticsearch!")
print(f"Cluster Name: {body.get('cluster_name')}")
print(f"Status Code: {meta.status}")
except Exception as e:
print(f"Failed to connect or perform request: {e}")
print("Ensure Elasticsearch is running and accessible at the specified host(s)")
print("Or check your ELASTIC_CLOUD_ID and ELASTIC_API_KEY environment variables.")
transport.close()