[bug] Guardrails prohibits OpenAI from utilizing env-var based API key
Describe the bug I am trying to run this example.
It returns an error:
The callable fn passed to Guard(fn, ...) failed with the following error: Error code: 401 - {'error': {'message': 'Incorrect API key provided'}}.
However, the API key is working perfectly when I test it without Guardrails.
Library version: guardrails-ai-0.4.2 openai 1.14.3 Python 3.8.18
@ronaldonunes thanks for raising this issue. To help us test and identify the problem could you share how are you setting the API key? Are you setting it via the OPENAI_API_KEY environment variable, or are you passing it via the api_key kwarg when calling the guard?
I set the API key using:
os.environ["OPENAI_API_KEY"] = ""
I tried both ways and it did'n work.
Would you mind sharing more details about your setup? For example, are you assigning the environment variable in the notebook or outside of it? Are you assigning the value statically or referencing another environment variable? etc.
I'm not able to replicate the issue with the information provided.
Hi @CalebCourier, thank you for helping out.
This is how I set the environment variable inside my jupyter notebook and the code to call the llm inside guardarails:
os.environ["OPENAI_API_KEY"] = open("key.txt", 'rb').read().decode('utf-8')
raw_llm_response, validated_response, *rest = guard( openai.completions.create, prompt_params={"document": content[:6000]}, model="gpt-35", max_tokens=2048, temperature=0 )
@ronaldonunes After testing with the code you shared I'm still not able to replicate the issue you're facing. I was however able to obtain the same error message if the key was corrupted in some way; i.e. a character was missing or an extra character added. Could you verify that the value returned by open("key.txt", 'rb').read().decode('utf-8') is what is expected?
I double-checked, and the key is not corrupted. To be honest, I still have not identified the problem. The only clue that I have is this error message about the API Key.
Thank you for spending time trying to figure out the problem.
@ronaldonunes does your API KEY perhaps belong to an organization within OpenAI? I was getting an error and had to add this line of code prior to calling guard(...)
openai.organization=org-....
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.
This issue was closed because it has been stalled for 14 days with no activity.