Skip to content

Commit 0e89bca

Browse files
committed
Fix inconsistent base_url behavior in module client and OpenAIClient
Fixes openai#1373 Update `base_url` behavior to enforce trailing slash in module client * Modify `src/openai/__init__.py` to enforce a trailing slash for `base_url` in the `_ModuleClient` class. * Add a helper method `_enforce_trailing_slash` to the `_ModuleClient` class. * Update tests in `tests/test_module_client.py` to verify the consistent behavior of `base_url` in both module client and `OpenAIClient`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/openai/openai-python/issues/1373?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 64af9e8 commit 0e89bca

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/openai/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def project(self, value: str | None) -> None: # type: ignore
182182
@override
183183
def base_url(self) -> _httpx.URL:
184184
if base_url is not None:
185-
return _httpx.URL(base_url)
185+
return self._enforce_trailing_slash(_httpx.URL(base_url))
186186

187187
return super().base_url
188188

@@ -245,6 +245,11 @@ def _client(self, value: _httpx.Client) -> None: # type: ignore
245245

246246
http_client = value
247247

248+
def _enforce_trailing_slash(self, url: _httpx.URL) -> _httpx.URL:
249+
if url.raw_path.endswith(b"/"):
250+
return url
251+
return url.copy_with(raw_path=url.raw_path + b"/")
252+
248253

249254
class _AzureModuleClient(_ModuleClient, AzureOpenAI): # type: ignore
250255
...

tests/test_module_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def test_base_url_option() -> None:
4141

4242
openai.base_url = "http://foo.com"
4343

44-
assert openai.base_url == URL("http://foo.com")
45-
assert openai.completions._client.base_url == URL("http://foo.com")
44+
assert openai.base_url == URL("http://foo.com/")
45+
assert openai.completions._client.base_url == URL("http://foo.com/")
4646

4747

4848
def test_timeout_option() -> None:

0 commit comments

Comments
 (0)