From aaa4d4178aac8c97a6164ea0547e0aba66a584c6 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Fri, 6 Dec 2024 20:59:58 -0800 Subject: [PATCH] litellm not honoring OPENAI_ORGANIZATION env var (#7066) * fix setting organization using .env vars * test_completion_bad_org * test_completion_bad_org --- litellm/main.py | 3 ++- tests/llm_translation/test_openai.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/litellm/main.py b/litellm/main.py index d33436a96f7..17e018c056d 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -1527,12 +1527,13 @@ def completion( # type: ignore # noqa: PLR0915 or get_secret("OPENAI_API_BASE") or "https://api.openai.com/v1" ) - openai.organization = ( + organization = ( organization or litellm.organization or get_secret("OPENAI_ORGANIZATION") or None # default - https://github.com/openai/openai-python/blob/284c1799070c723c6a553337134148a7ab088dd8/openai/util.py#L105 ) + openai.organization = organization # set API KEY api_key = ( api_key diff --git a/tests/llm_translation/test_openai.py b/tests/llm_translation/test_openai.py index 87e3a8a6143..4d4fa6566f6 100644 --- a/tests/llm_translation/test_openai.py +++ b/tests/llm_translation/test_openai.py @@ -278,3 +278,25 @@ class TestOpenAIChatCompletion(BaseLLMChatTest): def test_tool_call_no_arguments(self, tool_call_no_arguments): """Test that tool calls with no arguments is translated correctly. Relevant issue: https://github.com/BerriAI/litellm/issues/6833""" pass + + +def test_completion_bad_org(): + import litellm + + litellm.set_verbose = True + _old_org = os.environ.get("OPENAI_ORGANIZATION", None) + os.environ["OPENAI_ORGANIZATION"] = "bad-org" + messages = [{"role": "user", "content": "hi"}] + + with pytest.raises(Exception) as exc_info: + comp = litellm.completion( + model="gpt-4o-mini", messages=messages, organization="bad-org" + ) + + print(exc_info.value) + assert "No such organization: bad-org" in str(exc_info.value) + + if _old_org is not None: + os.environ["OPENAI_ORGANIZATION"] = _old_org + else: + del os.environ["OPENAI_ORGANIZATION"]