Sub topic 2
What OpenAI-compatible Means
This chapter assumes you know basic Python, HTTP requests and JSON. It also assumes you know roughly what a language model and a prompt are, and that a local model server such as Ollama is installed with at least one model downloaded.
A contract about the interface
"OpenAI-compatible" describes how you talk to a server. It says nothing about the model behind it. A local server is compatible when it accepts the same HTTP requests as the hosted OpenAI API and replies with the same JSON shapes. Ollama, llama.cpp's llama-server, LM Studio and vLLM all do this. Each runs a different model engine, but each exposes the same kind of HTTP interface.
The practical result is that code written for the hosted API can talk to your local model. You change where the requests go, and the rest of the code stays the same.
What the contract contains
The compatible surface is small. These are the parts you will use most:
| Part | Shape |
|---|---|
| List models | `GET /v1/models` |
| Chat | `POST /v1/chat/completions` |
| Embeddings | `POST /v1/embeddings` |
| Authentication | `Authorization: Bearer <key>` header |
| Chat request body | `model`, `messages`, and optional fields such as `temperature`, `max_tokens` and `stream` |
| Chat response | `choices[0].message.content` holds the reply, and `usage` reports token counts |
| Streaming | Server-sent events: lines starting with `data:` that carry JSON chunks, ending with `data: [DONE]` |
| Errors | An HTTP status code plus a JSON body with an `error` object |
Later chapters cover each of these in detail. For now, notice that everything is plain HTTP and JSON.
Seeing the contract directly
You do not need a client library to use the contract. This request goes to Ollama on its default port, using the llama3.2 model:
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "llama3.2", "messages": [{"role": "user", "content": "Say hello in one sentence."}]}'The path /v1/chat/completions and the body shape are the same ones the hosted API uses. If you replaced the host with api.openai.com and added a real key and a hosted model name, the request would still be valid. The server answers with JSON, and the reply text sits at choices[0].message.content.
Why the standard client works
The OpenAI Python client does not contain any logic specific to OpenAI's servers. It builds a URL from a base address, adds headers, sends JSON and parses the reply. Because the base address is a setting, you can point the client at localhost. The next chapter shows how.
What compatibility does not promise
Compatible means the shapes match. It does not mean the behavior matches. Expect these differences:
- Output quality. A small local model will answer differently from a large hosted one.
- Feature coverage. A server may implement chat but not embeddings, or accept a tool-calling field and handle it poorly.
- Model names. Each server uses its own IDs, such as
llama3.2. Hosted names likegpt-4owill not exist locally. - Parameter handling. A server may ignore a field it does not support instead of returning an error, so a setting can silently have no effect.
- Authentication. Most local servers do not check the key, but the client still needs a value.
Treat compatibility as a strong starting point. It removes most of the integration work, and you still have to check the details that differ between servers.
Exercise
Your code works against a local server, then you switch the base URL to a different compatible server and temperature seems to have no effect. Does this break the OpenAI-compatible contract? Why or why not?
Show answer
Not necessarily. The contract fixes the request and response shapes, not how every parameter is handled. A server may accept a field and ignore it. You should test parameters on each server instead of assuming they behave the same.