From 1d7e6ab9eb2ec07fea70757266a5b73aa0a44942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=B0=E6=9C=9B?= <2304238405@qq.com> Date: Thu, 19 Mar 2026 14:18:58 +0800 Subject: [PATCH] Normalize module client base_url trailing slash --- src/openai/__init__.py | 2 +- tests/test_module_client.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/openai/__init__.py b/src/openai/__init__.py index b2093ada68..d37315a9d9 100644 --- a/src/openai/__init__.py +++ b/src/openai/__init__.py @@ -204,7 +204,7 @@ def webhook_secret(self, value: str | None) -> None: # type: ignore @override def base_url(self) -> _httpx.URL: if base_url is not None: - return _httpx.URL(base_url) + return self._enforce_trailing_slash(_httpx.URL(base_url)) return super().base_url diff --git a/tests/test_module_client.py b/tests/test_module_client.py index 9c9a1addab..2d5a928c60 100644 --- a/tests/test_module_client.py +++ b/tests/test_module_client.py @@ -42,8 +42,15 @@ def test_base_url_option() -> None: openai.base_url = "http://foo.com" - assert openai.base_url == URL("http://foo.com") - assert openai.completions._client.base_url == URL("http://foo.com") + assert openai.base_url == "http://foo.com" + assert openai.completions._client.base_url.raw_path == b"/" + + +def test_base_url_option_without_trailing_slash() -> None: + openai.base_url = "http://foo.com/custom/path" + + assert openai.base_url == "http://foo.com/custom/path" + assert openai.completions._client.base_url == URL("http://foo.com/custom/path/") def test_timeout_option() -> None: