Skip to content

Commit 62ca606

Browse files
committed
feat(openrouter): sort models alphabetically by name
1 parent bb6e3d3 commit 62ca606

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

basilisk/provider_engine/openrouter_engine.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def _load_models(self) -> list[ProviderAIModel]:
6161
data = response.json()
6262
models = parse_model_rows(data.get("data", []))
6363
log.debug("Got %d models", len(models))
64+
models.sort(key=lambda m: (m.name or m.id).casefold())
6465
return models
6566

6667
def completion(

tests/provider_engine/test_openrouter_engine.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def _model_row(model_id: str, created=0) -> dict:
3636
}
3737

3838

39-
def test_models_sorted_by_created_desc(httpx_mock, monkeypatch, tmp_path):
40-
"""OpenRouter model list is sorted by created timestamp descending."""
39+
def test_models_sorted_alphabetically(httpx_mock, monkeypatch, tmp_path):
40+
"""OpenRouter model list is sorted alphabetically by name."""
4141
httpx_mock.add_response(
4242
json={
4343
"data": [
@@ -50,8 +50,7 @@ def test_models_sorted_by_created_desc(httpx_mock, monkeypatch, tmp_path):
5050
)
5151
engine = _make_engine(monkeypatch, tmp_path)
5252
models = engine.models
53-
assert [model.id for model in models] == ["newer", "middle", "older"]
54-
assert [model.created for model in models] == [3000, 2000, 1000]
53+
assert [model.id for model in models] == ["middle", "newer", "older"]
5554
for m in models:
5655
assert m.extra_info["metadata_catalog"] == CATALOG_SOURCE_OPENROUTER_API
5756

@@ -71,11 +70,15 @@ def test_models_invalid_created_falls_back_to_zero(
7170
)
7271
engine = _make_engine(monkeypatch, tmp_path)
7372
models = engine.models
73+
# Alphabetical order: invalid-created < valid-created
7474
assert [model.id for model in models] == [
75-
"valid-created",
7675
"invalid-created",
76+
"valid-created",
7777
]
78-
assert [model.created for model in models] == [42, 0]
78+
assert {m.id: m.created for m in models} == {
79+
"valid-created": 42,
80+
"invalid-created": 0,
81+
}
7982

8083

8184
def test_models_non_200_response_raises(httpx_mock, monkeypatch, tmp_path):

0 commit comments

Comments
 (0)