From 3a3345951fe67525fc7b2b8273ed19d4a0844e08 Mon Sep 17 00:00:00 2001 From: baKon Date: Wed, 7 Oct 2026 13:53:19 -0500 Subject: [PATCH] serve: /v1/models/:id answers the model object, not a list of one OpenAI's retrieve returns the entry itself, and its SDK reads `id` off the top level, where the list of one this route answered before has none. /v1/models still answers the list. --- core/server/oai.cpp | 18 +++++++++++++----- core/server/oai.h | 24 +++++++++++++++++------- core/server/server.cpp | 6 +++++- docs/GUIDE.md | 2 +- tests/server_test.cpp | 17 +++++++++++------ tests/wire_test.cpp | 20 ++++++++++++++++++-- 6 files changed, 65 insertions(+), 22 deletions(-) diff --git a/core/server/oai.cpp b/core/server/oai.cpp index c0a3205e..75a26e04 100644 --- a/core/server/oai.cpp +++ b/core/server/oai.cpp @@ -2205,19 +2205,27 @@ std::string embeddings_body(const OaiRequest& r, const std::vector 0) m["max_model_len"] = max_model_len; + if (c.max_model_len > 0) m["max_model_len"] = c.max_model_len; + return m; +} + +std::string models_body(const ModelCard& c) { json o; o["object"] = "list"; - o["data"] = json::array({m}); + o["data"] = json::array({model_card(c)}); return o.dump(); } +std::string model_body(const ModelCard& c) { + return model_card(c).dump(); +} + /* ================================================================== SSE */ std::string sse_event(const std::string& json_text) { diff --git a/core/server/oai.h b/core/server/oai.h index 3aa5fa65..0dc2694e 100644 --- a/core/server/oai.h +++ b/core/server/oai.h @@ -318,13 +318,23 @@ std::string text_completion_body(const OaiRequest& r, const std::vector>& vecs, const UsageOut& u); -/* `max_model_len` is the context the server was started with (--max-model-len, else the - * container's trained length), the field vLLM's model card carries. Clients size their own - * context from it -- an agent that compresses its history at the window's edge, a UI that shows - * how much is left -- and without it they guess from the model's name, which is wrong whenever the - * deployment sets its own length. 0 leaves the field out rather than reporting a length the - * server does not know. */ -std::string models_body(const std::string& model_id, int64_t created, int64_t max_model_len = 0); +/* ONE MODEL ENTRY: what /v1/models lists and /v1/models/:id returns on its own. */ +struct ModelCard { + std::string id; + int64_t created = 0; + /* The context the server was started with (--max-model-len, else the container's trained + * length), the field vLLM's model card carries. Clients size their own context from it -- an + * agent that compresses its history at the window's edge, a UI that shows how much is left -- + * and without it they guess from the model's name, which is wrong whenever the deployment sets + * its own length. 0 leaves the field out rather than reporting a length the server does not + * know. */ + int64_t max_model_len = 0; +}; +/* The list, for GET /v1/models. */ +std::string models_body(const ModelCard& m); +/* The entry itself, for GET /v1/models/:id: OpenAI's retrieve answers the model object, not a + * list of one, and its SDK reads `id` off the top level. */ +std::string model_body(const ModelCard& m); /* The usage chunk of each endpoint's stream: an SSE event whose choices array is empty and whose * only payload is `usage`. Emitted when the request asked for stream_options.include_usage, ONCE diff --git a/core/server/server.cpp b/core/server/server.cpp index 1a56cf85..07ebb993 100644 --- a/core/server/server.cpp +++ b/core/server/server.cpp @@ -1153,8 +1153,12 @@ HttpResponse Server::handle_models(const HttpRequest& req) { e.message = "no such model: " + id; return error_response(e, req.peer); } + ModelCard c; + c.id = opt_.model_id; + c.created = started_at_; + c.max_model_len = opt_.max_ctx; metrics_.observe_http(200); - return HttpResponse::json_body(200, models_body(opt_.model_id, started_at_, opt_.max_ctx)); + return HttpResponse::json_body(200, id.empty() ? models_body(c) : model_body(c)); } HttpResponse Server::handle_metrics(const HttpRequest&) { diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 727d84b4..9065b49c 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1132,7 +1132,7 @@ for a model that takes no images or video. | `POST /v1/chat/completions` | chat, with tools, reasoning and streaming | | `POST /v1/completions` | raw text completion | | `POST /v1/embeddings` | needs a model with an embedding head, else 501 | -| `GET /v1/models`, `/v1/models/:id` | what is loaded, with `max_model_len` (the context it serves, as vLLM reports it) | +| `GET /v1/models`, `/v1/models/:id` | what is loaded, with `max_model_len` (the context it serves, as vLLM reports it); `:id` answers the entry itself, as OpenAI's retrieve does | | `GET /health`, `/ping`, `POST /ping` | liveness | | `GET /load` | current load | | `GET /version` | server version | diff --git a/tests/server_test.cpp b/tests/server_test.cpp index 5846e69b..622c8a9d 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -2514,7 +2514,11 @@ TEST(models_and_health_answer) { CHECK_EQ(j["object"].get(), std::string("list")); CHECK_EQ(j["data"][0]["id"].get(), std::string("test-model")); - CHECK_EQ(f.get("/v1/models/test-model").status, 200); + HttpResponse one = f.get("/v1/models/test-model"); + CHECK_EQ(one.status, 200); + /* The entry itself, the way OpenAI's retrieve answers it. */ + CHECK_EQ(json::parse(one.body).value("id", std::string()), std::string("test-model")); + CHECK_EQ(json::parse(one.body).value("object", std::string()), std::string("model")); CHECK_EQ(f.get("/v1/models/other").status, 404); HttpResponse h = f.get("/health"); @@ -2525,11 +2529,12 @@ TEST(models_and_health_answer) { TEST(the_model_entry_carries_the_context_length_the_server_was_started_with) { /* Both routes a client sizes its window from: the list, and the entry it fetches by id. */ Fixture f(true, 262144); - for (const char* path : {"/v1/models", "/v1/models/test-model"}) { - HttpResponse r = f.get(path); - CHECK_EQ(r.status, 200); - CHECK_EQ(json::parse(r.body)["data"][0].value("max_model_len", (int64_t)0), (int64_t)262144); - } + HttpResponse list = f.get("/v1/models"); + CHECK_EQ(list.status, 200); + CHECK_EQ(json::parse(list.body)["data"][0].value("max_model_len", (int64_t)0), (int64_t)262144); + HttpResponse one = f.get("/v1/models/test-model"); + CHECK_EQ(one.status, 200); + CHECK_EQ(json::parse(one.body).value("max_model_len", (int64_t)0), (int64_t)262144); Fixture unknown; CHECK(!json::parse(unknown.get("/v1/models").body)["data"][0].contains("max_model_len")); } diff --git a/tests/wire_test.cpp b/tests/wire_test.cpp index b2cecf0d..68e91fe7 100644 --- a/tests/wire_test.cpp +++ b/tests/wire_test.cpp @@ -404,8 +404,16 @@ json parsed(const std::string& body) { } /* namespace */ +static ModelCard card(const char* id, int64_t created, int64_t max_model_len = 0) { + ModelCard c; + c.id = id; + c.created = created; + c.max_model_len = max_model_len; + return c; +} + TEST(the_model_list_is_a_list_of_one_with_the_fields_clients_read) { - const json o = parsed(models_body("q38-flashnext", 1700000000)); + const json o = parsed(models_body(card("q38-flashnext", 1700000000))); CHECK_EQ(o.value("object", std::string()), std::string("list")); CHECK(o["data"].is_array()); CHECK_EQ(o["data"].size(), 1u); @@ -420,10 +428,18 @@ TEST(the_model_list_is_a_list_of_one_with_the_fields_clients_read) { } TEST(the_model_list_reports_the_context_the_server_runs_with) { - const json o = parsed(models_body("q38-flashnext", 1700000000, 262144)); + const json o = parsed(models_body(card("q38-flashnext", 1700000000, 262144))); CHECK_EQ(o["data"][0].value("max_model_len", (int64_t)0), (int64_t)262144); } +TEST(one_model_is_the_entry_itself_not_a_list_of_one) { + const json m = parsed(model_body(card("q38-flashnext", 1700000000, 262144))); + CHECK_EQ(m.value("object", std::string()), std::string("model")); + CHECK_EQ(m.value("id", std::string()), std::string("q38-flashnext")); + CHECK_EQ(m.value("max_model_len", (int64_t)0), (int64_t)262144); + CHECK(!m.contains("data")); +} + TEST(a_chat_body_carries_usage_and_one_choice_per_completion) { ChoiceOut a; a.index = 0;