# Key error (even after using a right API key)

**URL:** <https://community.deeplearning.ai/t/key-error-even-after-using-a-right-api-key/351978>\
**Category:** LangChain for LLM Application Development\
**Created:** [June 7, 2023, 3:53pm UTC](https://community.deeplearning.ai/t/key-error-even-after-using-a-right-api-key/351978 "2023-06-07T15:53:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harsh\_Duche](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@Harsh\_Duche](https://community.deeplearning.ai/u/Harsh_Duche)\
**Post date:** [June 7, 2023, 3:53pm UTC](https://community.deeplearning.ai/t/key-error-even-after-using-a-right-api-key/351978/1 "2023-06-07T15:53:19Z")

</div>

* * *

KeyError Traceback (most recent call last)  
Cell In[21], line 6  
from dotenv import load\_dotenv, find\_dotenv  
\_ = load\_dotenv(find\_dotenv()) # read local .env file  
----\> openai.api\_key = os.environ[‘My\_API\_KEY’]

File :679, in **getitem** (self, key)

KeyError: ‘My\_API\_KEY’

---

<div class="post-metadata">

**Author:** ![reinoudbosch](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@reinoudbosch](https://community.deeplearning.ai/u/reinoudbosch)\
**Post date:** [June 8, 2023, 9:50pm UTC](https://community.deeplearning.ai/t/key-error-even-after-using-a-right-api-key/351978/2 "2023-06-08T21:50:59Z")

</div>

Hi Harsh\_Duche,

If you’re running this in the learning platform you can leave the key as it is, i.e.  
openai.api\_key = os.environ[‘OPENAI\_API\_KEY’]  
(do not replace the string ‘OPEN\_API\_KEY’)

---

<div class="post-metadata">

**Author:** ![hsetin](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@hsetin](https://community.deeplearning.ai/u/hsetin)\
**Post date:** [July 8, 2023, 11:02pm UTC](https://community.deeplearning.ai/t/key-error-even-after-using-a-right-api-key/351978/3 "2023-07-08T23:02:58Z")

</div>

When i use the following:

openai.api\_key=os.environ[‘sk- _Myhiddenkey_’]

I get this key error:

KeyError Traceback (most recent call last)  
Cell In[48], line 6  
4 from dotenv import load\_dotenv, find\_dotenv  
5 \_ = load\_dotenv(find\_dotenv()) # read local .env file  
----\> 6 openai.api\_key=os.environ[‘sk-_Myhiddenkey_’']

File ~\AppData\Local\anaconda3\lib\os.py:680, in \_Environ. **getitem** (self, key)  
677 value = self.\_data[self.encodekey(key)]  
678 except KeyError:  
679 # raise KeyError with the original key value  
 → 680 raise KeyError(key) from None  
681 return self.decodevalue(value)

KeyError: ‘sk-_Myhiddenkey_’

When i set my key directly ignoring the os.environ, it works fine, but later complains about arguments used for the os.environ function  
openai.api\_key=‘sk- _Myhiddenkey_’

Has anyone had this issue. This is at the beginning so i can not complete this course if thsi is not fixed. I think this is something todo with variabl declaration from teh prompt engineering course.

---

<div class="post-metadata">

**Author:** ![hsetin](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@hsetin](https://community.deeplearning.ai/u/hsetin)\
**Post date:** [July 19, 2023, 6:56pm UTC](https://community.deeplearning.ai/t/key-error-even-after-using-a-right-api-key/351978/4 "2023-07-19T18:56:16Z")

</div>

Yes, I am also having exactly the same issue.  
KeyError Traceback (most recent call last)  
Cell In[12], line 4  
2 \_ = load\_dotenv(find\_dotenv()) # read local .env file  
3 ##OPENAI\_API\_KEY=‘sk-Mykey’ I was trying to assign key to this variable, but didn’t work.  
----\> 4 openai.api\_key =os.environ[‘sk-Mykey’]

File ~\AppData\Local\anaconda3\lib\os.py:680, in \_Environ. **getitem** (self, key)  
677 value = self.\_data[self.encodekey(key)]  
678 except KeyError:  
679 # raise KeyError with the original key value  
 → 680 raise KeyError(key) from None  
681 return self.decodevalue(value)

KeyError: ‘sk-My key’
