# ChatOpenAI() suggesting validation error

**URL:** <https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857>\
**Category:** ChatGPT Prompt Engineering for Developers\
**Created:** [June 10, 2023, 5:12pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857 "2023-06-10T17:12:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Warren\_K](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@Warren\_K](https://community.deeplearning.ai/u/Warren_K)\
**Post date:** [June 10, 2023, 5:12pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/1 "2023-06-10T17:12:30Z")

</div>

I have been trying to implement the short course provided by Harrison Chase and Andrew on models,prompts, and parsers. I want to copy the code provided in the course into my notebook for learning. The first part of the code works by passing openai\_api\_key=“sk-xxxx”. But when I import langchain and pass ChatOpenAi(), I get validation error requesting no API key found. Is it that the code provided in the course cannot work outside the platforms notebook or I am not passing the correct class for ChatOpenAI()?

---

<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 10, 2023, 5:19pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/2 "2023-06-10T17:19:28Z")

</div>

For lang chain you have to set the value in the environment. Either do it from the operating system before you launch the notebook, or do it inside the notebook. Either way, just assigning the string works for using openai API, but not for langchain.

---

<div class="post-metadata">

**Author:** ![Mujassim\_Jamal](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/mujassim_jamal/32/53828_2.png) [@Mujassim\_Jamal](https://community.deeplearning.ai/u/Mujassim_Jamal)\
**Post date:** [June 10, 2023, 5:20pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/3 "2023-06-10T17:20:37Z")

</div>

> [@Warren\_K](#):
>
> openai\_api\_key=“sk-xxxx”

I think, It’s a `openai.api_key`, not `openai_api_key`.

---

<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 10, 2023, 5:26pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/4 "2023-06-10T17:26:28Z")

</div>

Just assigning the string doesn’t work for `langchain` api calls.

```auto
openai.api_key = 'string...'

```

is necessary but not sufficient. Add this

```auto
os.environ['OPENAI_API_KEY'] = openai.api_key

```

By doing that, I was able to run the `langchain` exercises on my laptop

---

<div class="post-metadata">

**Author:** ![Warren\_K](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@Warren\_K](https://community.deeplearning.ai/u/Warren_K)\
**Post date:** [June 10, 2023, 5:38pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/5 "2023-06-10T17:38:35Z")

</div>

can you provide the format to which you were able to use the ChatopenAI(). like  
import modules

import langchain

pass the ChatOpenAI() so I can reference.

---

<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 10, 2023, 5:58pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/6 "2023-06-10T17:58:18Z")

</div>

Sorry but I don’t understand the question. I just downloaded the notebooks and data files and ran them locally. For the prompt eng exercises I had been just sticking my key in as a string, but that threw an error which lead me to the solution shared above. Other than that, I didn’t change the notebooks.

---

<div class="post-metadata">

**Author:** ![Warren\_K](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@Warren\_K](https://community.deeplearning.ai/u/Warren_K)\
**Post date:** [June 10, 2023, 6:45pm UTC](https://community.deeplearning.ai/t/chatopenai-suggesting-validation-error/353857/7 "2023-06-10T18:45:03Z")

</div>

Thanks. actually, it did work. its been helpful.
