Skip to content

Inconsistent base_url behavior with module client and OpenAIClient #1373

Open

Description

@dramdass

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

The issue is that the base_url in the OpenAI client enforces a trailing slash whereas setting the module openai.base_url does not enforce.

This is 100% reproducible and while it can be worked around, the inconsistency in clients causes issues in scripts that use both clients.

To Reproduce

  1. Create a module client and use a base_url without a trailing slash. See error with URL for completions
  2. Create an OpenAIClient and use a base_url without a trailing slash. See no erros.

Code snippets

This works

    client = openai.OpenAI(
        api_key=my_key,
        base_url="https://myllmserver.com/api/v1"
    )
    print(client.completions.create(
       model="mistralai/Mistral-7B-Instruct-v0.2",
       prompt="test" 
    ))

This does not:

    openai.base_url="https://myllmserver.com/api/v1"
    openai.api_key=my_key
    print(openai.completions.create(
       model="mistralai/Mistral-7B-Instruct-v0.2",
       prompt="test"
    ))

Error:

https://myllmserver.com/api/v1completions "HTTP/1.1 404 Not Found"

OS

macOS

Python version

Python v3.12

Library version

openai v1.23.6

Activity

  1. added
    bug
    on Apr 26, 2024
  2. rattrayalex commented on May 13, 2024

    @rattrayalex
    Contributor

    Thanks for reporting, @dramdass. I'm not sure we'd prioritize a fix here, but we'd likely accept a contribution that updates the module client to match the non-module client.

  3. dramdass commented on May 14, 2024

    @dramdass
    Author

    The right fix seems to be in src/__init__.py but the comment says # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.. I could't find a relevant place in the schema here https://github.com/openai/openai-openapi/tree/master to generate it. Can I add the fix manually into src/__init__.py?

  4. rattrayalex commented on May 25, 2024

    @rattrayalex
    Contributor

    Yes, that would be the correct location.

  5. added a commit that references this issue on Mar 2, 2025
    0e89bca
  6. mrutunjay-kinagi commented on Sep 30, 2026

    @mrutunjay-kinagi

    This still reproduces on current main (3.22.1):

    import openai, httpx2 as httpx
    
    seen = []
    http = openai.DefaultHttpxClient(transport=httpx.MockTransport(
        lambda r: (seen.append(str(r.url)), httpx.Response(200, json={"object": "list", "data": []}))[1]))
    
    openai.OpenAI(api_key="x", base_url="https://example.com/api/v1", http_client=http).models.list()
    print(seen[-1])  # https://example.com/api/v1/models
    
    openai.api_key, openai.base_url, openai.http_client = "x", "https://example.com/api/v1", http
    openai.models.list()
    print(seen[-1])  # https://example.com/api/v1models

    Root cause. _ModuleClient.base_url in src/openai/__init__.py overrides the getter and returns the module-level global after only _normalize_httpx_url():

    @property
    @override
    def base_url(self) -> _httpx.URL:
        if base_url is not None:
            return _normalize_httpx_url(base_url)
        return super().base_url

    BaseClient applies _enforce_trailing_slash() in both __init__ and the base_url setter, but this override bypasses it whenever openai.base_url is set.

    Suggested fix (one line):

    -            return _normalize_httpx_url(base_url)
    +            return self._enforce_trailing_slash(_normalize_httpx_url(base_url))

    With that change the module client produces https://example.com/api/v1/models, the same as OpenAI(...). The existing tests/test_module_client.py suite passes (32 tests). The Azure and Bedrock module clients inherit this getter, so they pick up the fix too. A regression test could assert openai.completions._client.base_url == URL("http://foo.com/api/v1/") after setting openai.base_url = "http://foo.com/api/v1". The existing test_base_url_option uses a URL with no path, which is why it doesn't catch this.

    cc @marcuswood-oai

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsdk

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions