Solution to UnicodeEncodeError while calling OpenAI API

Full error:
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u201c’ in position 7: ordinal not in range(128)

It turns out this error arose from the way I was passing the OPENAI_API_KEY variable.

In terminal, I would execute:
export OPENAI_API_KEY=“****”

which I would copy and paste in from notes on mac.

Turns out notes automatically converts the standard double quotation (") into a curly quotation (”), and that was the elusive non ascii character the whole damn time.

So the solution is to type the command directly into the terminal using the standard double quotation character that I believe is on most keyboards, or just to otherwise make sure that there are no curly quotations or other non-ascii characters in the OPENAI_API_KEY variable.

I’m probably just an idiot here but this was demoralizing as hell. ChatGPT couldn’t figure it out either. Not going to lie, I’m pretty new to code and this had me stunted on a project for over two weeks. Hope this helps.

1 Like