Serve a model with vLLM¶
Serves Qwen/Qwen3-0.6B with vLLM's OpenAI-compatible server on one H100 endpoint behind a bearer token, sends one chat request, and deletes the endpoint.
View example files ↗ · Related reference: Endpoints →
What this example does¶
serve.pycreates the endpoint, waits until/v1/modelsanswers, sends one request to/v1/chat/completions, prints the reply, and deletes the endpoint infinally, also when a step fails or you press Ctrl-C.endpoint.yamlis the same spec as YAML.- The endpoint uses token auth, the default:
create()generates a random token, keeps it inendpoint.token, and callers sendAuthorization: Bearer <token>.
Requirements¶
- A Nebius profile that can create endpoints: see Install and authenticate.
- Spot H100 quota in the project's region.
Runtime and compute¶
| Machine | gpu="h100", spot (the default on a GPU). A preemption stops the endpoint; start() runs it again |
| Port | 8000, served at a managed https:// URL |
| Disk | 500Gi |
| Image | vllm/vllm-openai:v0.19.1 |
An endpoint bills for compute from create until it is stopped or deleted. Closing the process does neither.
Run it¶
The same spec from YAML:
import nebius_serverless as ns
spec = ns.EndpointSpec.from_yaml("examples/inference/vllm-endpoint/endpoint.yaml")
print(ns.Endpoint(spec).dry_run()) # the create request; nothing is created
Expected output¶
create() prints the endpoint id and a notice that a token was generated. wait(probe="/v1/models") prints each state, then the URL under RUNNING, then the probe's answers until the model responds. Pulling the image and loading the model take a few minutes. The script then prints the model's reply, and delete() prints that deletion was accepted.
Call it yourself¶
This client does not wrap inference. Send HTTP to endpoint.url with the token:
import urllib.request
request = urllib.request.Request(
endpoint.url.rstrip("/") + "/v1/models",
headers={"Authorization": f"Bearer {endpoint.token}"},
)
with urllib.request.urlopen(request) as response:
print(response.status)
Keep it, stop it, start it again¶
Save endpoint.id to come back later. endpoints.get fetches the endpoint and creates nothing; reveal_token() fetches its token:
A stopped endpoint is not billed for compute or its disk; mounted volumes still bill. It comes back at the same URL:
endpoint.stop()
endpoint.wait(until="stopped")
endpoint.start()
endpoint.wait(probe="/v1/models") # the model loads again
How to adapt it¶
- Serve another model: change
--modelin the command, andMODELinserve.py. - Spot pricing:
pricing="spot". A preempted spot endpoint goes toSTOPPED;start()resumes it. - Keep the token in MysteryBox for production:
token_secret="mbsec-…", whose payload hasAUTH_TOKEN. Clients then read the token from MysteryBox. auth="none"makes a public URL that anyone who has it can call, at your cost. Use it only on purpose.
Troubleshooting¶
- The endpoint ends in
ERRORbeforeRUNNING. Usually there was no capacity for the machine. An endpoint inERRORis not billed;display()shows the reason. Delete it, then retry later or in another region. RUNNING, but requests fail.RUNNINGwith a URL means the tunnel is up, not that vLLM has loaded the model. Wait withprobe=, asserve.pydoes.AccessErrorfrom the probe. The endpoint answered 401 or 403: the handle has no token or the wrong one. Callreveal_token()or passtoken=towait.WaitTimeoutafter 60 minutes. The endpoint is unchanged and may still be billing. Callwait()again, ordelete().
Source¶
"""Serve a small model with vLLM on an endpoint, call it, then delete it.
``gpu="h100"`` with token auth. Uses the active Nebius profile. The same
recipe as YAML is ``endpoint.yaml`` in this folder.
The endpoint bills for compute while it runs. This script deletes it in
``finally``, including when a step fails or you press Ctrl-C.
"""
import json
import urllib.request
import nebius_serverless as ns
MODEL = "Qwen/Qwen3-0.6B"
def main() -> None:
"""Create the endpoint, wait for the model, send one request, delete."""
# Describe what should serve, then create it once. Omit command to keep
# the image entrypoint. The generated token is endpoint.token.
endpoint = ns.Endpoint(
command=[
"bash",
"-c",
"python3 -m vllm.entrypoints.openai.api_server "
f"--model {MODEL} --host 0.0.0.0 --port 8000",
],
image="vllm/vllm-openai:v0.19.1",
gpu="h100",
port=8000,
disk="500Gi",
name="qwen-tiny",
).create()
try:
# RUNNING with a URL, then /v1/models answers: the model is loaded.
endpoint.wait(probe="/v1/models")
# ERROR and STOPPED end the wait as data. An ERROR record can still
# list the URL, so check the state, not the URL.
if endpoint.status != "RUNNING":
raise SystemExit(f"{endpoint.id} ended {endpoint.status}")
print(_chat(endpoint, "Say hello in five words."))
finally:
endpoint.delete()
def _chat(endpoint: ns.Endpoint, prompt: str) -> str:
assert endpoint.url is not None
body = {
"model": MODEL,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 64,
}
request = urllib.request.Request(
endpoint.url.rstrip("/") + "/v1/chat/completions",
data=json.dumps(body).encode(),
headers={
"Authorization": f"Bearer {endpoint.token}",
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(request, timeout=60) as response:
payload = json.load(response)
return payload["choices"][0]["message"]["content"]
if __name__ == "__main__":
main()