Sub topic 4
Checking which models the server offers
This chapter assumes you can run Python and install packages with pip. You also need the openai package installed and a local server such as Ollama running with at least one model downloaded. Earlier chapters showed how to point the client at the server with a base URL and a placeholder API key. Here you ask the server which models it has, then pick one by name.
Why list models first
Every chat request needs a model field, and the server must recognize that exact string. A wrong name is the most common cause of a failed first request. The OpenAI-compatible API defines a GET /v1/models endpoint for this. It returns the model IDs the server will accept. Checking it first costs one request and removes guesswork.
Listing models with the client
The client wraps the endpoint as client.models.list(). This is the same call from the topic's setup example.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
for m in client.models.list():
print(m.id)With Ollama, the output might look like this:
llama3.2:latest
nomic-embed-text:latestEach line is a model ID. Copy it exactly, including any tag after the colon.
The raw HTTP version
The client is a thin wrapper over HTTP. You can make the same request with curl to see what the server actually returns.
curl http://localhost:11434/v1/modelsThe response is JSON with an object field set to "list" and a data array. Each entry has an id, an object value of "model", and often created and owned_by fields. The client turns each entry into an object, which is why you read m.id.
Selecting a model by name
Store the IDs in a list, then check that the model you want is present before you use it.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
available = [m.id for m in client.models.list()]
wanted = "llama3.2"
matches = [name for name in available if name.split(":")[0] == wanted]
if not matches:
raise SystemExit(f"{wanted} not found. Available: {available}")
model_id = matches[0]
print("Using", model_id)The comparison drops the tag, so llama3.2 matches llama3.2:latest. If several tags exist, such as llama3.2:1b and llama3.2:3b, the code takes the first match. Use the full ID when you need a specific one.
What the list means on different servers
Servers differ in what this endpoint reports.
| Server | What `/v1/models` typically shows |
|---|---|
| Ollama | Models you have downloaded, with tags |
| LM Studio | Models available to load, and sometimes only the loaded one |
| llama.cpp `llama-server` | The single model it was started with |
| vLLM | The model or models it was launched to serve |
Check your server's documentation for the exact behavior. Some servers also accept requests whose model name does not match the list, and answer with whatever model is loaded. A successful reply therefore does not prove you reached the model you intended. Print the model field of the response when it matters.
Common errors
- Connection error. The server is not running, or the base URL is wrong. A server that is down fails here, before any chat request.
- 404 Not Found. The base URL probably lacks the
/v1path, so the client requests the wrong route. - Empty list. The server is running but no model is downloaded or loaded. With Ollama, pull one first, for example
ollama pull llama3.2. - Embedding models in the list. Models like
nomic-embed-textappear next to chat models. Sending one a chat request will fail. The list does not label which models are for chat.
Exercise
Your script prints llama3.2:latest from the models list, but a chat request with model="llama3.2:3b" returns a 404 error. What is the most likely cause, and how do you fix it?
Show answer
The server does not have a model with the ID llama3.2:3b. Only llama3.2:latest was downloaded. Either use the exact listed ID, llama3.2:latest, or download the 3b tag (for Ollama, ollama pull llama3.2:3b) and list the models again.