Skip to main content

What "OpenAI-Compatible" Leaves Out

Updated 5 min readMarkdown

Every vendor in our provider list speaks the same endpoint: POST {base_url}/chat/completions, a Bearer token, a JSON body with model and messages. The request shape is identical. What comes back is not. The adapter — one class, OpenAICompatibleProvider, sitting between the caller and the vendors it serves — exists because "compatible" describes the wire format and nothing else. Its job is to take in whatever each vendor actually does and hand the rest of the system something it can rely on.

Seven requests killed the retry

For a while the provider had a forgiving habit: if a request came back 400 or 422, it would drop the parameter that looked responsible and try again. The premise sounded reasonable — some endpoints reject keys they don't recognize, so shed the offending key and the call goes through. Seven real requests against the DeepSeek endpoint proved the premise wrong, and the retry was deleted in September 2026.

A 400 turned out to mean a semantic error: a model name that doesn't exist. A 422 meant a bad field or enum value: an illegal thinking.type, a malformed tools entry, a wrong role. Both errors name the cause precisely in error.message, down to the field name and the legal values. And an unrecognized parameter name — the one case the retry was built for — never triggers either status. Misspell thinking as thinkin and the endpoint answers HTTP 200, silently ignoring the key. So the retry only ever fired on requests that were genuinely wrong, and each retry converted a precise, diagnosable error into a silent 200. Our own body was wrong, and the system reported success. The fix was to throw failures as-is. No parameter is ever dropped and retried; if the endpoint rejects the call, the caller sees exactly what the endpoint said.

The stream that arrived garbled

Streaming exposed a different kind of difference: the same bytes, decoded by the wrong rule. The provider used to iterate the SSE stream with iter_lines(decode_unicode=True), which decodes each line according to resp.encoding. Requests guesses that encoding from the Content-Type header, and when the header carries no charset — which is the common case for text/event-stream, since the SSE spec says the stream is always UTF-8 and the charset parameter is ignored — it falls back to ISO-8859-1. One vendor wrote charset=utf-8 and everything worked. Another didn't, and every Chinese character in the stream came back garbled.

The garbling didn't stay cosmetic. The model emits search terms as part of its reply, and those terms came back corrupted too, so the downstream search answered a question nobody asked. The fix, made in October 2026, stops decoding the stream as text at all: raw bytes go straight to json.loads, and the JSON spec decides the encoding. The HTTP header's historical default no longer gets a vote, and no vendor has to be patched for it.

Counting tokens the vendor won't count for you

The usage block looks standard — prompt_tokens, completion_tokens, total_tokens — but the numbers inside it don't mean the same thing everywhere. One vendor tucks its reasoning tokens inside completion_tokens_details.reasoning_tokens; if you read completion_tokens at face value, a long chain of thought looks like a long answer, and the TOKEN_RATIO check that guards against runaway output starts tripping on thinking. The adapter pulls reasoning_tokens out and keeps it separate, so the ratio check measures what was actually written.

Cache accounting gets the same treatment. Some vendors report prompt_cache_hit_tokens and prompt_cache_miss_tokens separately, because a cached prompt is billed cheaper — one vendor charges a tenth for cache hits. Multi-round tool loops are almost pure prefix with a little appended at the end, which is exactly the shape caches love, so the difference between hit and miss is the difference between "the cache is working" and "we're paying full price every round." Vendors that don't report the split default to zero. The normalized usage object always has the same six fields, and the caller never has to ask which vendor it's talking to.

The base class assumes yes

The base class, LLMProvider, defaults supports() to True. A provider that never implemented capability probing behaves exactly as it did before the capability mechanism existed: every optional key is sent, optimistically. The adapter overrides that default only where a probe has actually concluded otherwise — a probed "no" means the key is never sent. Nothing else changes. Adding the whole probing apparatus left unprobed providers untouched, which is the point: the adapter absorbs differences, it doesn't manufacture them.

There is a related habit worth naming. When something goes wrong, the error text is read from the raw response bytes decoded as UTF-8, not from str(HTTPError) — which carries only the status code — and not from resp.text, which mis-decodes when no charset is present, the same trap as the SSE stream. The error message is the one place where the vendor tells you precisely what's wrong, and the adapter's job is to pass that through intact, not to summarize it into a status code.

That is the whole job, really. The endpoint shape is a promise the vendors keep. Everything else — which errors mean what, which encoding the bytes are in, which tokens are counted where, which keys an endpoint quietly ignores — is left out of the promise, and the adapter is where those differences get absorbed, one at a time, as they are found. The rest of the system never sees them.

About the author

  • Writing about ebook technology.

No comments yet. Be the first to comment.