# VectorstoreIndexCrerator does't find openai API KEY

**URL:** <https://community.deeplearning.ai/t/vectorstoreindexcrerator-doest-find-openai-api-key/364434>\
**Category:** AI Discussions\
**Created:** [June 27, 2023, 10:09am UTC](https://community.deeplearning.ai/t/vectorstoreindexcrerator-doest-find-openai-api-key/364434 "2023-06-27T10:09:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Netwolf](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Netwolf](https://community.deeplearning.ai/u/Netwolf)\
**Post date:** [June 27, 2023, 10:09am UTC](https://community.deeplearning.ai/t/vectorstoreindexcrerator-doest-find-openai-api-key/364434/1 "2023-06-27T10:09:46Z")

</div>

There shoud be a place to send the parameter opnai.api-key somewhere

I can’t get rid of this error:

```
index = VectorstoreIndexCreator(

```

File “pydantic/main.py”, line 339, in pydantic.main.BaseModel. **init**  
File “pydantic/main.py”, line 1066, in pydantic.main.validate\_model  
File “pydantic/fields.py”, line 439, in pydantic.fields.ModelField.get\_default  
File “pydantic/main.py”, line 341, in pydantic.main.BaseModel. **init**  
pydantic.error\_wrappers.ValidationError: 1 validation error for OpenAIEmbeddings  
**root**  
Did not find openai\_api\_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. (type=value\_error)

Cna somebody help pls?

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/ai_curious/32/128003_2.png) [@ai\_curious](https://community.deeplearning.ai/u/ai_curious)\
**Post date:** [June 27, 2023, 11:14am UTC](https://community.deeplearning.ai/t/vectorstoreindexcrerator-doest-find-openai-api-key/364434/2 "2023-06-27T11:14:19Z")

</div>

As far as I could tell, when using LangChain API, setting the key directly in the code is not an available option: you must set it in the environment. That can be done either through the OS or in the code, but it must be done that way. There are existing threads that discuss this and show how.

---

<div class="post-metadata">

**Author:** ![Netwolf](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Netwolf](https://community.deeplearning.ai/u/Netwolf)\
**Post date:** [July 18, 2023, 3:55pm UTC](https://community.deeplearning.ai/t/vectorstoreindexcrerator-doest-find-openai-api-key/364434/3 "2023-07-18T15:55:56Z")

</div>

> [@ai\_curious](#):
>
> setting the key directly in the code is not an available option: you must set it in the environment.

When using google colab, this works:

!pip install openai

!pip install python-dotenv

import openai

import os

import sys

import dotenv

from dotenv import dotenv\_values

env\_vars = dotenv\_values(‘your .env file path’)

sys.path.append(‘your .env file path’)

openai.api\_key = env\_vars[‘OPENAI\_API\_KEY’] # get the variable from ,env file

os.environ[‘OPENAI\_API\_KEY’] = env\_vars[‘OPENAI\_API\_KEY’] # sets the environ variable

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/ai_curious/32/128003_2.png) [@ai\_curious](https://community.deeplearning.ai/u/ai_curious)\
**Post date:** [July 18, 2023, 4:24pm UTC](https://community.deeplearning.ai/t/vectorstoreindexcrerator-doest-find-openai-api-key/364434/4 "2023-07-18T16:24:08Z")

</div>

> [@Netwolf](#):
>
> os.environ[‘OPENAI\_API\_KEY’] = env\_vars[‘OPENAI\_API\_KEY’] # sets the environ variable

I think that is what I suggested above, but thanks for showing it in Python. What _doesn’t_ work is just assigning a string literal value to the key as it was done in some of the early examples of the prompt engineering course. Then it was an option to do it that way. With langchain, not so.
