yfinance: Getting json.decoder.JSONDecodeError: Expecting value

Getting this error randomly and the thread is stuck. Tried upgrading the library but doesn’t work.

Here’s the trace:

Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/yfinance/multi.py", line 169, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "/usr/local/lib/python3.9/site-packages/yfinance/multi.py", line 181, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "/usr/local/lib/python3.9/site-packages/yfinance/base.py", line 162, in history
    data = data.json()
  File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)```

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 32 (2 by maintainers)

Most upvoted comments

same issue here on version == 0.1.60, however resolved pip install yfinance==0.1.62 in my case.

@ranaroussi From what I’ve seen this error isn’t thrown from the library. I can see the error in the logs but the thread that called the download function hangs. Any external logic I can implement here is irrelevant due to that.

Adding ‘threads=False’ seems to have mitigated to issue for me. Not sure if it’s a permanent solution yet.

Same issue here. I’ve been using the same script for the past few weeks with no issues. Bulk downloading chunks of 100 tickers for appx 2500-4000 tickers. Just started getting this error this week. Something has changed on Yahoo’s end.

I can’t reproduce this with 0.1.63… 🤷‍♂️

asciicast

Successfully downloaded all nasdaq tickers data this AM in chunks of 100 with threads = False. Execution time went from .3 secs per stock to .9 secs, but it worked.

Just tested something out. Ran identical code on my main machine and my low powered laptop.

My (4.2GHz, 6C/12T) desktop got this error numerous times while downloading data for approx 11,000 tickers.

My (2.6GHz, 2C/4T) laptop didn’t get the error once. Both machines have a similar network connection speed wise.

So, @krazykoder , while a delay definitely helps, I don’t think it needs to be as large as the one you are using.

Maybe as a group we can figure out the best amount of a delay to implement and potentially create a PR to have this implemented when the count of tickers is over a certain number?

@Ozymandias1700 i concur. i did see different behavior on different machines with different compute power. I think this has something to do with how fast the threads (default) are generated or frequency of requests that are sent to yahoo servers. Let me try with threads=False, will report back.

yf.download(" ".join(symbols),start="2020-01-01",end="2021-01-01").head()

And this is what happens:

[                       0%                       ]  4 of 8800 completed

The only solution that worked for me over 2 days.

  • Say good bye to live.
  • Say good bye to 8000+ ticker download all at once. 😭
  • Split the symbols into chunks of 100, don’t download more than 100 tickers and maintain a 5 min gap. I prefer download after-hours.

@CKDarling yf.__version__ Out[176]: '0.1.63'

Yes are right, if you run it with a couple of symbols and define start and end date it does work. However I tried to run this for 8800 symbols:

yf.download(" ".join(symbols),start="2020-01-01",end="2021-01-01").head()

And this is what happens:

[                       0%                       ]  4 of 8800 completed
Exception in thread Thread-13006:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\yfinance\multi.py", line 169, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "C:\ProgramData\Anaconda3\lib\site-packages\yfinance\multi.py", line 181, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "C:\ProgramData\Anaconda3\lib\site-packages\yfinance\base.py", line 162, in history
    data = data.json()
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


@CKDarling try runing that code for a large number of tickers.

For downloading that many tickers, you should implement some sort of forced delay, some retries, and probably use several network cards  with different ips to “fight” yahoo’s limiters.

Is it possible for the library to report errors or break execution so that we can try-catch it. That way we can enforce a delay between the ticker downloads. Maybe this already exists.

@ranaroussi it doesn’t happen for all tickers, only some. I attempted to download ~11,000 tickers data and got this error many times. It also hangs at the end of the downloads sometimes but that’s a separate issue.