Skip to content

Commit ca5fdc6

Browse files
feat(client): support reading the base url from an env variable (#829)
1 parent 8407a27 commit ca5fdc6

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ import httpx
437437
from openai import OpenAI
438438

439439
client = OpenAI(
440+
# Or use the `OPENAI_BASE_URL` env var
440441
base_url="http://my.test.server.example.com:8083",
441442
http_client=httpx.Client(
442443
proxies="http://my.test.proxy.example.com",

src/openai/_client.py

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def __init__(
9999
organization = os.environ.get("OPENAI_ORG_ID")
100100
self.organization = organization
101101

102+
if base_url is None:
103+
base_url = os.environ.get("OPENAI_BASE_URL")
102104
if base_url is None:
103105
base_url = f"https://api.openai.com/v1"
104106

@@ -307,6 +309,8 @@ def __init__(
307309
organization = os.environ.get("OPENAI_ORG_ID")
308310
self.organization = organization
309311

312+
if base_url is None:
313+
base_url = os.environ.get("OPENAI_BASE_URL")
310314
if base_url is None:
311315
base_url = f"https://api.openai.com/v1"
312316

tests/test_client.py

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
make_request_options,
2727
)
2828

29+
from .utils import update_env
30+
2931
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
3032
api_key = "My API Key"
3133

@@ -399,6 +401,11 @@ class Model2(BaseModel):
399401
assert isinstance(response, Model1)
400402
assert response.foo == 1
401403

404+
def test_base_url_env(self) -> None:
405+
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
406+
client = OpenAI(api_key=api_key, _strict_response_validation=True)
407+
assert client.base_url == "http://localhost:5000/from/env/"
408+
402409
@pytest.mark.parametrize(
403410
"client",
404411
[
@@ -932,6 +939,11 @@ class Model2(BaseModel):
932939
assert isinstance(response, Model1)
933940
assert response.foo == 1
934941

942+
def test_base_url_env(self) -> None:
943+
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
944+
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
945+
assert client.base_url == "http://localhost:5000/from/env/"
946+
935947
@pytest.mark.parametrize(
936948
"client",
937949
[

tests/utils.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

3+
import os
34
import traceback
4-
from typing import Any, TypeVar, cast
5+
import contextlib
6+
from typing import Any, TypeVar, Iterator, cast
57
from datetime import date, datetime
68
from typing_extensions import Literal, get_args, get_origin, assert_type
79

@@ -103,3 +105,16 @@ def _assert_list_type(type_: type[object], value: object) -> None:
103105
inner_type = get_args(type_)[0]
104106
for entry in value:
105107
assert_type(inner_type, entry) # type: ignore
108+
109+
110+
@contextlib.contextmanager
111+
def update_env(**new_env: str) -> Iterator[None]:
112+
old = os.environ.copy()
113+
114+
try:
115+
os.environ.update(new_env)
116+
117+
yield None
118+
finally:
119+
os.environ.clear()
120+
os.environ.update(old)

0 commit comments

Comments
 (0)