Skip to content

Commit b129558

Browse files
committed
throw error when use_ai_gateway=True but AI Gateway V2 is not available for this workspace, remove logs
1 parent f5ef30b commit b129558

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

integrations/openai/src/databricks_openai/utils/clients.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import os
32
from typing import Any, Generator
43
from urllib.parse import urlparse
@@ -11,8 +10,6 @@
1110
from openai.resources.responses import AsyncResponses, Responses
1211
from typing_extensions import override
1312

14-
logger = logging.getLogger(__name__)
15-
1613
# Prefix for routing requests to Databricks Apps
1714
_APPS_ENDPOINT_PREFIX = "apps/"
1815
# Domain pattern indicating a Databricks App URL
@@ -117,28 +114,19 @@ def _get_ai_gateway_base_url(
117114
Returns None if gateway is not available.
118115
"""
119116
try:
120-
url = f"{host}/api/ai-gateway/v2/endpoints"
121-
logger.debug("Probing AI Gateway V2 at %s", url)
122-
response = http_client.get(url)
117+
response = http_client.get(f"{host}/api/ai-gateway/v2/endpoints")
123118
if response.status_code != 200:
124-
logger.debug("AI Gateway V2 probe returned status %s", response.status_code)
125119
return None
126120
data = response.json()
127121
endpoints = data.get("endpoints", [])
128122
if not endpoints:
129-
logger.debug("AI Gateway V2 returned no endpoints")
130123
return None
131-
# Extract gateway base URL from the first endpoint's ai_gateway_url
132124
gateway_url = endpoints[0].get("ai_gateway_url")
133125
if not gateway_url:
134-
logger.debug("AI Gateway V2 endpoint missing ai_gateway_url field")
135126
return None
136127
parsed = urlparse(gateway_url)
137-
base_url = f"{parsed.scheme}://{parsed.netloc}/mlflow/v1"
138-
logger.info("AI Gateway V2 detected, using base URL: %s", base_url)
139-
return base_url
128+
return f"{parsed.scheme}://{parsed.netloc}/mlflow/v1"
140129
except Exception:
141-
logger.debug("AI Gateway V2 check failed, falling back to serving endpoints", exc_info=True)
142130
return None
143131

144132

@@ -159,6 +147,10 @@ def _resolve_base_url(
159147
gateway_url = _get_ai_gateway_base_url(http_client, workspace_client.config.host)
160148
if gateway_url:
161149
return gateway_url
150+
raise ValueError(
151+
"use_ai_gateway=True but AI Gateway V2 is not available for this workspace. "
152+
"Set use_ai_gateway=False to use serving endpoints directly."
153+
)
162154

163155
# Fallback to using serving endpoints
164156
return f"{workspace_client.config.host}/serving-endpoints"

integrations/openai/tests/unit_tests/test_clients.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -776,18 +776,16 @@ def test_gateway_available_uses_gateway_url(self, client_cls_name, mock_workspac
776776
assert "12345.ai-gateway.cloud.databricks.com" in str(client.base_url)
777777

778778
@pytest.mark.parametrize("client_cls_name", ["DatabricksOpenAI", "AsyncDatabricksOpenAI"])
779-
def test_gateway_unavailable_falls_back_to_serving(
780-
self, client_cls_name, mock_workspace_client
781-
):
779+
def test_gateway_unavailable_raises_error(self, client_cls_name, mock_workspace_client):
782780
client_cls = (
783781
DatabricksOpenAI if client_cls_name == "DatabricksOpenAI" else AsyncDatabricksOpenAI
784782
)
785783
with patch(
786784
"databricks_openai.utils.clients._get_ai_gateway_base_url",
787785
return_value=None,
788786
):
789-
client = client_cls(workspace_client=mock_workspace_client, use_ai_gateway=True)
790-
assert "/serving-endpoints/" in str(client.base_url)
787+
with pytest.raises(ValueError, match="use_ai_gateway=True but AI Gateway V2"):
788+
client_cls(workspace_client=mock_workspace_client, use_ai_gateway=True)
791789

792790
@pytest.mark.parametrize("client_cls_name", ["DatabricksOpenAI", "AsyncDatabricksOpenAI"])
793791
def test_gateway_disabled_no_api_call(self, client_cls_name, mock_workspace_client):
@@ -815,15 +813,3 @@ def test_explicit_base_url_skips_gateway_check(self, client_cls_name, mock_works
815813
)
816814
mock_gateway.assert_not_called()
817815
assert "custom.example.com" in str(client.base_url)
818-
819-
@pytest.mark.parametrize("client_cls_name", ["DatabricksOpenAI", "AsyncDatabricksOpenAI"])
820-
def test_gateway_detection_failure_falls_back(self, client_cls_name, mock_workspace_client):
821-
client_cls = (
822-
DatabricksOpenAI if client_cls_name == "DatabricksOpenAI" else AsyncDatabricksOpenAI
823-
)
824-
with patch(
825-
"databricks_openai.utils.clients._get_ai_gateway_base_url",
826-
return_value=None,
827-
):
828-
client = client_cls(workspace_client=mock_workspace_client, use_ai_gateway=True)
829-
assert "/serving-endpoints/" in str(client.base_url)

0 commit comments

Comments
 (0)