Skip to content

Add ability to customize "/chat/completions" path after base url #1344

Closed as not planned

Description

@LukeSamkharadze

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • This is a feature request for the Python library

Describe the feature or improvement you're requesting

Some providers have custom endpoint for calling models.

For ex https://api.minimax.chat/v1/text/chatcompletion_v2

If we used python library with base url of https://api.minimax.chat/v1 final request is being made on https://api.minimax.chat/v1/chat/completions which doesn't exist.

Instead of hardcoding path /chat/completions param So lets add option for specifying custom path.

Example:

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="custom_model",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  custom_path="text/chatcompletion_v2"
)

print(completion.choices[0].message)

Additional context

No response

Activity

  1. ChenjiaxingXJTU commented on Mar 25, 2025

    @ChenjiaxingXJTU

    you can replace path using httpx.Client, FYI: #547

  2. RobertCraigie commented on Mar 25, 2025

    @RobertCraigie
    Contributor

    Thanks for the feature request! I think that replacing the path at the httpx layer is a better approach for now as if we added this as a method argument you'd have to remember to pass it everytime you call client.chat.completions.create().

    Here's an example for updating the path:

    import httpx
    from openai import OpenAI, DefaultHttpxClient
    
    def update_base_url(request: httpx.Request) -> None:
        # rewrite the path segment to what the proxy expects
        if request.url.path == "/v1/chat/completions":
            request.url = request.url.copy_with(path="/my/custom/path")
    
    client = OpenAI(
        base_url="https://www.my.base.url",
        http_client=DefaultHttpxClient(
            event_hooks={
                "request": [update_base_url],
            }
        ),
    )
    completion = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {
                "role": "user",
                "content": "Say hi!",
            }
        ],
    )
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions