litellm/tests/unit/llms/openrouter
ellisc42 d7e0d23686
fix(openrouter): report the context window and output cap from OpenRouter's catalog (#54046)
* fix(openrouter): list OpenRouter's live catalog for openrouter/* when check_provider_endpoint is on

* fix(openrouter): list models off the event loop and keep only chat-callable catalog ids

The model-list builders are sync, and with check_provider_endpoint on they call OpenRouter's catalog with a 10 second timeout and no caching of failures. They ran on the event loop inside /v1/models, /model/info, /model_group/info and the per-user listing, so one slow catalog froze every request on the worker. Those calls now run in a worker thread with asyncio.to_thread

File, batch and RAG credential lookups only need each name's deployment, and a wildcard that is itself a deployment sits in the list ahead of its expansions and resolves its own credentials. get_complete_model_list takes expand_deployed_wildcard_routes, which that lookup sets to False, so a deployed wildcard never fetches its provider catalog there. A key wildcard wider than any deployment, like anthropic/* over a deployed anthropic/claude-*, still expands so it finds the matching deployment's credentials. Every other caller keeps the default

The pattern router's model lookup tolerates a pattern removed between its two reads of the pattern table, which a deployment delete on the event loop can now do while a listing runs in a thread

Listing now reads only OpenRouter's models/user catalog. OpenRouter's own /models list carries no embedding models, the OpenAI-format /v1/models has no mode field so chat pickers cannot tell embeddings apart, /model_group/info labels them mode chat, and one failing catalog emptied the whole list. Ids ending in :batch are dropped because chat completions answers 404 for them, while :free and every other variant stay. An api_base that already ends in /chat/completions, which chat accepts as is, has that suffix stripped before the catalog route is joined

* fix(proxy): keep model listing off the shared thread pool and out of credential lookups

Listings run on their own thread pool so many slow catalogs cannot starve
the default executor that chat and Responses calls share. File and batch
credential lookups match deployed names against a key's wildcard grants
instead of listing any provider catalog. The token counter keeps OpenRouter
deployments on their previous path, and a catch-all "*" over provider/*
lists bare ids that route back through it

* test(proxy): give the off-loop listing tests a generous deadlock guard and typed ids

* fix(proxy): keep routed models in wildcard model info, skip key-only OpenRouter catalogs, cache failed listings briefly

* fix(utils): cache failed model listings under their own key so a late failure never hides a good listing

* fix(proxy): remember only timed-out provider listings so a provider that failed fast is listed again

* fix(proxy): ask each provider catalog once per worker, keep deployed names out of catch-all ids, and match file credentials by name

Listings that start together for one deployment now wait for the first catalog request. A catch-all wildcard no longer lists a name a deployment serves by name. A key wildcard only finds file and batch credentials on deployments named under it, so a bare name keeps the default credentials as before.

* fix(proxy): keep the listing failure log line the same for timed-out listings

* refactor(proxy): fold the cached listing lookup back into the provider listing function

* fix(proxy): list each model id once and keep a named deployment's row over a wildcard's

* fix(openrouter): show published limits on the proxy model-info routes

An OpenRouter model with no cost-map row showed a null context window and
output cap on /v1/model/info and /model_group/info, so lite pi fell back to
128k. The model-info refresh now reads OpenRouter's /models/user catalog, keeps
one shared catalog per wildcard deployment, and looks up each concrete model on
read, with no new call on the request path. Operator model_info still wins, and
catalog values win over a cost-map row. Wildcard rows on /v1/models show the
same limits, since the lite OpenCode and Codex model sync reads them there

* fix(proxy): drop a wildcard's model info row when named deployments serve every name it lists

* fix(openrouter): keep /v1/models cheap when no wildcard catalog is cached

* test(router): list the OpenRouter discovery helpers in the router coverage gate

* fix(openrouter): keep catalog limits through outages, scope the env key, share one fetch per key

A failed catalog fetch now keeps the last published limits instead of
dropping them after the cache entry ages out, and a model the catalog no
longer lists loses its limits on the next good refresh.

The proxy-wide OpenRouter key goes only to openrouter.ai. A deployment
with its own api_base on another host sends only its own key.

Overlapping refreshes on one key share a single catalog download.

A team-owned openrouter/* wildcard now shows each model's catalog limits
on /v1/models, and the wildcard-catalog flag resets when the model list
is replaced

* fix(openrouter): scope wildcard listing limits to the caller's team, read wildcard catalog rows at request time

* fix(openrouter): keep discovered limits out of model saves, send the shared key only to openrouter.ai, list only routable wildcard names

A model saved from the Admin UI echoes the limits /model/info showed, and PATCH /model/{id}/update stored them
as overrides, so the model stopped following its backend. Limits equal to the discovered ones are now display
values on both update routes. The catalog request now picks its key from the host it actually calls, so a global
api_base never gets the proxy-wide OpenRouter key. Wildcard listing resolves names the way requests route, and a
refresh clears the group caches once at its end

* fix(router): keep a deployment's discovered limits when an edit leaves its backend alone

Saving a model in the UI replaced the deployment, and the discovered limits
only matched the old deployment object, so the model info routes showed no
limits until the next refresh, up to 5 minutes later. An edit that changes
only tpm, rpm, max_parallel_requests or token prices now carries the
discovered limits to the new deployment. Any other change still waits for
the refresh, so a new api_base or key never shows the old backend's limits

* fix(router): keep other deployments' discovered limits when a save carries one over

The discovered-limits cache holds one entry per deployment, so writing the carried entry over a full cache evicted another deployment's limits first. Delete the edited entry before writing it, as the refresh path does

* test(router): pin that discovered limits outlive only a same-backend replacement

Main's test pinned that any replacement forgets discovered limits. A replacement that changes only rpm now keeps them, and one that points at another api_base still forgets them

* fix(openrouter): keep typed limits, show new models' limits at once, scope team wildcards

A request that types a limit equal to the discovered one is stored again, since only
a /model/info row carrying its key reads as an echo. Wildcard rows now carry their
routed model as key, so echoing one back no longer pins its limits on the wildcard.

A model added through the API, a DB sync or a read-through starts a model info refresh,
so its backend limits show right away instead of after up to 5 minutes.

A team wildcard under the public openrouter/* name lists its catalog limits only to its
team. Regional openrouter.ai hosts get the proxy-wide key for the catalog. Cached limits
are a frozen pydantic model, so the in-memory cache debug route encodes them again, and
fetch locks are kept per event loop.

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-24 04:37:30 -08:00
..
chat fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
image_edit test: add package initializers to migrated unit test directories 2026-09-20 11:52:07 +00:00
image_generation refactor(types): replace Any with proven types in 137 files (#44478) 2026-10-05 11:01:45 -07:00
responses fix(spend): keep a NaN or infinite cost from wedging daily spend writes (#48829) 2026-10-30 08:27:35 -07:00
__init__.py test: add __init__.py to intermediate phase 12 unit test directories 2026-09-20 12:37:21 +00:00
test_openrouter_common_utils.py fix(openrouter): report the context window and output cap from OpenRouter's catalog (#54046) 2026-11-24 04:37:30 -08:00
test_openrouter_embedding_transformation.py test: migrate tests/llm_translation into unit, integration and e2e tiers (#45759) 2026-10-11 07:58:53 +00:00
test_openrouter_provider_routing.py test: migrate openai, openai_like and openrouter legacy tests to tests/unit 2026-09-20 10:40:27 +00:00