Algolia DevCon
Oct. 2–3 2024, virtual.

Upgrade the Python API clients to v4

The latest major version of the algoliasearch package is v4. This page lists the breaking changes since the last release, v3.

Async only

The Python API client only supports asynchronous Python code with asyncio.

Client imports

The imports for the API clients changed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Search API
from algoliasearch.search.client import SearchClient
# Recommend API
from algoliasearch.recommend.client import RecommendClient
# A/B testing
from algoliasearch.abtesting.client import AbtestingClient
# Analytics API
from algoliasearch.analytics.client import AnalyticsClient
# Ingestion API
from algoliasearch.ingestion.client import IngestionClient
# Insights API
from algoliasearch.insights.client import InsightsClient
# Monitoring API
from algoliaserarch.monitoring.client import MonitoringClient
# Personalization API
from algoliasearch.personalization.client import PersonalizationClient
# Query Suggestions API
from algoliasearch.query_suggestions.client import QuerySuggestionsClient
# Usage API
from algoliasearch.usage.client import UsageClient

Client creation

To create a client, create an instance of the SearchClient class. The SearchClient.create() method has been removed. The API clients support the async with syntax to automatically close open connections.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from algoliasearch.search.client import SearchClient
from algoliasearch.search.config import SearchConfig

app_id = "ALGOLIA_APPLICATION_ID"
api_key = "ALGOLIA_API_KEY"


async def main():
    # With positional arguments
    async with SearchClient(app_id, api_key) as client:
        ...

    # With keyword arguments
    async with SearchClient(app_id=app_id, api_key=api_key) as client:
        ...

  # With a custom configuration
    async with SearchClient(
        app_id=app_id,
        api_key=api_key,
        config=SearchConfig()
    ) as client:
        ...


if __name__ == "__main__":
    asyncio.run(main())

Removal of init_index

All methods are methods of a client instance. The init_index method of the SearchClient has been removed. Instead, all methods require a index_name parameter.

1
2
3
4
5
6
7
8
# v3
client = SearchClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
index = client.init_index("ALGOLIA_INDEX_NAME")
index.search("QUERY")

# v4
async with SearchClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY") as client:
    client.search_single_index("ALGOLIA_INDEX_NAME", {"query": "QUERY"})

Wait for tasks

The wait method has been removed. Instead, use one of the following helpers:

Copy or move indices, settings, synonyms, or rules

Use the operation_index method, which replaces the following methods:

  • copy_index
  • move_index
  • copy_rules
  • copy_synonyms
  • copy_settings
Did you find this page helpful?
Python API clients v4