Sub topic 13
Troubleshooting Common Failures
This chapter assumes you can run the earlier examples: a local server such as Ollama is installed, at least one model is downloaded, and the openai Python package is installed. Most failures come down to three things: the client cannot reach the server, the URL is wrong, or the model name does not exist. Check them in that order.
A Troubleshooting Order
Work from the network inward. Each step rules out one layer.
Symptom 1: Connection Error
The client raises openai.APIConnectionError. The request never reached a server. Common causes:
- The server is not running.
- The port is wrong. Ollama uses 11434. LM Studio and
llama-serveruse other defaults, and you can change all of them. - The server runs in a container or on another machine, so
localhostpoints to the wrong place.
Test the server without Python:
curl http://localhost:11434/v1/modelsIf curl also fails, the problem is the server or the address, not your code. If curl works but Python fails, compare the base_url in your code with the URL you just used.
Symptom 2: 404 Not Found
A 404 means a server answered, but not at that path or for that model. The client raises openai.NotFoundError. There are two usual causes.
- Missing
/v1. The base URL must behttp://localhost:11434/v1. The client appends/chat/completionsto it. Without/v1the final path is wrong. - Unknown model. Many servers return 404 when the
modelvalue does not match a model they have. Ollama, for example, reports that the model was not found and suggests pulling it.
Ask the server which names it accepts, then copy one exactly:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
for m in client.models.list():
print(m.id)Names include tags. A model listed as llama3.2:latest may also answer to llama3.2, but a model downloaded under a different tag will not. If the list is empty, download a model first, for example with ollama pull llama3.2.
Symptom 3: Timeouts and Slow Replies
The first request after startup is often slow because the server loads the model into memory. A large model on a CPU can also take a long time to produce the first token. The client's default timeout may be too short for this.
- Raise
timeouton the client, for example to 60 seconds or more. - Send a small warm-up request before timing anything.
- Use streaming so you see output as it is produced instead of waiting for the full reply.
- Use a smaller or more heavily quantized model if memory is short.
Retries help with brief failures, but they repeat the whole request. Keep max_retries low for slow models.
Symptom 4: Other HTTP Errors
Catch errors from most specific to least specific. This example separates the three cases above:
import openai
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="local",
timeout=60.0,
max_retries=2,
)
try:
response = client.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)
except openai.APIConnectionError:
print("Cannot reach the server. Is it running, and is the base URL correct?")
except openai.NotFoundError:
print("The server returned 404. Check the model name and the /v1 path.")
except openai.APIStatusError as e:
print(f"Server returned status {e.status_code}")APITimeoutError is a subclass of APIConnectionError, so the first branch also catches timeouts. Print e in that branch if you need to tell them apart.
| Symptom | Likely cause | First check |
|---|---|---|
| Connection error | Server down, wrong host or port | `curl` the `/v1/models` URL |
| 404 | Missing `/v1`, or model not pulled | List models, copy the exact name |
| Timeout | Model still loading, slow hardware | Longer timeout, warm-up request |
| 400 | Unsupported parameter or field | Remove optional fields, retry |
| Cut-off reply | `max_tokens` too small | Check `finish_reason` |
Subtler Problems
- Truncated answers. If
finish_reasonislength, the reply hitmax_tokens. Raise it. - Forgotten history. The server does not remember earlier requests. Send the full message list every time.
- Unsupported features. Some servers ignore or reject options such as tool calling or structured output. Remove the option to confirm it is the cause.
- Placeholder key rejected. Most local servers ignore the key, but a server started with an API key setting will return 401. Use the key it expects.
Exercise
Your script raises openai.NotFoundError, and curl http://localhost:11434/v1/models works. What do you check next, and why?
Show answer
Check the model name and the base URL path. The server is reachable, so the 404 comes from either a model value that is not in the models list (for example a missing tag or a model that was never pulled) or a base URL missing /v1. Compare the name in your code to the IDs the models endpoint returns.
Exercise
The first request takes 40 seconds and fails with a timeout, but the next request succeeds quickly. What is the likely cause, and what are two fixes?
Show answer
The server was loading the model into memory on the first request. Two fixes: raise the client timeout, and send a small warm-up request before the real work.