[model-gateway] fix v1/models response format to be oai compatible (#13693)

Co-authored-by: Simo Lin <linsimo.mark@gmail.com>
This commit is contained in:
Chang Su
2025-12-01 07:20:38 -08:00
committed by GitHub
co-authored by Simo Lin
parent 79b389da6b
commit ec92d7f14e
3 changed files with 21 additions and 7 deletions
@@ -277,8 +277,7 @@ The SmartHome Mini is a compact smart home assistant available in black or white
def test_model_list(self):
client = openai.Client(api_key=self.api_key, base_url=self.base_url)
# TODO: Update the logic here when router /v1/models response format matching the openai api standard
models = list(client.models.list().models)
models = list(client.models.list().data)
assert len(models) == 1
# assert isinstance(getattr(models[0], "max_model_len", None), int)
@@ -70,8 +70,7 @@ class TestToolChoiceLlama32(CustomTestCase):
def setUp(self):
self.client = openai.Client(base_url=self.base_url, api_key=self.api_key)
# TODO: Update the logic here when router /v1/models response format matching the openai api standard
self.model_name = self.client.models.list().models[0]
self.model_name = self.client.models.list().data[0].id
def _is_flaky_test(self):
"""Check if the current test is marked as flaky for this class"""
+19 -3
View File
@@ -334,14 +334,30 @@ impl RouterTrait for RouterManager {
}
async fn get_models(&self, _req: Request<Body>) -> Response {
let models = self.worker_registry.get_models();
let model_names = self.worker_registry.get_models();
if models.is_empty() {
if model_names.is_empty() {
(StatusCode::SERVICE_UNAVAILABLE, "No models available").into_response()
} else {
// Convert model names to OpenAI-compatible model objects
let models: Vec<Value> = model_names
.iter()
.map(|name| {
serde_json::json!({
"id": name,
"object": "model",
"owned_by": "local"
})
})
.collect();
(
StatusCode::OK,
serde_json::json!({ "models": models }).to_string(),
serde_json::json!({
"object": "list",
"data": models
})
.to_string(),
)
.into_response()
}