Skip to content
C.W.K.
Lesson 04 of 05 · published

OpenAI-Compatible Endpoints

~22 min · inference, openai-compat

Level 0Scout
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Why OpenAI compatibility matters

Most clients in the wild — LangChain, LlamaIndex, your custom shim, the OpenAI SDK itself — speak OpenAI's wire format. HF's Inference Providers accept that format at https://router.huggingface.co/v1. Point an existing OpenAI client at that base URL with your HF token, and models served by any HF-routed provider answer code that thinks it's calling OpenAI.

Compatible means chat, and only chat

The endpoint serves one task: chat completion. HF's own docs label it chat only. Send embeddings or image generation to the same base URL and both /v1/embeddings and /v1/images/generations come back 404. Those tasks go through huggingface_hub's InferenceClient instead: feature_extraction for embeddings, text_to_image for images.

Inside chat, the shapes match OpenAI's: messages, tools, response_format, streaming chunks. Authorization: Bearer ${HF_TOKEN} takes the place of OPENAI_API_KEY, and GET /v1/models lists what you can call.

Same shape is not same capability

Model ids are HF repo ids, not OpenAI model names. A suffix on the id picks how the provider is chosen: no suffix means :fastest, :cheapest takes the lowest price, and a provider name such as :groq pins one.

The trap is that providers differ in what they actually support. /v1/models reports supports_tools and supports_structured_output per provider. In October 2026, four providers served meta-llama/Llama-3.1-8B-Instruct, and only one of them was marked as supporting tool calls. An endpoint that accepts your request JSON makes no promise that the feature behind it works. Rate limits and billing follow HF and the provider you landed on, not OpenAI.

Code

Use the OpenAI SDK against HF·python
from openai import OpenAI
import os

client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=os.environ["HF_TOKEN"],
)

resp = client.chat.completions.create(
    model="meta-llama/Llama-3.1-8B-Instruct",  # HF repo id
    messages=[{"role": "user", "content": "Hi from the OpenAI SDK pointed at HF."}],
    max_tokens=60,
)
print(resp.choices[0].message.content)
Streaming through the OpenAI SDK·python
from openai import OpenAI
import os

client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=os.environ["HF_TOKEN"])

stream = client.chat.completions.create(
    model="meta-llama/Llama-3.1-8B-Instruct",
    messages=[{"role": "user", "content": "List three open-source LLMs."}],
    max_tokens=200,
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="", flush=True)

External links

Exercise

Take an existing piece of code that calls OpenAI (or write one). Switch it to call HF's router with a Llama-3 instruct model. Three lines change: base_url, api_key, and model. If the address and key come from environment variables (OPENAI_BASE_URL, OPENAI_API_KEY), only model changes in the code. Run both versions on the same prompt and compare outputs. Then call client.embeddings.create through the same client, watch the 404, and get the same embedding with InferenceClient.feature_extraction.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.

No comments yet — be the first.