You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ability to customize "/chat/completions" path after base url #1344
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:
fromopenaiimportOpenAIclient=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
Reacted by Luka Samkharadze, GauravRanganath, Cheikh Fiteni, Shriyash Upadhyay, GZack2000, Bastian Burger, vishaldas-datafacade, Junchao Zhang, Hadrien, Pankaj Gupta and 2 more
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:
importhttpxfromopenaiimportOpenAI, DefaultHttpxClientdefupdate_base_url(request: httpx.Request) ->None:
# rewrite the path segment to what the proxy expectsifrequest.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!",
}
],
)
Activity
ChenjiaxingXJTU commentedon Mar 25, 2025
you can replace path using httpx.Client, FYI: #547
RobertCraigie commentedon Mar 25, 2025
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: