Sub topic 12
Where Local Servers Differ from the Hosted API
This chapter assumes you can already send chat completions and embeddings to a local server with the OpenAI Python client. It covers where servers such as Ollama, llama-server, LM Studio, and vLLM depart from the hosted OpenAI API, and how to test for those differences before you rely on them.
Why compatibility is partial
"OpenAI-compatible" means a server accepts the same URL paths and JSON shapes for the core endpoints. It does not mean the server implements every endpoint, every parameter, or the same behavior behind them. Each project implements the subset its maintainers care about, and that subset changes between releases.
The client library does not warn you about this. It sends what you ask for. The server may then ignore a field, reject it, or interpret it differently.
Common differences
| Area | Hosted API | Local servers |
|---|---|---|
| Model names | Fixed names such as `gpt-4o-mini` | Whatever you downloaded, such as `llama3.2` or a file path |
| API key | Required and checked | Usually ignored, but the client needs a value |
| Endpoints | Full set | Chat and models are common. Embeddings, completions, and others vary |
| Parameters | All documented fields work | Some are ignored silently, others cause a 400 error |
| Structured output | JSON schema enforcement | Varies. Some servers enforce a grammar, others only suggest JSON |
| Tool calling | Supported on current models | Depends on both the server and the model |
| Limits | Documented rate limits | Set by your hardware. Requests queue or slow down |
| Token counts | Accurate `usage` object | May be missing or approximate |
Model-dependent features
Some features depend on the model as well as the server. A server can accept a tools field, yet a model that was never trained for tool calling will answer in plain text. Embeddings need an embedding model such as nomic-embed-text. Sending a chat model to the embeddings endpoint either fails or returns poor vectors.
Silent ignoring
The most dangerous difference is a parameter the server accepts and then ignores. If you set seed for reproducibility or response_format for JSON and the server drops it, your code runs without errors but behaves differently than you expect.
Test before you rely on it
Write a small probe for each feature you need. Run it against each server and model you plan to use. The following script checks for three features and reports what happened.
import json
import openai
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local", timeout=60.0)
MODEL = "llama3.2"
def probe_usage():
r = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": "Say hi."}],
max_tokens=20,
)
return r.usage is not None and r.usage.total_tokens > 0
def probe_json_mode():
r = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": "Return a JSON object with key ok set to true."}],
response_format={"type": "json_object"},
)
json.loads(r.choices[0].message.content)
return True
def probe_tools():
tools = [{
"type": "function",
"function": {
"name": "get_time",
"description": "Get the current time",
"parameters": {"type": "object", "properties": {}},
},
}]
r = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": "What time is it? Use the tool."}],
tools=tools,
)
return bool(r.choices[0].message.tool_calls)
for name, probe in [("usage", probe_usage), ("json_mode", probe_json_mode), ("tools", probe_tools)]:
try:
print(name, "ok" if probe() else "not supported")
except (openai.APIStatusError, json.JSONDecodeError) as e:
print(name, "failed:", type(e).__name__)Each probe has three outcomes. It can succeed, return a result that shows the feature is missing, or raise an error. Treat the second outcome as seriously as the third, because it is the silent case.
Design for portability
- Keep the base URL, model name, and API key in configuration, not in code.
- Validate model output yourself. Parse JSON and check required fields even when you requested JSON mode.
- Do not depend on
usagefor billing or limits unless a probe confirms it. - Read the server's documentation for a list of supported parameters, and check its release notes when you upgrade.
Exercise
You set response_format={"type": "json_object"} and the request succeeds with no error, but the reply is prose with extra text around the JSON. What has most likely happened, and what should your code do?
Show answer
The server accepted the field but did not enforce it, so the parameter was silently ignored or only treated as a hint. Your code should not assume valid JSON. It should parse the reply inside a try block, validate the fields it needs, and retry or fall back to a prompt that asks for JSON explicitly if parsing fails.
Exercise
Name two reasons a request with a tools field might return a plain text answer instead of a tool call on a local server.
Show answer
The server may accept the field but not implement tool calling. Or the model may not have been trained for tool calling, so it ignores the tool definitions and answers in text.