From f7ccc25a7f382c3b331261494a26c910989b6d43 Mon Sep 17 00:00:00 2001 From: 9qeklajc Date: Tue, 5 May 2026 23:36:31 +0200 Subject: [PATCH] forward correct model id --- routstr/algorithm.py | 24 +++++++++++++++++++----- routstr/payment/models.py | 2 +- routstr/upstream/base.py | 2 +- routstr/upstream/ollama.py | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/routstr/algorithm.py b/routstr/algorithm.py index 73ff75d1..83c516b7 100644 --- a/routstr/algorithm.py +++ b/routstr/algorithm.py @@ -195,14 +195,15 @@ def create_model_mappings( # Add to unique models base_id = get_base_model_id(model_to_use.id) - if not is_openrouter or base_id not in unique_models: + unique_key = model_to_use.forwarded_model_id or base_id + if not is_openrouter or unique_key not in unique_models: unique_model = model_to_use.copy( update={ "id": base_id, "upstream_provider_id": upstream.provider_type, } ) - unique_models[base_id] = unique_model + unique_models[unique_key] = unique_model # Get all aliases for this model aliases = resolve_model_alias( @@ -272,18 +273,19 @@ def create_model_mappings( continue base_id = get_base_model_id(model_to_use.id) + unique_key = model_to_use.forwarded_model_id or base_id is_openrouter = ( getattr(upstream_for_override, "base_url", "") == "https://openrouter.ai/api/v1" ) - if not is_openrouter or base_id not in unique_models: + if not is_openrouter or unique_key not in unique_models: unique_model = model_to_use.copy( update={ "id": base_id, "upstream_provider_id": upstream_for_override.provider_type, } ) - unique_models[base_id] = unique_model + unique_models[unique_key] = unique_model try: aliases = resolve_model_alias( @@ -322,7 +324,19 @@ def create_model_mappings( provider_map: dict[str, list["BaseUpstreamProvider"]] = {} def alias_priority(model: "Model", alias: str) -> int: - """Rank how strong the mapping of alias->model is.""" + """Rank how strong the mapping of alias->model is. + + forwarded_model_id is the most specific identifier (set per-provider + instance), so a match there should beat a model_id match. This way, + when multiple providers have the same model_id but different + forwarded_model_ids, the one whose forwarded_model_id equals the + requested alias wins. + """ + if ( + model.forwarded_model_id + and model.forwarded_model_id.lower() == alias + ): + return 4 model_base = get_base_model_id(model.id) if model_base == alias: return 3 diff --git a/routstr/payment/models.py b/routstr/payment/models.py index 49ea863f..f7fcb3e0 100644 --- a/routstr/payment/models.py +++ b/routstr/payment/models.py @@ -363,7 +363,7 @@ async def _update_sats_pricing_once() -> None: for m in upstream.get_cached_models() ] upstream._models_cache = updated_models - upstream._models_by_id = {m.id: m for m in updated_models} + upstream._models_by_id = {m.forwarded_model_id or m.id: m for m in updated_models} updated_count += len(updated_models) if updated_count > 0: diff --git a/routstr/upstream/base.py b/routstr/upstream/base.py index 0640efe0..5ff10c1b 100644 --- a/routstr/upstream/base.py +++ b/routstr/upstream/base.py @@ -4305,7 +4305,7 @@ class BaseUpstreamProvider: except Exception: self._models_cache = models_with_fees - self._models_by_id = {m.id: m for m in self._models_cache} + self._models_by_id = {m.forwarded_model_id or m.id: m for m in self._models_cache} except Exception as e: logger.error( diff --git a/routstr/upstream/ollama.py b/routstr/upstream/ollama.py index b8df6f3f..74327d0b 100644 --- a/routstr/upstream/ollama.py +++ b/routstr/upstream/ollama.py @@ -185,7 +185,7 @@ class OllamaUpstreamProvider(BaseUpstreamProvider): except Exception: self._models_cache = models_with_fees - self._models_by_id = {m.id: m for m in self._models_cache} + self._models_by_id = {m.forwarded_model_id or m.id: m for m in self._models_cache} logger.info( f"Refreshed models cache for {self.base_url}", extra={"model_count": len(models)},