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.

improvement: add support for extra headers in OpenAI configuration (#6134)

Matthew Pillari16c626a41f67
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index e0101db6c3..87017e6f5b 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -300,6 +300,7 @@ class OpenAiConfig(TypedDict, total=False):
- `ssl_verify` : Boolean argument for httpx passed to open ai client. httpx defaults to true, but some use cases to let users override to False in some testing scenarios
- `ca_bundle_path`: custom ca bundle to be used for verifying SSL certificates. Used to create custom SSL context for httpx client
- `client_pem` : custom path of a client .pem cert used for verifying identity of client server
+ - `extra_headers`: extra headers to be passed to the OpenAI client
"""
api_key: str
@@ -307,6 +308,7 @@ class OpenAiConfig(TypedDict, total=False):
ssl_verify: NotRequired[bool]
ca_bundle_path: NotRequired[str]
client_pem: NotRequired[str]
+ extra_headers: NotRequired[dict[str, str]]
# @deprecated: use `ai.models.chat_model` instead
model: NotRequired[str]
diff --git a/marimo/_server/ai/config.py b/marimo/_server/ai/config.py
index b01a9ef4f0..4dfb91ed58 100644
--- a/marimo/_server/ai/config.py
+++ b/marimo/_server/ai/config.py
@@ -32,6 +32,7 @@ class AnyProviderConfig:
ssl_verify: Optional[bool] = None
ca_bundle_path: Optional[str] = None
client_pem: Optional[str] = None
+ extra_headers: Optional[dict[str, str]] = None
tools: Optional[list[Tool]] = None
def __post_init__(self) -> None:
@@ -103,6 +104,7 @@ class AnyProviderConfig:
"ssl_verify": ai_config.get("ssl_verify", True),
"ca_bundle_path": ai_config.get("ca_bundle_path", None),
"client_pem": ai_config.get("client_pem", None),
+ "extra_headers": ai_config.get("extra_headers", None),
"tools": _get_tools(config.get("mode", "manual")),
}
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index d3b3729284..15d89292bf 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -345,6 +345,7 @@ class OpenAIProvider(
ssl_verify: bool = (
config.ssl_verify if config.ssl_verify is not None else True
)
+ extra_headers: Optional[dict[str, str]] = config.extra_headers
ca_bundle_path: Optional[str] = config.ca_bundle_path
client_pem: Optional[str] = config.client_pem
@@ -388,9 +389,10 @@ class OpenAIProvider(
client = httpx.AsyncClient(verify=False)
# if client is created, either with a custom context or with verify=False, use it as the http_client object in `AsyncOpenAI`
+ extra_headers = extra_headers or {}
if client:
return AsyncOpenAI(
- default_headers={"api-key": key},
+ default_headers={"api-key": key, **extra_headers},
api_key=key,
base_url=base_url,
http_client=client,
@@ -398,7 +400,7 @@ class OpenAIProvider(
# if not, return bog standard AsyncOpenAI object
return AsyncOpenAI(
- default_headers={"api-key": key},
+ default_headers={"api-key": key, **extra_headers},
api_key=key,
base_url=base_url,
)
diff --git a/marimo/_server/codes.py b/marimo/_server/codes.py
index c08af177bd..41acecfcbb 100644
--- a/marimo/_server/codes.py
+++ b/marimo/_server/codes.py
@@ -1,3 +1,4 @@
+# Copyright 2025 Marimo. All rights reserved.
from enum import IntEnum
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 16e2b512ba..08b92fc81f 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -2052,6 +2052,10 @@ components:
type: string
client_pem:
type: string
+ extra_headers:
+ additionalProperties:
+ type: string
+ type: object
model:
type: string
ssl_verify:
@@ -2825,7 +2829,7 @@ components:
type: object
info:
title: marimo API
- version: 0.14.17
+ version: 0.15.0
openapi: 3.1.0
paths:
/@file/{filename_and_length}:
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 24867ffe14..fd7a11c533 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -3422,6 +3422,9 @@ export interface components {
base_url?: string;
ca_bundle_path?: string;
client_pem?: string;
+ extra_headers?: {
+ [key: string]: string;
+ };
model?: string;
ssl_verify?: boolean;
};
diff --git a/tests/_server/ai/test_ai_config.py b/tests/_server/ai/test_ai_config.py
index 0960ba8670..47c1195768 100644
--- a/tests/_server/ai/test_ai_config.py
+++ b/tests/_server/ai/test_ai_config.py
@@ -47,6 +47,7 @@ class TestAnyProviderConfig:
assert provider_config.ssl_verify is True
assert provider_config.ca_bundle_path is None
assert provider_config.client_pem is None
+ assert provider_config.extra_headers is None
def test_for_openai_with_base_url(self):
"""Test OpenAI configuration with custom base URL."""
@@ -57,6 +58,7 @@ class TestAnyProviderConfig:
"ssl_verify": False,
"ca_bundle_path": "/path/to/ca.pem",
"client_pem": "/path/to/client.pem",
+ "extra_headers": {"test-header": "test-value"},
}
}
@@ -67,6 +69,7 @@ class TestAnyProviderConfig:
assert provider_config.ssl_verify is False
assert provider_config.ca_bundle_path == "/path/to/ca.pem"
assert provider_config.client_pem == "/path/to/client.pem"
+ assert provider_config.extra_headers == {"test-header": "test-value"}
def test_for_azure(self):
"""Test Azure OpenAI configuration."""