Sub topic 3
Pointing the client at your server
This chapter assumes you can run Python with pip, read JSON, and already have a local model server (for example Ollama) running with at least one model downloaded. The examples use Ollama on its default port with the llama3.2 model. Adjust the URL and model name for your own server.
Why the client needs two settings
The OpenAI Python client is built to talk to OpenAI's hosted service. It decides where to send requests from a base URL, and it decides how to authenticate from an API key. To use a local server, you change the base URL. You also supply a key, even though the server ignores it.
The base URL is the prefix that every request path is added to. With the client's default, a chat request goes to https://api.openai.com/v1/chat/completions. If you set the base URL to http://localhost:11434/v1, the same call goes to http://localhost:11434/v1/chat/completions.
Install the client
Install the official package. It is the same one used for the hosted service.
pip install openaiCreate the client
Pass base_url and api_key when you construct the client.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
for m in client.models.list():
print(m.id)The loop lists the model IDs the server reports. It is a quick way to confirm that the connection works. If it prints your model names, the base URL is right and the server is reachable.
The API key is a placeholder
Most local servers do not check the key. The client, however, refuses to start without one. If you omit api_key and no OPENAI_API_KEY environment variable is set, constructing the client raises an error. Any non-empty string works, so "local" is a common choice.
Use environment variables instead
You can leave the constructor empty and configure the client from the environment. The client reads OPENAI_BASE_URL and OPENAI_API_KEY.
export OPENAI_BASE_URL="http://localhost:11434/v1"
export OPENAI_API_KEY="local"from openai import OpenAI
client = OpenAI()
print([m.id for m in client.models.list()])This keeps server details out of your code. You can then switch between a local server and a hosted one without editing the script. Arguments passed to the constructor take priority over environment variables.
Common base URL values
Each server picks its own port. The path suffix is usually /v1.
| Server | Typical base URL |
|---|---|
| Ollama | `http://localhost:11434/v1` |
| llama.cpp `llama-server` | `http://localhost:8080/v1` |
| LM Studio | `http://localhost:1234/v1` |
| vLLM | `http://localhost:8000/v1` |
These are defaults. Check your server's startup output or settings, because ports can be changed.
Mistakes to avoid
- Leaving out
/v1. The client appends paths such as/chat/completionsto your base URL. Without/v1, many servers answer 404. - Adding too much. Do not put
/chat/completionsin the base URL. The client adds it. - Using
httpsfor a plain local server. Local servers usually speakhttp. AnhttpsURL fails with a connection or TLS error. - Using
localhostinside a container. In Docker,localhostmeans the container itself. Use the host's address, such ashost.docker.internalwhere it is available. - Forgetting the key. An empty key causes an error before any request is sent.
Check the connection without Python
If the client fails, test the server directly. A request to the models path shows whether the server is up and the URL is correct.
curl http://localhost:11434/v1/modelsA JSON list means the server is working, so the problem is in your client settings. A "connection refused" message means the server is not running or the port is wrong.
Exercise
Your script sets base_url to "http://localhost:11434" and calls client.models.list(). The server is running, but the call fails with a 404 error. What is the likely cause, and how do you fix it?
Show answer
The base URL is missing the /v1 path, so the client requests /models instead of /v1/models. Change the base URL to "http://localhost:11434/v1".
Exercise
Why must you pass an api_key to the OpenAI client even when the local server ignores it?
Show answer
The client requires a value and raises an error if none is set, either as an argument or in OPENAIAPIKEY. A placeholder string such as "local" satisfies it.