Repository navigation
openrouter: read supported params from OpenRouter instead of the bundled model-cost map - #8
Conversation
OpenrouterConfig gated reasoning_effort/thinking on litellm.supports_reasoning, which reads the bundled model-cost map. For OpenRouter that map is wrong by construction: OpenRouter publishes new slugs continuously and the map lags, so a current model answers False and the knob is dropped before the request body is built, silently. OpenRouter publishes the answer itself. GET /api/v1/models returns every model with a supported_parameters list and needs no API key. Read that, cache it for the process, and union it with the existing map-based answer. The union is deliberate rather than letting live data win outright. supported_parameters under-reports reasoning_effort: deepseek/deepseek-r1 is flagged supports_reasoning in the bundled map and is unambiguously a reasoning model, yet OpenRouter advertises only reasoning and include_reasoning for it, treating the OpenAI-compatible spelling as an alias rather than listing it. Reading that absence as a denial would drop a working parameter, which is the failure this change exists to remove. So the lookup can admit a knob the map does not know about but never withholds one the map allows, and the worst case is exactly what shipped before. Fails soft throughout: any error, timeout, non-200 or malformed payload yields no opinion and the caller falls back. A failed fetch is cached for the TTL, so an offline deployment costs one attempt per hour rather than one per request, and a lock keeps concurrent first requests from stampeding the endpoint. LITELLM_OPENROUTER_CAPABILITY_FETCH=0 restores the previous behaviour. Verified against the live endpoint through a Router with the transport mocked: reasoning_effort now reaches the wire for moonshotai/kimi-k3 and z-ai/glm-5.2 (absent from the map, previously dropped), stays absent for poolside/laguna-s-2.1 (which advertises reasoning but not reasoning_effort, and is in neither source), and reverts to absent for all three with the fetch disabled. A conftest disables the lookup for the rest of the OpenRouter unit tests so they stay deterministic and offline; the directory runs in 1.8s with the network blocked.
|
Deployment note for the fleet this was built against, measured after the PR was opened. Sending
On kimi-k3 the distributions do not overlap (lowest baseline sample 2240, highest Nothing here changes the correctness argument: config and wire disagreeing with no signal is a defect whichever way the knob moves generation. But anyone adopting this should audit their config before deploying, because a |
| # Stamped even on failure, so an unreachable endpoint costs one attempt | ||
| # per TTL rather than one per request. | ||
| _CACHE = _fetch() | ||
| _CACHE_STAMP = time.monotonic() |
litellm requires >=3.10, so dict[...]/set[...]/X | None are native. Keeps this module byte-identical to the copy egg's build-time patcher installs (config/litellm/openrouter_capabilities.py), whose linter requires the modern spelling; divergence between the two would be silent drift.
Companion to #7. That PR makes a silent
drop_paramsdrop visible; this one stops the most common cause of it. They are independent and neither needs the other.The problem
OpenrouterConfig.get_supported_openai_paramsgatesreasoning_effortandthinkingonlitellm.supports_reasoning, which reads the bundled model-cost map. For OpenRouter that map is wrong by construction: OpenRouter publishes new slugs continuously, the bundled map lags, and an unlisted model answersFalse. The gate is a bareif, so it fails closed, anddrop_paramsthen discards the parameter without an exception or a log line.Absent is the normal state, not an exotic one. Every OpenRouter slug in the deployment where this was found is missing from the map at the pinned release:
model_costsupports_reasoningopenrouter/moonshotai/kimi-k3Falseopenrouter/z-ai/glm-5.2Falseopenrouter/poolside/laguna-s-2.1Falseopenrouter/deepseek/deepseek-v4-proFalseopenrouter/deepseek/deepseek-r1(control)TrueThe fix
OpenRouter publishes the answer itself.
GET https://openrouter.ai/api/v1/modelsreturns every model with asupported_parameterslist and requires no API key (verified: HTTP 200 unauthenticated, 345 models). This reads that, caches it for the process, and unions it with the existing map-based answer.Why union, and not "live data wins"
I built it the other way first and the existing test suite caught it, correctly.
supported_parametersunder-reportsreasoning_effort.deepseek/deepseek-r1is flaggedsupports_reasoning: truein the bundled map and is unambiguously a reasoning model, yet OpenRouter advertises onlyreasoningandinclude_reasoningfor it. The OpenAI-compatiblereasoning_effortspelling is treated as an alias of the nativereasoningobject rather than listed in its own right. Treating that absence as a denial drops a working parameter, which is the exact failure this PR exists to remove, so it would have traded one silent drop for another.The lookup therefore admits knobs the map does not know about and never withholds one the map allows. The worst case is precisely what shipped before, which also means this cannot regress an existing deployment.
Fails soft, by construction
This sits behind a hot synchronous path, so:
supports_reasoning. A capability lookup must never be able to break a request.LITELLM_OPENROUTER_CAPABILITY_FETCH=0disables it entirely.LITELLM_OPENROUTER_CAPABILITY_TTLandLITELLM_OPENROUTER_CAPABILITY_TIMEOUTtune it.Verification
Driven through a
Routermodel_listentry with the transport mocked and the capability fetch hitting the real endpoint:reasoning_efforton the wire, beforeopenrouter/moonshotai/kimi-k3highopenrouter/z-ai/glm-5.2highopenrouter/poolside/laguna-s-2.1reasoning, notreasoning_effort; in neither source)openrouter/moonshotai/kimi-k3, fetch disabledTesting
:variantsuffixes), fail-soft across non-200 / transport error / malformed payload, the negative cache, TTL expiry, the opt-out, and the gate's behaviour in both directions including the union case above.tests/test_litellm/llms/openrouter/, including the 4 pre-existing reasoning tests that the first design broke.tests/test_litellm/test_utils.py.conftest.pydisables the lookup for the rest of the OpenRouter unit tests so they stay deterministic and offline. The directory runs in 1.8s with the network blocked; without the conftest those tests were doing real HTTP.ruff checkandruff format --checkclean.Scope
Chat completions only, and only the reasoning knobs, which is where the failure was observed. The same data could inform other params (several models do not advertise
seed,top_p, ortemperature), but that would mean withholding params that pass today, so it wants its own change and its own argument.