I'm trying to use algolia with my django project. However, i've been running into this strange exception that I have not seen before. When I run the command,
python3 manage.py algolia_reindex
I get the following
The following models were reindexed:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/util.py", line 144, in _validate_timeout
float(value)
TypeError: float() argument must be a string or a number, not 'tuple'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/contrib/algoliasearch/management/commands/algolia_reindex.py", line 22, in handle
batch_size=options.get('batchsize', None))
File "/usr/local/lib/python3.4/dist-packages/django/contrib/algoliasearch/models.py", line 207, in reindex_all
self.__tmp_index.clear_index()
File "/usr/local/lib/python3.4/dist-packages/algoliasearch/index.py", line 560, in clear_index
return self._perform_request(self.write_hosts, '/clear', 'POST')
File "/usr/local/lib/python3.4/dist-packages/algoliasearch/index.py", line 792, in _perform_request
params=params, body=body, is_search=is_search)
File "/usr/local/lib/python3.4/dist-packages/algoliasearch/client.py", line 499, in _perform_request
params=params, data=body, timeout=timeout)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 455, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 558, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 316, in send
timeout = TimeoutSauce(connect=timeout, read=timeout)
File "/usr/lib/python3/dist-packages/urllib3/util.py", line 116, in __init__
self._connect = self._validate_timeout(connect, 'connect')
File "/usr/lib/python3/dist-packages/urllib3/util.py", line 147, in _validate_timeout
"int or float." % (name, value))
ValueError: Timeout value connect was (1, 30), but it must be an int or float.
Can anyone help me resolve this problem. It doesn't seem to be from any of my files. So, I think this is just a config problem...
Answer
The error you're encountering occurs because urllib3
(which is used by the requests
library) is receiving a tuple
as the timeout value, which it can't process. Specifically, this is the relevant part of the error:
ValueError: Timeout value connect was (1, 30), but it must be an int or float.
This indicates that somewhere in your code (or in a configuration), the timeout value has been set as a tuple (1, 30)
instead of a single integer or float value.
Steps to Resolve
-
Check Timeout Configuration in Algolia Client:
The issue seems to be with how the timeout is configured for the Algolia client. Algolia’s client (algoliasearch
package) allows you to set timeouts, and it seems that one of those timeout values might have been incorrectly configured as a tuple.The timeout configuration might look something like this in your code:
client = algoliasearch.Client('YourApplicationID', 'YourAPIKey') client.set_timeout((1, 30)) # This should not be a tuple
Instead, it should be set with a single integer or float:
client = algoliasearch.Client('YourApplicationID', 'YourAPIKey') client.set_timeout(30) # Set timeout as an integer (in seconds)
If you have any such configurations in your project, make sure to check the timeout value and remove any tuple assignments. The
requests
library expects either an integer or a float for the timeout value, not a tuple. -
Check for Timeout Configuration in Django Settings:
Since you are using Django with Algolia, you may also have timeouts configured in your settings or in thedjango.contrib.algoliasearch
settings. You might have something like this insettings.py
:ALGOLIA_SEARCH_TIMEOUT = (1, 30) # This is invalid and should be an integer or float
It should be corrected to a single value:
ALGOLIA_SEARCH_TIMEOUT = 30 # Set timeout to 30 seconds (or whatever your preferred value is)
If you find such a configuration, change it to use a single integer or float value.
-
Check for Timeout in
algolia_reindex
Command:
The exception is being raised during thealgolia_reindex
command. Make sure that this command (or any related method) isn't passing an invalid timeout parameter.You can trace where the timeout is being set by inspecting the
algolia_reindex
command code. Specifically, look for where the request is being made to Algolia (likely in thereindex_all
method or similar). -
Verify with Latest
requests
andurllib3
:
You may also want to ensure that you're using the latest versions ofrequests
andurllib3
, as older versions might have bugs that result in misinterpretation of the timeout value. To update them, run:pip install --upgrade requests urllib3
-
Check Your
algoliasearch
Version:
Make sure youralgoliasearch
client is up to date as well. You can upgrade it using:pip install --upgrade algoliasearch
Additional Debugging Tips
-
Print Timeout Value: Add debugging statements to check the timeout value being passed to
requests
oralgoliasearch
:print("Timeout Value:", timeout)
This will help you understand where the tuple
(1, 30)
is coming from. -
Look at the Stack Trace: From the stack trace, it appears the error occurs during the
clear_index()
method. Look into thealgoliasearch
client and check if there’s any point where the timeout might be incorrectly set.
Final Thoughts
The problem is related to how the timeout is configured in the Algolia client or your Django settings. The timeout should be a single integer or float (e.g., 30
for 30 seconds), not a tuple. Once you correct this configuration, the error should be resolved.