Skip to content

Fix inconsistent base_url behavior in module client and OpenAIClient - #2157

Closed
SmartManoj wants to merge 2 commits into
openai:mainfrom
SmartManoj:fix-base-url
Closed

SmartManoj wants to merge 2 commits into
openai:mainfrom
SmartManoj:fix-base-url

Conversation

@SmartManoj

@SmartManoj SmartManoj commented Mar 2, 2025 •

Copy link
Copy Markdown

Fixes #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.

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).
@SmartManoj
SmartManoj requested a review from a team as a code owner March 2, 2025 14:16
@jnohclee-rgb

Copy link
Copy Markdown

AI-assisted independent offline check at 85739b008fe7f1c34a8b6708b6037b2d4ff2dcdb, compared with pinned main 919b6236382f3f9f76d8453efb9f9ad03cff1126.

12/12 URL controls pass on this head versus 10/12 on pinned main. Direct-client and module-client controls cover root, /v1 and nested /a/b base URLs, each with/without a trailing slash. The module baseline joins the two path-without-slash cases as /v1models and /a/bmodels; this head prepares /v1/models and /a/b/models while the direct-client and trailing-slash controls remain unchanged.

Scope: Actual local _prepare_url helper on direct and module client objects. Fictional host/key values; in-process module globals restored and clients closed. No requests, Azure module variant or query/fragment-containing base URL policy proof. Python 3.14.7 / Pydantic 2.13.5 and shared local dependencies; this is a bounded matrix, not a full SDK suite or historical dependency matrix. Network denied by the sandbox.

Standalone reproducer (run separately with each source tree on PYTHONPATH)
import json,openai
rows=[]
saved=(openai.api_key,openai.base_url)
try:
 for kind in ['direct','module']:
  for url in ['https://example.invalid','https://example.invalid/','https://example.invalid/v1','https://example.invalid/v1/','https://example.invalid/a/b','https://example.invalid/a/b/']:
   openai.api_key='fictional';openai.base_url=url
   client=(openai.OpenAI if kind=='direct' else openai._ModuleClient)(api_key='fictional',base_url=url)
   actual=str(client._prepare_url('/models'));expected=url.rstrip('/')+'/models'
   rows.append({'kind':kind,'base_url':url,'prepared':actual,'expected':expected,'pass':actual==expected});client.close()
finally:openai.api_key,openai.base_url=saved
print(json.dumps({'cases':rows,'passed':sum(x['pass'] for x in rows),'failed':sum(not x['pass'] for x in rows),'scope':'Actual module/direct client local URL preparation; fictional values, globals restored, no endpoint request/query-component policy proof.'}))

@marcuswood-oai marcuswood-oai added the v4 Planned for Python SDK v4 label Oct 26, 2026 — with ChatGPT Codex Connector
@marcuswood-oai

Copy link
Copy Markdown
Contributor

Thanks for catching the missing slash. I confirmed that the module client can still turn /v1 and /models into /v1models. The instance client already handles the slash while preserving query parameters, so I’ll close this PR and keep #1373 open to reuse that logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sdk v4 Planned for Python SDK v4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent base_url behavior with module client and OpenAIClient

4 participants