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: support for Azure OpenAI with AI Assistant (#3120)

matulef97a52e124de6
diff --git a/marimo/_ai/llm.py b/marimo/_ai/llm.py
index f9afa5dead..6e6e686aab 100644
--- a/marimo/_ai/llm.py
+++ b/marimo/_ai/llm.py
@@ -104,15 +104,37 @@ class openai(ChatModel):
DependencyManager.openai.require(
"chat model requires openai. `pip install openai`"
)
- from openai import OpenAI # type: ignore[import-not-found]
+ from urllib.parse import parse_qs, urlparse
+
+ from openai import ( # type: ignore[import-not-found]
+ AzureOpenAI,
+ OpenAI,
+ )
from openai.types.chat import ( # type: ignore[import-not-found]
ChatCompletionMessageParam,
)
- client = OpenAI(
- api_key=self._require_api_key,
- base_url=self.base_url,
- )
+ # Azure OpenAI clients are instantiated slightly differently
+ # To check if we're using Azure, we check the base_url for the format
+ # https://[subdomain].openai.azure.com/openai/deployments/[model]/chat/completions?api-version=[api_version]
+ parsed_url = urlparse(self.base_url)
+ if parsed_url.hostname and cast(str, parsed_url.hostname).endswith(
+ "openai.azure.com"
+ ):
+ self.model = cast(str, parsed_url.path).split("/")[3]
+ api_version = parse_qs(cast(str, parsed_url.query))["api-version"][
+ 0
+ ]
+ client: AzureOpenAI | OpenAI = AzureOpenAI(
+ api_key=self._require_api_key,
+ api_version=api_version,
+ azure_endpoint=f"{cast(str,parsed_url.scheme)}://{cast(str,parsed_url.hostname)}",
+ )
+ else:
+ client = OpenAI(
+ api_key=self._require_api_key,
+ base_url=self.base_url,
+ )
openai_messages = convert_to_openai_messages(
[ChatMessage(role="system", content=self.system_message)]
diff --git a/marimo/_server/api/endpoints/ai.py b/marimo/_server/api/endpoints/ai.py
index ea40ce31dd..d8f0b22c62 100644
--- a/marimo/_server/api/endpoints/ai.py
+++ b/marimo/_server/api/endpoints/ai.py
@@ -56,7 +56,12 @@ router = APIRouter()
def get_openai_client(config: MarimoConfig) -> "OpenAI":
try:
- from openai import OpenAI # type: ignore[import-not-found]
+ from urllib.parse import parse_qs, urlparse
+
+ from openai import ( # type: ignore[import-not-found]
+ AzureOpenAI,
+ OpenAI,
+ )
except ImportError:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
@@ -89,11 +94,27 @@ def get_openai_client(config: MarimoConfig) -> "OpenAI":
if not base_url:
base_url = None
- return OpenAI(
- default_headers={"api-key": key},
- api_key=key,
- base_url=base_url,
- )
+ # Azure OpenAI clients are instantiated slightly differently
+ # To check if we're using Azure, we check the base_url for the format
+ # https://[subdomain].openai.azure.com/openai/deployments/[model]/chat/completions?api-version=[api_version]
+ parsed_url = urlparse(base_url)
+ if parsed_url.hostname and cast(str, parsed_url.hostname).endswith(
+ "openai.azure.com"
+ ):
+ deployment_model = cast(str, parsed_url.path).split("/")[3]
+ api_version = parse_qs(cast(str, parsed_url.query))["api-version"][0]
+ return AzureOpenAI(
+ api_key=key,
+ api_version=api_version,
+ azure_deployment=deployment_model,
+ azure_endpoint=f"{cast(str,parsed_url.scheme)}://{cast(str,parsed_url.hostname)}",
+ )
+ else:
+ return OpenAI(
+ default_headers={"api-key": key},
+ api_key=key,
+ base_url=base_url,
+ )
def get_anthropic_client(config: MarimoConfig) -> "Client":
@@ -146,7 +167,7 @@ def get_content(
| ChatCompletionChunk
| GenerateContentResponse,
) -> str | None:
- if hasattr(response, "choices"):
+ if hasattr(response, "choices") and response.choices:
return response.choices[0].delta.content # type: ignore
if hasattr(response, "text"):