yfinance: TypeError: unsupported operand type(s) for |: 'type' and '_GenericAlias'

For the past year, I have received no errors when requesting quotes. In the past week, I was receiving this error, Error - YFRateLimitError(‘Too Many Requests. Rate limited. Try after a while.’) and now I receive the following error.

Describe bug

import yfinance as yf
Traceback (most recent call last):

  Cell In[14], line 1
    import yfinance as yf

  File ~\anaconda3\lib\site-packages\yfinance\__init__.py:25
    from .ticker import Ticker

  File ~\anaconda3\lib\site-packages\yfinance\ticker.py:29
    from .base import TickerBase

  File ~\anaconda3\lib\site-packages\yfinance\base.py:37
    from .live import WebSocket

  File ~\anaconda3\lib\site-packages\yfinance\live.py:39
    class AsyncWebSocket(BaseWebSocket):

  File ~\anaconda3\lib\site-packages\yfinance\live.py:87 in AsyncWebSocket
    async def subscribe(self, symbols: str | List[str]):

TypeError: unsupported operand type(s) for |: 'type' and '_GenericAlias'

Simple code that reproduces your problem

import yfinance as yf Traceback (most recent call last):

Cell In[14], line 1 import yfinance as yf

File ~\anaconda3\lib\site-packages\yfinance_init_.py:25 from .ticker import Ticker

File ~\anaconda3\lib\site-packages\yfinance\ticker.py:29 from .base import TickerBase

File ~\anaconda3\lib\site-packages\yfinance\base.py:37 from .live import WebSocket

File ~\anaconda3\lib\site-packages\yfinance\live.py:39 class AsyncWebSocket(BaseWebSocket):

File ~\anaconda3\lib\site-packages\yfinance\live.py:87 in AsyncWebSocket async def subscribe(self, symbols: str | List[str]):

TypeError: unsupported operand type(s) for |: ‘type’ and ‘_GenericAlias’

Debug log from yf.enable_debug_mode()

import yfinance as yf Traceback (most recent call last):

Cell In[14], line 1 import yfinance as yf

File ~\anaconda3\lib\site-packages\yfinance_init_.py:25 from .ticker import Ticker

File ~\anaconda3\lib\site-packages\yfinance\ticker.py:29 from .base import TickerBase

File ~\anaconda3\lib\site-packages\yfinance\base.py:37 from .live import WebSocket

File ~\anaconda3\lib\site-packages\yfinance\live.py:39 class AsyncWebSocket(BaseWebSocket):

File ~\anaconda3\lib\site-packages\yfinance\live.py:87 in AsyncWebSocket async def subscribe(self, symbols: str | List[str]):

TypeError: unsupported operand type(s) for |: ‘type’ and ‘_GenericAlias’

Bad data proof

No response

yfinance version

print(yf.version) Gives me an error.

Python version

3.9.13

Operating system

Windows 11

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 25

Most upvoted comments

My bad, I missed those. Fixed now.

Many thanks for your efforts, but unfortunately that still fails for me with the error:


line 238, in WebSocket def subscribe(self, symbols: str | List[str]):


Try latest version

why do they make these changes!

But: It’s better to use Union for 3.8 compatibility

Instead of relying on quoted modern syntax, the idiomatic and more compatible version for Python 3.8 is:

from typing import Union, List

async def subscribe(self, symbols: Union[str, List[str]]):
async def unsubscribe(self, symbols: Union[str, List[str]]):

This worked for me. Go to live.py in /usr/local/lib/python3.8/dist-packages/yfinance/ and just add quotation marks around each of the str | List[str] lines in the subscribe and unsubscribe async functions

async def subscribe(self, symbols: "str | List[str]"): async def unsubscribe(self, symbols: "str | List[str]"):

Some recent update made yfinance incompatible with old python versions (using “|” for typing), use python 3.10+ and it should work