Sub topic 4
Checking which models the server offers
This chapter assumes you have the OpenAI Python client installed (pip install openai), a local server running at a known base URL, and at least one model downloaded. It also assumes you know how to set a base URL and a placeholder API key on the client, which the previous chapter covered. Here you will ask the server what it has, read the answer, and use it to pick a model name that works.
Why list models first
Every chat request carries a model field. The server uses that string to decide which weights to run. If the string does not match something the server knows, the request fails, usually with a 404 or a 400 error.
Hosted APIs publish a fixed model catalog, so you can look names up in documentation. A local server is different. The set of models depends on what you downloaded, how you named it, and which server you run. Two machines running the same server can offer different names.
Listing models first gives you three things:
- A check that the server is reachable and that your base URL is right.
- The exact model IDs the server accepts.
- A cheap test, because the call runs no inference.
Because this call is so cheap, it is a good first step in any script that talks to a local server.
The models endpoint
OpenAI-compatible servers expose GET /v1/models. It returns a JSON object whose data field is a list of model entries. A typical response looks like this:
{
"object": "list",
"data": [
{
"id": "llama3.2:latest",
"object": "model",
"created": 1731000000,
"owned_by": "library"
},
{
"id": "nomic-embed-text:latest",
"object": "model",
"created": 1730000000,
"owned_by": "library"
}
]
}The field you need is id. Treat it as an opaque string and copy it exactly as shown. Other fields vary by server.
You can call the endpoint without any client library. This is useful when you want to separate "the server is broken" from "my Python code is broken":
curl http://localhost:11434/v1/modelsMost local servers do not check the API key on this call. If yours does, add an Authorization: Bearer <key> header.
Listing models with the Python client
The client wraps the endpoint in client.models.list(). It returns an iterable page of model objects, and each object has an id attribute.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
for m in client.models.list():
print(m.id)If you run this against Ollama with llama3.2 and nomic-embed-text downloaded, the output looks like this:
llama3.2:latest
nomic-embed-text:latestYour output will differ. What matters is that each printed line is a valid value for the model parameter.
Collecting IDs into a list
Often you want the IDs as data, not as printed text, so that your code can check or choose one. A list comprehension does this:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
available = [m.id for m in client.models.list()]
print(available)Choosing a model by name
A common pattern is to ask for a preferred model and fall back with a clear error if it is missing. The following script does that. It fails early with a message that names the available models, which is far easier to act on than a 404 from a later chat call.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
wanted = "llama3.2"
available = [m.id for m in client.models.list()]
if wanted in available:
chosen = wanted
else:
matches = [m for m in available if m.startswith(wanted)]
if not matches:
raise SystemExit(f"Model {wanted!r} not found. Available: {available}")
chosen = matches[0]
print("Using model:", chosen)The prefix match handles a common case: you ask for llama3.2, but the server reports llama3.2:latest. If several tags share the prefix, the script takes the first one. For anything beyond a quick script, prefer an exact match, because llama3.2:1b and llama3.2:3b are different models with different speed and quality.
How the request flows
The list call is a single round trip. The client sends a GET, the server reads its local registry or directory of models, and it returns the IDs.
What the list means on different servers
The endpoint is standardized, but what it lists is not. The meaning of "offered" depends on how the server manages models.
| Server | What `/v1/models` typically shows | Notes |
|---|---|---|
| Ollama | Models you have downloaded, with tags such as `:latest` | The tag is part of the ID. |
| llama.cpp `llama-server` | Usually the single model loaded at startup | The ID is often the file path or alias; many setups ignore the `model` field in requests. |
| LM Studio | Models available in its library, and which are loaded can vary | A model may need to be loaded before it can answer. |
| vLLM | The model or models the server was launched with | The ID is the name passed at launch, such as a Hugging Face repository name. |
These behaviors can change between versions, so treat the table as a guide and check your own server's output. The important rule is the same everywhere: use what the server reports, not what you remember from another setup.
Listed does not always mean loaded
Some servers list models that exist on disk but are not in memory. The first chat request for such a model may take much longer, because the server must load the weights. With a short client timeout, that first call can fail even though the model name is correct. If this happens, raise the timeout for the first request or send a small warm-up request.
Chat models and embedding models share one list
The endpoint does not tell you what a model is for. In the example above, nomic-embed-text appears next to a chat model. If you send it a chat request, you will get an error or poor output. Many servers add extra fields with details about each model, but these are not part of the shared contract, so do not depend on them. A naming convention such as embed in the ID is a practical hint, not a guarantee.
Retrieving a single model
The API also defines GET /v1/models/{id}. The client exposes it as client.models.retrieve():
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
info = client.models.retrieve("llama3.2")
print(info.id, info.owned_by)This is handy for a quick existence check, but support is less uniform than the list call. If the server does not implement it, you get a 404 even for a valid model. For that reason the list call is the safer way to check, and it is what the rest of this tutorial uses.
Common problems
- The list is empty. The server is running but has no models. Download one using your server's own tool, then list again.
- Connection error. The server is not running, the port is wrong, or the base URL is wrong. Try the
curlcommand above to confirm. - 404 on the list call. The base URL probably lacks
/v1. Withbase_url="http://localhost:11434"the client requests/models, not/v1/models. - A name works in the server's own tool but not in the API. Copy the ID from
/v1/models. The server's command line may accept a short name that the API does not. - Your model is not in the list. You may be talking to a different server instance than you think, for example one on another port. Check the base URL.
Summary of the working model
GET /v1/modelsreturns the IDs the server accepts in themodelfield.client.models.list()gives you those IDs in Python, andm.idis the string to use.- IDs are server-specific, may include tags, and may mix chat and embedding models.
- A listed model may still be slow on first use if it must be loaded.
- Checking the list first turns later, vaguer failures into one clear message.
Exercise
You call client.models.list() and the output shows llama3.2:3b and nomic-embed-text:latest. A script then sends a chat request with model="llama3.2" and the server returns a 404. What is the most likely cause, and how would you change the script so the error is clearer?
Show answer
The server does not map llama3.2 to llama3.2:3b, so the name does not match any ID it knows. Use the exact ID from the list, llama3.2:3b. To make the failure clearer, call client.models.list() first, build a list of IDs, and raise an error that names the available models if the wanted one is missing, as in the "Choosing a model by name" script.
Exercise
Your list call fails with a 404, but curl http://localhost:11434/v1/models returns a valid list. Your client is created with base_url="http://localhost:11434". What should you change?
Show answer
Add the /v1 path: base_url="http://localhost:11434/v1". The client appends /models to the base URL, so without /v1 it requests /models, which the server does not serve.