Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions opengateway/mojo_bridge/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def chat_completion(
_enforce_rate_limit(auth)

fallback_module = (
_module_name_for(route.fallback)
if route is not None and route.fallback
else None
_module_name_for(route.fallback) if route is not None and route.fallback else None
)
result, used_module = _run_with_fallback(
body,
Expand Down Expand Up @@ -89,9 +87,7 @@ def _run_with_fallback(
import asyncio

return asyncio.run(
chat_with_fallback(
body, primary_module=primary_module, fallback_module=fallback_module
)
chat_with_fallback(body, primary_module=primary_module, fallback_module=fallback_module)
)


Expand Down
15 changes: 6 additions & 9 deletions opengateway/mojo_bridge/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ def __init__(self, original: BaseException) -> None:
self.original = original


def _provider_call_kwargs(
provider_module: str, body: dict[str, Any]
) -> tuple[str, str | None]:
def _provider_call_kwargs(provider_module: str, body: dict[str, Any]) -> tuple[str, str | None]:
"""Resolve the API key + base URL for ``provider_module``.

Only enforces "API key configured" for modules with a registered
Expand All @@ -215,9 +213,7 @@ def _provider_call_kwargs(
return api_key or "", resolve_provider_base_url(provider_module)


async def _provider_chat(
provider_module: str, body: dict[str, Any]
) -> _OpenAIEnvelope:
async def _provider_chat(provider_module: str, body: dict[str, Any]) -> _OpenAIEnvelope:
"""Run a single non-streaming provider call, mapping transient
failures to ``_RetryableError`` so ``chat_with_fallback`` can pick them
up, and mapping non-retryable upstream HTTP errors to a plain
Expand Down Expand Up @@ -245,8 +241,7 @@ async def _provider_chat(
# internal_error, since this is the provider failing, not
# us.
raise RuntimeError(
f"upstream provider {provider_module} returned "
f"{exc.response.status_code}"
f"upstream provider {provider_module} returned {exc.response.status_code}"
) from exc
raise
finally:
Expand Down Expand Up @@ -359,7 +354,9 @@ async def chat_stream_with_fallback(

# ── Fallback attempt ───────────────────────────────────────────────
if fallback_module is None or fallback_module == primary_module or primary_failed is None:
raise StreamFallbackError(primary_failed or RuntimeError("primary failed"), on_fallback=False)
raise StreamFallbackError(
primary_failed or RuntimeError("primary failed"), on_fallback=False
)

logger.info(
"retried streaming on fallback",
Expand Down
4 changes: 1 addition & 3 deletions opengateway/mojo_bridge/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ def start_streaming_chat(
raise RuntimeError(f"no API key configured for provider {resolved_primary}")

fallback_module = (
_routing_module_name(route.fallback)
if route is not None and route.fallback
else None
_routing_module_name(route.fallback) if route is not None and route.fallback else None
)
if (
fallback_module is not None
Expand Down
12 changes: 3 additions & 9 deletions tests/test_cost_cap.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def test_cost_cap_rejects_when_estimate_exceeds_limit(
_set_routes(monkeypatch)
monkeypatch.setenv("ROOT_KEY", "sk-root-good")
monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
store = _FakeStore(
{_hash_key("sk-og-tenant"): _fake_record(max_cost_usd=0.01)}
)
store = _FakeStore({_hash_key("sk-og-tenant"): _fake_record(max_cost_usd=0.01)})
monkeypatch.setattr("opengateway.mojo_bridge.db.get_store", lambda: store)

envelope = handle_chat(
Expand Down Expand Up @@ -165,9 +163,7 @@ def test_cost_cap_skipped_when_no_route_configured(
monkeypatch.setenv("ROOT_KEY", "sk-root-good")
monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
monkeypatch.delenv("ROUTES_JSON", raising=False)
store = _FakeStore(
{_hash_key("sk-og-tenant"): _fake_record(max_cost_usd=0.0001)}
)
store = _FakeStore({_hash_key("sk-og-tenant"): _fake_record(max_cost_usd=0.0001)})
monkeypatch.setattr("opengateway.mojo_bridge.db.get_store", lambda: store)

envelope = handle_chat(
Expand Down Expand Up @@ -214,9 +210,7 @@ def test_cost_cap_enforced_on_streaming_too(
_set_routes(monkeypatch)
monkeypatch.setenv("ROOT_KEY", "sk-root-good")
monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
store = _FakeStore(
{_hash_key("sk-og-tenant"): _fake_record(max_cost_usd=0.01)}
)
store = _FakeStore({_hash_key("sk-og-tenant"): _fake_record(max_cost_usd=0.01)})
monkeypatch.setattr("opengateway.mojo_bridge.db.get_store", lambda: store)

envelope = handle_chat_stream(
Expand Down
Loading