Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 83a86d8

Browse files
Fix appropriately setting the route for openrouter when muxing (#1029)
We were not setting the route correctly for openrouter
1 parent b3cd064 commit 83a86d8

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/codegate/muxing/adapter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ class BodyAdapter:
3232

3333
def _get_provider_formatted_url(self, model_route: rulematcher.ModelRoute) -> str:
3434
"""Get the provider formatted URL to use in base_url. Note this value comes from DB"""
35-
if model_route.endpoint.provider_type in [
36-
db_models.ProviderType.openai,
37-
db_models.ProviderType.openrouter,
38-
]:
35+
if model_route.endpoint.provider_type == db_models.ProviderType.openai:
3936
return urljoin(model_route.endpoint.endpoint, "/v1")
37+
if model_route.endpoint.provider_type == db_models.ProviderType.openrouter:
38+
return urljoin(model_route.endpoint.endpoint, "/api/v1")
4039
return model_route.endpoint.endpoint
4140

4241
def set_destination_info(self, model_route: rulematcher.ModelRoute, data: dict) -> dict:
@@ -199,7 +198,8 @@ def _format_antropic(self, chunk: str) -> str:
199198
],
200199
)
201200
return open_ai_chunk.model_dump_json(exclude_none=True, exclude_unset=True)
202-
except Exception:
201+
except Exception as e:
202+
logger.warning(f"Error formatting Anthropic chunk: {chunk}. Error: {e}")
203203
return cleaned_chunk.strip()
204204

205205

tests/muxing/__init__.py

Whitespace-only changes.

tests/muxing/test_adapter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from codegate.db.models import ProviderType
4+
from codegate.muxing.adapter import BodyAdapter
5+
6+
7+
class MockedEndpoint:
8+
def __init__(self, provider_type: ProviderType, endpoint_route: str):
9+
self.provider_type = provider_type
10+
self.endpoint = endpoint_route
11+
12+
13+
class MockedModelRoute:
14+
def __init__(self, provider_type: ProviderType, endpoint_route: str):
15+
self.endpoint = MockedEndpoint(provider_type, endpoint_route)
16+
17+
18+
@pytest.mark.parametrize(
19+
"provider_type, endpoint_route, expected_route",
20+
[
21+
(ProviderType.openai, "https://api.openai.com/", "https://api.openai.com/v1"),
22+
(ProviderType.openrouter, "https://openrouter.ai/api", "https://openrouter.ai/api/v1"),
23+
(ProviderType.openrouter, "https://openrouter.ai/", "https://openrouter.ai/api/v1"),
24+
(ProviderType.ollama, "http://localhost:11434", "http://localhost:11434"),
25+
],
26+
)
27+
def test_catch_all(provider_type, endpoint_route, expected_route):
28+
body_adapter = BodyAdapter()
29+
model_route = MockedModelRoute(provider_type, endpoint_route)
30+
actual_route = body_adapter._get_provider_formatted_url(model_route)
31+
assert actual_route == expected_route

0 commit comments

Comments
 (0)