Skip to content

Issue with populating api configs using environment variables after importing openai #557

@lumoslnt

Description

@lumoslnt

Describe the bug

I'm encountering an issue where I am unable to populate the api_key, api_base, api_type, and api_version using environment variables.

The openai package seems to read the api_key, api_base, api_type, and api_version from environment variables only at the moment it is imported. If these api configs are specified after the openai package is imported, they are not recognized.

The code is in here.

To Reproduce

Due to python's caching mechanism, to reproduce this issue, the openai package must be reloaded.

Code snippets

import openai
import os

os.environ["OPENAI_API_KEY"] = "<api_key>"
os.environ["OPENAI_API_BASE"] = "<api_base>"
os.environ["OPENAI_API_TYPE"] = "<api_type>"
os.environ["OPENAI_API_VERSION"] = "<api_version>"

openai.Completion.create(prompt="Hello", engine="text-davinci-003", max_tokens=5)

OS

Windows 11

Python version

Python v3.8.16

Library version

openai-python v0.27.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed in v1Issues addressed by the v1 beta

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions