Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix(orcarouter): resolve the API key explicitly instead of inheriting OPENAI_API_KEY

Saurav Pandabe76e6cceef4
diff --git a/browser_use/llm/orcarouter/chat.py b/browser_use/llm/orcarouter/chat.py
index 66523aba7..e76ae4b52 100644
--- a/browser_use/llm/orcarouter/chat.py
+++ b/browser_use/llm/orcarouter/chat.py
@@ -1,3 +1,4 @@
+import os
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any, TypeVar, overload
@@ -54,11 +55,19 @@ class ChatOrcaRouter(BaseChatModel):
def provider(self) -> str:
return 'orcarouter'
+ def _get_api_key(self) -> str:
+ # AsyncOpenAI falls back to OPENAI_API_KEY when api_key is unset, which would send an
+ # unrelated provider's key to the OrcaRouter endpoint.
+ key = self.api_key or os.getenv('ORCAROUTER_API_KEY')
+ if not key:
+ raise ModelProviderError('Missing OrcaRouter API key', status_code=401, model=self.name)
+ return key
+
def _get_client_params(self) -> dict[str, Any]:
"""Prepare client parameters dictionary."""
# Define base client params
base_params = {
- 'api_key': self.api_key,
+ 'api_key': self._get_api_key(),
'base_url': self.base_url,
'timeout': self.timeout,
'max_retries': self.max_retries,
diff --git a/examples/models/orcarouter.py b/examples/models/orcarouter.py
index 6314b2a9e..9fd9d28d6 100644
--- a/examples/models/orcarouter.py
+++ b/examples/models/orcarouter.py
@@ -5,7 +5,6 @@ Simple try of the agent with OrcaRouter.
"""
import asyncio
-import os
from dotenv import load_dotenv
@@ -14,11 +13,7 @@ from browser_use import Agent, ChatOrcaRouter
load_dotenv()
# OrcaRouter is an OpenAI-compatible model gateway routing to 190+ models via one endpoint.
-llm = ChatOrcaRouter(
- model='orcarouter/auto',
- base_url='https://api.orcarouter.ai/v1',
- api_key=os.getenv('ORCAROUTER_API_KEY'),
-)
+llm = ChatOrcaRouter(model='orcarouter/auto')
agent = Agent(
task='Find the number of stars of the browser-use repo',
llm=llm,
diff --git a/tests/ci/test_orcarouter.py b/tests/ci/test_orcarouter.py
index 1dcaa9e92..15bb2c756 100644
--- a/tests/ci/test_orcarouter.py
+++ b/tests/ci/test_orcarouter.py
@@ -1,5 +1,6 @@
import pytest
+from browser_use.llm.exceptions import ModelProviderError
from browser_use.llm.messages import ContentPartTextParam, SystemMessage, UserMessage
from browser_use.llm.orcarouter.chat import ChatOrcaRouter
from browser_use.llm.orcarouter.serializer import OrcaRouterMessageSerializer
@@ -62,3 +63,25 @@ async def test_registered_orcarouter_llm_never_matches_upstream_pricing(
assert seen_model_names == ['orcarouter/openai/gpt-4o-mini']
assert cost is None
+
+
+def test_orcarouter_reads_api_key_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
+ """ORCAROUTER_API_KEY is the documented env var, so it must actually be read."""
+ monkeypatch.setenv('ORCAROUTER_API_KEY', 'orca-key')
+ monkeypatch.setenv('OPENAI_API_KEY', 'sk-unrelated-openai-key')
+
+ client = ChatOrcaRouter(model='orcarouter/auto').get_client()
+
+ assert client.api_key == 'orca-key'
+
+
+def test_orcarouter_never_falls_back_to_the_openai_key(monkeypatch: pytest.MonkeyPatch) -> None:
+ """An unset OrcaRouter key must fail loudly, not ship OPENAI_API_KEY to the gateway."""
+ monkeypatch.delenv('ORCAROUTER_API_KEY', raising=False)
+ monkeypatch.setenv('OPENAI_API_KEY', 'sk-unrelated-openai-key')
+
+ with pytest.raises(ModelProviderError) as exc_info:
+ ChatOrcaRouter(model='orcarouter/auto').get_client()
+
+ assert exc_info.value.status_code == 401
+ assert 'sk-unrelated-openai-key' not in str(exc_info.value)