Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: add openrouter as a supported provider (#6531)

Myles Scolnick6d7d4d3b11c8
diff --git a/frontend/src/components/ai/ai-provider-icon.tsx b/frontend/src/components/ai/ai-provider-icon.tsx
index 83d3960d8a..14b9d8d53b 100644
--- a/frontend/src/components/ai/ai-provider-icon.tsx
+++ b/frontend/src/components/ai/ai-provider-icon.tsx
@@ -8,6 +8,7 @@ import GitHubIcon from "@marimo-team/llm-info/icons/github.svg?inline";
import GeminiIcon from "@marimo-team/llm-info/icons/googlegemini.svg?inline";
import OllamaIcon from "@marimo-team/llm-info/icons/ollama.svg?inline";
import OpenAIIcon from "@marimo-team/llm-info/icons/openai.svg?inline";
+import OpenRouterIcon from "@marimo-team/llm-info/icons/openrouter.svg?inline";
import { BotIcon } from "lucide-react";
import * as React from "react";
import type { ProviderId } from "@/core/ai/ids/ids";
@@ -27,6 +28,7 @@ const icons: Record<ProviderId | Aliases, string> = {
bedrock: BedrockIcon,
deepseek: DeepseekIcon,
github: GitHubIcon,
+ openrouter: OpenRouterIcon,
marimo: marimoIcon,
};
diff --git a/frontend/src/components/app-config/ai-config.tsx b/frontend/src/components/app-config/ai-config.tsx
index 74de26c053..3017cabbae 100644
--- a/frontend/src/components/app-config/ai-config.tsx
+++ b/frontend/src/components/app-config/ai-config.tsx
@@ -734,6 +734,36 @@ export const AiProvidersConfig: React.FC<AiConfigProps> = ({
/>
</AccordionFormItem>
+ <AccordionFormItem
+ title="OpenRouter"
+ provider="openrouter"
+ isConfigured={hasValue("ai.openrouter.api_key")}
+ >
+ <ApiKey
+ form={form}
+ config={config}
+ name="ai.openrouter.api_key"
+ placeholder="or-..."
+ testId="ai-openrouter-api-key-input"
+ description={
+ <>
+ Your OpenRouter API key from {""}
+ <ExternalLink href="https://openrouter.ai/keys">
+ openrouter.ai
+ </ExternalLink>
+ .
+ </>
+ }
+ />
+ <BaseUrl
+ form={form}
+ config={config}
+ name="ai.openrouter.base_url"
+ placeholder="https://openrouter.ai/api/v1/"
+ testId="ai-openrouter-base-url-input"
+ />
+ </AccordionFormItem>
+
<AccordionFormItem
title="Azure"
provider="azure"
diff --git a/frontend/src/components/ui/links.tsx b/frontend/src/components/ui/links.tsx
index 804cabf4f5..7433f023ee 100644
--- a/frontend/src/components/ui/links.tsx
+++ b/frontend/src/components/ui/links.tsx
@@ -8,6 +8,7 @@ export const ExternalLink = ({
| `https://console.anthropic.com/${string}`
| `https://aistudio.google.com/${string}`
| `https://github.com/${string}`
+ | `https://openrouter.ai/${string}`
| `https://docs.marimo.io/${string}`
| `https://docs.python.org/${string}`
| `https://marimo.io/${string}`
diff --git a/frontend/src/core/ai/__tests__/model-registry.test.ts b/frontend/src/core/ai/__tests__/model-registry.test.ts
index 1b30239c6a..1d64ad4ed5 100644
--- a/frontend/src/core/ai/__tests__/model-registry.test.ts
+++ b/frontend/src/core/ai/__tests__/model-registry.test.ts
@@ -250,16 +250,6 @@ describe("AiModelRegistry", () => {
const providers = listModelsByProvider.map(([provider]) => provider);
expect(providers).toEqual(PROVIDER_SORT_ORDER);
});
-
- it("should include custom providers at the top", () => {
- const customModels = ["openrouter/custom-gpt"];
- const registry = AiModelRegistry.create({ customModels });
- const listModelsByProvider = registry.getListModelsByProvider();
- expect(listModelsByProvider).toHaveLength(4);
-
- const providers = listModelsByProvider.map(([provider]) => provider);
- expect(providers).toEqual(["openrouter", ...PROVIDER_SORT_ORDER]);
- });
});
describe("getCustomModels", () => {
diff --git a/frontend/src/core/ai/ids/ids.ts b/frontend/src/core/ai/ids/ids.ts
index a3473954da..90fc5f33b5 100644
--- a/frontend/src/core/ai/ids/ids.ts
+++ b/frontend/src/core/ai/ids/ids.ts
@@ -11,6 +11,7 @@ export const PROVIDERS = [
"deepseek",
"azure",
"github",
+ "openrouter",
"marimo",
] as const;
export type ProviderId = (typeof PROVIDERS)[number];
diff --git a/frontend/src/core/ai/model-registry.ts b/frontend/src/core/ai/model-registry.ts
index 71c54f5a74..ba5cf6f4e9 100644
--- a/frontend/src/core/ai/model-registry.ts
+++ b/frontend/src/core/ai/model-registry.ts
@@ -19,6 +19,7 @@ export const PROVIDER_SORT_ORDER: ProviderId[] = [
"openai",
"google",
"github",
+ "openrouter",
"deepseek",
"azure",
"bedrock",
diff --git a/frontend/src/core/config/config-schema.ts b/frontend/src/core/config/config-schema.ts
index 771e43d0ba..c7ea7d57e8 100644
--- a/frontend/src/core/config/config-schema.ts
+++ b/frontend/src/core/config/config-schema.ts
@@ -163,6 +163,7 @@ export const UserConfigSchema = z
anthropic: AiConfigSchema.optional(),
google: AiConfigSchema.optional(),
ollama: AiConfigSchema.optional(),
+ openrouter: AiConfigSchema.optional(),
open_ai_compatible: AiConfigSchema.optional(),
azure: AiConfigSchema.optional(),
bedrock: z
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index ecb84f86a3..4d8064dd3a 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -272,6 +272,7 @@ class AiConfig(TypedDict, total=False):
- `azure`: the Azure config
- `ollama`: the Ollama config
- `github`: the GitHub config
+ - `openrouter`: the OpenRouter config
- `open_ai_compatible`: the OpenAI-compatible config
"""
@@ -288,6 +289,7 @@ class AiConfig(TypedDict, total=False):
azure: OpenAiConfig
ollama: OpenAiConfig
github: GitHubConfig
+ openrouter: OpenAiConfig
open_ai_compatible: OpenAiConfig
diff --git a/marimo/_server/ai/config.py b/marimo/_server/ai/config.py
index 3f6fafa3b4..0f23815b6f 100644
--- a/marimo/_server/ai/config.py
+++ b/marimo/_server/ai/config.py
@@ -95,6 +95,19 @@ class AnyProviderConfig:
require_key=True,
)
+ @classmethod
+ def for_openrouter(cls, config: AiConfig) -> AnyProviderConfig:
+ fallback_key = cls.os_key("OPENROUTER_API_KEY")
+ return cls._for_openai_like(
+ config,
+ "openrouter",
+ "OpenRouter",
+ fallback_key=fallback_key,
+ # Default base URL for OpenRouter
+ fallback_base_url="https://openrouter.ai/api/v1/",
+ require_key=True,
+ )
+
@classmethod
def _for_openai_like(
cls,
@@ -184,6 +197,8 @@ class AnyProviderConfig:
return cls.for_azure(config)
elif model_id.provider == "github":
return cls.for_github(config)
+ elif model_id.provider == "openrouter":
+ return cls.for_openrouter(config)
elif model_id.provider == "openai_compatible":
return cls.for_openai_compatible(config)
else:
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index a371868d0a..0b8f73d27d 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -988,6 +988,8 @@ def get_completion_provider(
return BedrockProvider(model_id.model, config)
elif model_id.provider == "azure":
return AzureOpenAIProvider(model_id.model, config)
+ elif model_id.provider == "openrouter":
+ return OpenAIProvider(model_id.model, config)
else:
return OpenAIProvider(model_id.model, config)
diff --git a/packages/llm-info/data/providers.yml b/packages/llm-info/data/providers.yml
index 2acb14cac9..4c84eaac16 100644
--- a/packages/llm-info/data/providers.yml
+++ b/packages/llm-info/data/providers.yml
@@ -37,3 +37,38 @@
id: marimo
description: marimo's hosted models.
url: https://docs.marimo.io/guides/editor_features/ai_completion/
+
+- name: DeepSeek
+ id: deepseek
+ description: DeepSeek's API for GPT models.
+ url: https://deepseek.com/
+
+- name: LM Studio
+ id: lmstudio
+ description: LM Studio's API for GPT models.
+ url: https://lmstudio.ai/
+
+- name: OpenRouter
+ id: openrouter
+ description: OpenRouter's API for GPT models.
+ url: https://openrouter.ai/
+
+- name: Mistral
+ id: mistral
+ description: Mistral's API for GPT models.
+ url: https://mistral.ai/
+
+- name: Vercel v0
+ id: vercel
+ description: Vercel's API for GPT models.
+ url: https://vercel.com/
+
+- name: Together AI
+ id: together
+ description: Together AI's API for GPT models.
+ url: https://together.ai/
+
+- name: xAI
+ id: xai
+ description: xAI's API for GPT models.
+ url: https://x.ai/
diff --git a/packages/llm-info/icons/openrouter.svg b/packages/llm-info/icons/openrouter.svg
new file mode 100644
index 0000000000..e6cca2a869
--- /dev/null
+++ b/packages/llm-info/icons/openrouter.svg
@@ -0,0 +1 @@
+<svg fill="currentColor" fill-rule="evenodd" height="1em" style="flex:none;line-height:1" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><title>OpenRouter</title><path d="M16.804 1.957l7.22 4.105v.087L16.73 10.21l.017-2.117-.821-.03c-1.059-.028-1.611.002-2.268.11-1.064.175-2.038.577-3.147 1.352L8.345 11.03c-.284.195-.495.336-.68.455l-.515.322-.397.234.385.23.53.338c.476.314 1.17.796 2.701 1.866 1.11.775 2.083 1.177 3.147 1.352l.3.045c.694.091 1.375.094 2.825.033l.022-2.159 7.22 4.105v.087L16.589 22l.014-1.862-.635.022c-1.386.042-2.137.002-3.138-.162-1.694-.28-3.26-.926-4.881-2.059l-2.158-1.5a21.997 21.997 0 00-.755-.498l-.467-.28a55.927 55.927 0 00-.76-.43C2.908 14.73.563 14.116 0 14.116V9.888l.14.004c.564-.007 2.91-.622 3.809-1.124l1.016-.58.438-.274c.428-.28 1.072-.726 2.686-1.853 1.621-1.133 3.186-1.78 4.881-2.059 1.152-.19 1.974-.213 3.814-.138l.02-1.907z"></path></svg>
\ No newline at end of file
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 5b196a082e..b227cb216f 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -94,7 +94,8 @@ components:
\ the OpenAI config\n - `anthropic`: the Anthropic config\n - `google`:\
\ the Google AI config\n - `bedrock`: the Bedrock config\n - `azure`:\
\ the Azure config\n - `ollama`: the Ollama config\n - `github`: the\
- \ GitHub config\n - `open_ai_compatible`: the OpenAI-compatible config"
+ \ GitHub config\n - `openrouter`: the OpenRouter config\n - `open_ai_compatible`:\
+ \ the OpenAI-compatible config"
properties:
anthropic:
$ref: '#/components/schemas/AnthropicConfig'
@@ -120,6 +121,8 @@ components:
$ref: '#/components/schemas/OpenAiConfig'
open_ai_compatible:
$ref: '#/components/schemas/OpenAiConfig'
+ openrouter:
+ $ref: '#/components/schemas/OpenAiConfig'
rules:
type: string
required: []
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 51badc1dd9..a9559cc061 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -2686,6 +2686,7 @@ export interface components {
* - `azure`: the Azure config
* - `ollama`: the Ollama config
* - `github`: the GitHub config
+ * - `openrouter`: the OpenRouter config
* - `open_ai_compatible`: the OpenAI-compatible config
*/
AiConfig: {
@@ -2701,6 +2702,7 @@ export interface components {
ollama?: components["schemas"]["OpenAiConfig"];
open_ai?: components["schemas"]["OpenAiConfig"];
open_ai_compatible?: components["schemas"]["OpenAiConfig"];
+ openrouter?: components["schemas"]["OpenAiConfig"];
rules?: string;
};
/** AiInlineCompletionRequest */
diff --git a/tests/_config/test_secrets_config.py b/tests/_config/test_secrets_config.py
index 5c7f1c1a33..4a22f08aac 100644
--- a/tests/_config/test_secrets_config.py
+++ b/tests/_config/test_secrets_config.py
@@ -15,6 +15,7 @@ def test_mask_secrets() -> None:
"anthropic": {"api_key": "anthropic_secret"},
"google": {"api_key": "google_secret"},
"github": {"api_key": "github_secret"},
+ "openrouter": {"api_key": "openrouter_secret"},
"bedrock": {
"aws_access_key_id": "bedrock_access_key_id",
"aws_secret_access_key": "bedrock_secret_access_key",
@@ -28,6 +29,7 @@ def test_mask_secrets() -> None:
assert config["ai"]["anthropic"]["api_key"] == "anthropic_secret"
assert config["ai"]["google"]["api_key"] == "google_secret"
assert config["ai"]["github"]["api_key"] == "github_secret"
+ assert config["ai"]["openrouter"]["api_key"] == "openrouter_secret"
assert (
config["ai"]["bedrock"]["aws_access_key_id"] == "bedrock_access_key_id"
)
@@ -41,6 +43,7 @@ def test_mask_secrets() -> None:
assert new_config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER
assert new_config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
assert new_config["ai"]["github"]["api_key"] == SECRET_PLACEHOLDER
+ assert new_config["ai"]["openrouter"]["api_key"] == SECRET_PLACEHOLDER
assert (
new_config["ai"]["bedrock"]["aws_access_key_id"] == SECRET_PLACEHOLDER
)
@@ -54,6 +57,7 @@ def test_mask_secrets() -> None:
assert config["ai"]["anthropic"]["api_key"] == "anthropic_secret"
assert config["ai"]["google"]["api_key"] == "google_secret"
assert config["ai"]["github"]["api_key"] == "github_secret"
+ assert config["ai"]["openrouter"]["api_key"] == "openrouter_secret"
assert (
config["ai"]["bedrock"]["aws_access_key_id"] == "bedrock_access_key_id"
)
@@ -81,6 +85,7 @@ def test_mask_secrets_empty() -> None:
"open_ai": {"model": "davinci"},
"google": {},
"anthropic": {},
+ "openrouter": {},
},
runtime={
"dotenv": [],
@@ -94,6 +99,7 @@ def test_mask_secrets_empty() -> None:
assert "api_key" not in new_config["ai"]["open_ai"]
assert "api_key" not in new_config["ai"]["google"]
assert "api_key" not in new_config["ai"]["anthropic"]
+ assert "api_key" not in new_config["ai"]["openrouter"]
# Ensure the original config is not modified
assert config["ai"]["open_ai"]["model"] == "davinci"
@@ -101,14 +107,17 @@ def test_mask_secrets_empty() -> None:
config["ai"]["open_ai"]["api_key"] = ""
config["ai"]["google"]["api_key"] = ""
config["ai"]["anthropic"]["api_key"] = ""
+ config["ai"]["openrouter"]["api_key"] = ""
new_config = mask_secrets(config)
assert new_config["ai"]["open_ai"]["api_key"] == ""
assert new_config["ai"]["google"]["api_key"] == ""
assert new_config["ai"]["anthropic"]["api_key"] == ""
+ assert new_config["ai"]["openrouter"]["api_key"] == ""
# Ensure the original config is not modified
assert config["ai"]["open_ai"]["api_key"] == ""
assert config["ai"]["google"]["api_key"] == ""
assert config["ai"]["anthropic"]["api_key"] == ""
+ assert config["ai"]["openrouter"]["api_key"] == ""
def test_remove_secret_placeholders() -> None:
@@ -117,16 +126,20 @@ def test_remove_secret_placeholders() -> None:
"open_ai": {"api_key": SECRET_PLACEHOLDER},
"google": {"api_key": SECRET_PLACEHOLDER},
"anthropic": {"api_key": SECRET_PLACEHOLDER},
+ "openrouter": {"api_key": SECRET_PLACEHOLDER},
},
)
assert config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
assert config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
assert config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER
+ assert config["ai"]["openrouter"]["api_key"] == SECRET_PLACEHOLDER
new_config = remove_secret_placeholders(config)
assert "api_key" not in new_config["ai"]["open_ai"]
assert "api_key" not in new_config["ai"]["google"]
assert "api_key" not in new_config["ai"]["anthropic"]
+ assert "api_key" not in new_config["ai"]["openrouter"]
# Ensure the original config is not modified
assert config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
assert config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
assert config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER
+ assert config["ai"]["openrouter"]["api_key"] == SECRET_PLACEHOLDER
diff --git a/tests/_server/ai/test_ai_config.py b/tests/_server/ai/test_ai_config.py
index 7665694f49..17a7df44ec 100644
--- a/tests/_server/ai/test_ai_config.py
+++ b/tests/_server/ai/test_ai_config.py
@@ -168,6 +168,33 @@ class TestAnyProviderConfig:
assert provider_config.api_key == "test-github-key"
assert provider_config.base_url == "https://api.githubcopilot.com/"
+ def test_for_openrouter(self):
+ """Test OpenRouter configuration."""
+ config: AiConfig = {
+ "openrouter": {
+ "api_key": "test-openrouter-key",
+ "base_url": "https://openrouter.ai/api/v1/",
+ }
+ }
+
+ provider_config = AnyProviderConfig.for_openrouter(config)
+
+ assert provider_config.api_key == "test-openrouter-key"
+ assert provider_config.base_url == "https://openrouter.ai/api/v1/"
+
+ def test_for_openrouter_with_fallback_base_url(self):
+ """Test OpenRouter configuration uses fallback base URL when not specified."""
+ config: AiConfig = {
+ "openrouter": {
+ "api_key": "test-openrouter-key",
+ }
+ }
+
+ provider_config = AnyProviderConfig.for_openrouter(config)
+
+ assert provider_config.api_key == "test-openrouter-key"
+ assert provider_config.base_url == "https://openrouter.ai/api/v1/"
+
def test_for_anthropic(self):
"""Test Anthropic configuration."""
config: AiConfig = {
@@ -249,6 +276,17 @@ class TestAnyProviderConfig:
assert provider_config.api_key == "test-github-key"
+ def test_for_model_openrouter(self) -> None:
+ """Test for_model with OpenRouter model."""
+ config: AiConfig = {"openrouter": {"api_key": "test-openrouter-key"}}
+
+ provider_config = AnyProviderConfig.for_model(
+ "openrouter/gpt-4", config
+ )
+
+ assert provider_config.api_key == "test-openrouter-key"
+ assert provider_config.base_url == "https://openrouter.ai/api/v1/"
+
def test_for_model_unknown_defaults_to_ollama(self) -> None:
"""Test for_model with unknown provider defaults to Ollama."""
config: AiConfig = {"ollama": {"api_key": "test-key"}}
@@ -506,6 +544,34 @@ class TestProviderConfigWithFallback:
assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST
assert "GitHub API key not configured" in str(exc_info.value.detail)
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "env-openrouter-token"})
+ def test_for_openrouter_with_fallback_key(self) -> None:
+ """Test OpenRouter config uses fallback key when config is missing api_key."""
+ config: AiConfig = {"openrouter": {}}
+ provider_config = AnyProviderConfig.for_openrouter(config)
+ assert provider_config.api_key == "env-openrouter-token"
+
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "env-openrouter-token"})
+ def test_for_openrouter_config_key_takes_precedence(self) -> None:
+ """Test OpenRouter config key takes precedence over environment variable."""
+ config: AiConfig = {
+ "openrouter": {"api_key": "config-openrouter-token"}
+ }
+ provider_config = AnyProviderConfig.for_openrouter(config)
+ assert provider_config.api_key == "config-openrouter-token"
+
+ @patch.dict(os.environ, {}, clear=True)
+ def test_for_openrouter_no_fallback_available(self) -> None:
+ """Test OpenRouter config fails when no config key and no env var."""
+ config: AiConfig = {"openrouter": {}}
+ with pytest.raises(HTTPException) as exc_info:
+ AnyProviderConfig.for_openrouter(config)
+
+ assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST
+ assert "OpenRouter API key not configured" in str(
+ exc_info.value.detail
+ )
+
class TestGetKey:
"""Tests for _get_key function."""
diff --git a/tests/_server/ai/test_providers.py b/tests/_server/ai/test_providers.py
index cca1ded852..1109f5eb9e 100644
--- a/tests/_server/ai/test_providers.py
+++ b/tests/_server/ai/test_providers.py
@@ -27,6 +27,7 @@ from marimo._server.ai.providers import (
"bedrock",
id="bedrock",
),
+ pytest.param("openrouter/gpt-4", "openrouter", id="openrouter"),
],
)
def test_anyprovider_for_model(model_name: str, provider_name: str) -> None:
@@ -45,6 +46,9 @@ def test_anyprovider_for_model(model_name: str, provider_name: str) -> None:
bedrock={
"profile_name": "aws-profile",
},
+ openrouter={
+ "api_key": "openrouter-key",
+ },
)
config = AnyProviderConfig.for_model(model_name, ai_config)
@@ -68,6 +72,7 @@ def test_anyprovider_for_model(model_name: str, provider_name: str) -> None:
BedrockProvider,
id="bedrock",
),
+ pytest.param("openrouter/gpt-4", OpenAIProvider, id="openrouter"),
],
)
def test_get_completion_provider(model_name: str, provider_type: type) -> None: