Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge pull request #19792 from Chesars/fix/openrouter-register-model-index-error

Cesar Garcia587977e19a5b
diff --cc tests/test_litellm/test_utils.py
index 35cb290fccd,6d99d64f7b2..7f0b3b5b501
--- a/tests/test_litellm/test_utils.py
+++ b/tests/test_litellm/test_utils.py
@@@ -2370,13 -2298,66 +2370,71 @@@ def test_register_model_with_scientific
assert registered_model["output_cost_per_token"] == 6e-07
assert registered_model["litellm_provider"] == "openai"
assert registered_model["mode"] == "chat"
+
+ # Clean up after test
+ if test_model_name in litellm.model_cost:
+ del litellm.model_cost[test_model_name]
+ _invalidate_model_cost_lowercase_map()
+ def test_register_model_openrouter_without_slash():
+ """
+ Test that register_model handles openrouter models without '/' in the name.
+
+ Fixes https://github.com/BerriAI/litellm/issues/18936
+
+ Previously, the code did `split_string[1]` which would fail with IndexError
+ when the model name didn't contain '/'. Now it uses `split_string[-1]` which
+ always works.
+ """
+ # Clear any existing entries
+ litellm.openrouter_models.discard("my-custom-alias")
+ litellm.openrouter_models.discard("gpt-4")
+ litellm.openrouter_models.discard("openai/gpt-4")
+
+ # Test 1: Model name without '/' (this was the bug - would raise IndexError)
+ litellm.register_model(
+ {
+ "my-custom-alias": {
+ "max_tokens": 8192,
+ "input_cost_per_token": 0.00001,
+ "output_cost_per_token": 0.00002,
+ "litellm_provider": "openrouter",
+ "mode": "chat",
+ },
+ }
+ )
+ assert "my-custom-alias" in litellm.openrouter_models
+
+ # Test 2: Model name with single '/' (openrouter/model format)
+ litellm.register_model(
+ {
+ "openrouter/gpt-4": {
+ "max_tokens": 8192,
+ "input_cost_per_token": 0.00001,
+ "output_cost_per_token": 0.00002,
+ "litellm_provider": "openrouter",
+ "mode": "chat",
+ },
+ }
+ )
+ assert "gpt-4" in litellm.openrouter_models
+
+ # Test 3: Model name with double '/' (openrouter/provider/model format)
+ litellm.register_model(
+ {
+ "openrouter/openai/gpt-4-turbo": {
+ "max_tokens": 8192,
+ "input_cost_per_token": 0.00001,
+ "output_cost_per_token": 0.00002,
+ "litellm_provider": "openrouter",
+ "mode": "chat",
+ },
+ }
+ )
+ assert "openai/gpt-4-turbo" in litellm.openrouter_models
+
+
def test_reasoning_content_preserved_in_text_completion_wrapper():
"""Ensure reasoning_content is copied from delta to text_choices."""
chunk = ModelResponseStream(