OpenAI SDK against self-hosted endpoints
Application code that talks to your own model server through the official openai package (Python openai, Node openai), with the right base URL, key handling, model names and API choice, and a probe that proves the endpoint before the app is blamed.
Scope: the OpenAI SDKs as clients of OpenAI-compatible servers. Anthropic Messages clients belong to anthropic-sdk; the gateway belongs to litellm or llama-swap; engines to vllm, llama-cpp, ollama; parser flags to tool-calling-parsers; choosing among clients to llm-clients.
Discover first
Read-only, never prints secret values.
python3 -c 'import openai; print(openai.__version__)' 2>/dev/null; npm ls openai 2>/dev/null | grep openai@
env | grep -E '^OPENAI_(BASE_URL|API_KEY|CUSTOM_HEADERS)=' | sed -E 's/=.*/=<set>/' # which vars exist, no values
env | grep -E '^OPENAI_BASE_URL=' ; env | grep -E '^(HTTPS?_PROXY|NO_PROXY)=' | sed -E 's#//[^@/]*@#//<redacted>@#'
BASE=http://localhost:4000 MODEL=<id> LLM_API_KEY_FILE=~/.config/llm/key scripts/check-endpoint.sh # models, chat, tools, stream
Ask only what discovery cannot show: server and URL, whether it needs a key, whether it serves Chat Completions only or also Responses, whether the app needs tools or streaming.
Decision flow
- Endpoint not proven yet: run
scripts/check-endpoint.sh. Fix the server before touching app code. -> references/troubleshooting.md
- Base URL and key:
base_url ends in /v1; the key is any non-empty string if the server has no auth. -> references/client-config.md
- Which API:
chat.completions works on every engine; responses only where the server implements /v1/responses. -> references/apis-tools-streaming.md
- Model name: use the id returned by
GET /v1/models (--served-model-name, gateway alias, Ollama tag). -> references/server-recipes.md
- Tools or structured output fail: server flags,
tool_calls parsing, strict and stream caveats. -> references/apis-tools-streaming.md
- Error: match the status or message. ->
references/troubleshooting.md
Rules
- Never hardcode or print keys. Read from
OPENAI_API_KEY or a 0600 file; do not log request headers, client.api_key or the full environment.
base_url includes /v1 and never /chat/completions. A missing /v1 gives 404 on every call.
OPENAI_BASE_URL is picked up by every OpenAI-SDK program in the shell, including tools you did not mean to redirect. Prefer constructor arguments in application code and an env var only for a deliberate switch.
- The SDK raises if no key is available (Python:
Missing credentials; Node: OPENAI_API_KEY error). For keyless servers pass a dummy string such as "none".
- Probe before blaming code. One
curl through the same URL, key and model id isolates server from client.
- Defaults are generous. Python and Node default to 600 s timeout (Python connect 5 s) and 2 retries; set them explicitly for long generations and for fail-fast health checks.
- Check versions upstream. Flags and defaults come from the SDK release notes and the server's docs, not memory.
- Other skills: install with
npx skills add ScaleDockHQ/llm-skills --skill <name>.
Workflows
Point an app at a self-hosted server
Add tool calling
Debug a failing call
Reference index
| File |
Read when |
references/client-config.md |
Constructors, env vars, keys, headers, timeouts, retries, proxies, TLS |
references/apis-tools-streaming.md |
Chat Completions vs Responses, streaming, usage, tools, structured output, reasoning fields, extra_body |
references/server-recipes.md |
vLLM, llama.cpp, Ollama, LiteLLM, llama-swap: URLs, auth, model names, flags |
references/troubleshooting.md |
Symptom to cause to fix |
Assets: assets/client.mjs (Node smoke test). Script: scripts/check-endpoint.sh (read-only probe).
Companion skills
anthropic-sdk: the same job for Anthropic Messages clients.
litellm, llama-swap: gateways that give stable model names and keys.
vllm, llama-cpp, ollama: engines behind the endpoint.
tool-calling-parsers: when tool_calls come back empty.
llm-clients: choosing and troubleshooting clients of every kind.
1---2name: openai-sdk3description: OpenAI Python and Node SDKs pointed at self-hosted endpoints. Use to set base_url/baseURL and OPENAI_API_KEY for vLLM, llama.cpp, Ollama, LiteLLM or llama-swap, map model names, pick Chat Completions or Responses, stream, call tools, set timeouts, or fix 404, 401, tool_calls missing, Missing credentials, connection errors. Not for Anthropic SDK (anthropic-sdk) or AI SDK.4license: MIT5compatibility: Commands assume bash, curl, jq, and Python 3.9+ or Node.js 20+ with the openai package. Installing packages and changing app config need the user's approval.6metadata:7 source: https://skillmd.com/skills/scaledockhq/openai-sdk8 author: ScaleDockHQ9 version: "0.1.0"10---1112# OpenAI SDK against self-hosted endpoints1314Application code that talks to your own model server through the official `openai` package (Python `openai`, Node `openai`), with the right base URL, key handling, model names and API choice, and a probe that proves the endpoint before the app is blamed.1516**Scope:** the OpenAI SDKs as clients of OpenAI-compatible servers. Anthropic Messages clients belong to `anthropic-sdk`; the gateway belongs to `litellm` or `llama-swap`; engines to `vllm`, `llama-cpp`, `ollama`; parser flags to `tool-calling-parsers`; choosing among clients to `llm-clients`.1718## Discover first1920Read-only, never prints secret values.2122```bash23python3 -c 'import openai; print(openai.__version__)' 2>/dev/null; npm ls openai 2>/dev/null | grep openai@24env | grep -E '^OPENAI_(BASE_URL|API_KEY|CUSTOM_HEADERS)=' | sed -E 's/=.*/=<set>/' # which vars exist, no values25env | grep -E '^OPENAI_BASE_URL=' ; env | grep -E '^(HTTPS?_PROXY|NO_PROXY)=' | sed -E 's#//[^@/]*@#//<redacted>@#'26BASE=http://localhost:4000 MODEL=<id> LLM_API_KEY_FILE=~/.config/llm/key scripts/check-endpoint.sh # models, chat, tools, stream27```2829Ask only what discovery cannot show: server and URL, whether it needs a key, whether it serves Chat Completions only or also Responses, whether the app needs tools or streaming.3031## Decision flow32331. **Endpoint not proven yet:** run `scripts/check-endpoint.sh`. Fix the server before touching app code. -> `references/troubleshooting.md`342. **Base URL and key:** `base_url` ends in `/v1`; the key is any non-empty string if the server has no auth. -> `references/client-config.md`353. **Which API:** `chat.completions` works on every engine; `responses` only where the server implements `/v1/responses`. -> `references/apis-tools-streaming.md`364. **Model name:** use the id returned by `GET /v1/models` (`--served-model-name`, gateway alias, Ollama tag). -> `references/server-recipes.md`375. **Tools or structured output fail:** server flags, `tool_calls` parsing, `strict` and `stream` caveats. -> `references/apis-tools-streaming.md`386. **Error:** match the status or message. -> `references/troubleshooting.md`3940## Rules4142- **Never hardcode or print keys.** Read from `OPENAI_API_KEY` or a `0600` file; do not log request headers, `client.api_key` or the full environment.43- **`base_url` includes `/v1`** and never `/chat/completions`. A missing `/v1` gives 404 on every call.44- **`OPENAI_BASE_URL` is picked up by every OpenAI-SDK program** in the shell, including tools you did not mean to redirect. Prefer constructor arguments in application code and an env var only for a deliberate switch.45- **The SDK raises if no key is available** (Python: `Missing credentials`; Node: `OPENAI_API_KEY` error). For keyless servers pass a dummy string such as `"none"`.46- **Probe before blaming code.** One `curl` through the same URL, key and model id isolates server from client.47- **Defaults are generous.** Python and Node default to 600 s timeout (Python connect 5 s) and 2 retries; set them explicitly for long generations and for fail-fast health checks.48- **Check versions upstream.** Flags and defaults come from the SDK release notes and the server's docs, not memory.49- **Other skills:** install with `npx skills add ScaleDockHQ/llm-skills --skill <name>`.5051## Workflows5253### Point an app at a self-hosted server5455- [ ] Probe: `BASE=... MODEL=... scripts/check-endpoint.sh` passes models, chat, and (if needed) tool and stream lines.56- [ ] Set `base_url` (with `/v1`) and key via env or constructor -> `references/client-config.md`.57- [ ] Use the model id from `/v1/models`.58- [ ] Choose `chat.completions` unless the probe shows `/v1/responses` works.59- [ ] Pass vendor options through `extra_body` and non-standard auth through `default_headers`.60- [ ] Run one real request and one streamed request from the app.6162### Add tool calling6364- [ ] Server started with tool parsing on (vLLM `--enable-auto-tool-choice --tool-call-parser <name>`, llama.cpp `--jinja`) -> `references/server-recipes.md`.65- [ ] Probe prints a `tool_call:` line.66- [ ] Execute the returned call, send a `tool` role message with the matching `tool_call_id`, and request again.6768### Debug a failing call6970- [ ] Re-run the same call with `curl -v` or `check-endpoint.sh`; read the server log line.71- [ ] If curl works, diff URL, key header, model id and body against the SDK request (`OPENAI_LOG=debug` in both SDKs; redact the Authorization line before sharing).7273## Reference index7475| File | Read when |76| ------------------------------------ | --------------------------------------------------------------------------------------------------------- |77| `references/client-config.md` | Constructors, env vars, keys, headers, timeouts, retries, proxies, TLS |78| `references/apis-tools-streaming.md` | Chat Completions vs Responses, streaming, usage, tools, structured output, reasoning fields, `extra_body` |79| `references/server-recipes.md` | vLLM, llama.cpp, Ollama, LiteLLM, llama-swap: URLs, auth, model names, flags |80| `references/troubleshooting.md` | Symptom to cause to fix |8182Assets: `assets/client.mjs` (Node smoke test). Script: `scripts/check-endpoint.sh` (read-only probe).8384## Companion skills8586- `anthropic-sdk`: the same job for Anthropic Messages clients.87- `litellm`, `llama-swap`: gateways that give stable model names and keys.88- `vllm`, `llama-cpp`, `ollama`: engines behind the endpoint.89- `tool-calling-parsers`: when `tool_calls` come back empty.90- `llm-clients`: choosing and troubleshooting clients of every kind.