Skip to content

Support AI Gateway URLs in DatabricksOpenAI#373

Merged
jennsun merged 7 commits intomainfrom
ai-gateway-databricks-openai
Mar 20, 2026
Merged

Support AI Gateway URLs in DatabricksOpenAI#373
jennsun merged 7 commits intomainfrom
ai-gateway-databricks-openai

Conversation

@jennsun
Copy link
Copy Markdown
Contributor

@jennsun jennsun commented Mar 14, 2026

test notebooks:

current databricks-openai package not detecting ai gateway and routing through serving endpoints on an ai gateway enabled workspace: https://dogfood.staging.databricks.com/editor/notebooks/713384057499601?o=6051921418418893

updated databricks-openai package (This branch) to auto detect ai gateway and route request through that endpoint on an ai gateway enabled workspace: https://dogfood.staging.databricks.com/editor/notebooks/713384057499604?o=6051921418418893

testing on non-gateway enabled workspace, changes enabled but queries model serving endpoint instead of ai gateway: https://eng-ml-agent-platform.staging.cloud.databricks.com/editor/notebooks/1312429564150317?o=2850744067564480

if ai gateway is not enabled and we set use_ai_gateway=true
image

@jennsun jennsun changed the title add ai gateway support for databricksopenai client Support AI Gateway URLs in DatabricksOpenAI Mar 16, 2026
from typing_extensions import override

logger = logging.getLogger(__name__)

Copy link
Copy Markdown
Contributor Author

@jennsun jennsun Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging was to get more observability during notebook debugging (see pr description) but I can remove logging instances before merging

logger.debug("AI Gateway V2 returned no endpoints")
return None
# Extract gateway base URL from the first endpoint's ai_gateway_url
gateway_url = endpoints[0].get("ai_gateway_url")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using direct http request to list ai gateway endpoints to check ai gateway status + get url so we don't assume hardcoded gateway url value here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for context example response to calling https://dogfood.staging.databricks.com/ajax-api/ai-gateway/v2/endpoints looks like:

 "endpoints": [
        {
            "name": "databricks-bge-large-en",
            "id": "41eaa162-d05a-3401-bf27-2a040480e808",
            "created_by": "Databricks",
            "config": {
                "destinations": [
                    {
                        "name": "system.ai.bge_large_en_v1_5",
                        "type": "PAY_PER_TOKEN_FOUNDATION_MODEL",
                        "traffic_percentage": 100,
                        "id": "01a22d18007038459db772d12ef43b3d"
                    }
                ],
                "usage_tracking": {
                    "enabled": true
                }
            },
            "created_timestamp": "1699610000000",
            "last_updated_timestamp": "1699610000000",
            "ai_gateway_url": "https://6051921418418893.ai-gateway.staging.cloud.databricks.com"
        },

# Default: Serving endpoints
target_base_url = f"{workspace_client.config.host}/serving-endpoints"
# Use a sync http client for the gateway probe (init is synchronous)
sync_http_client = _get_authorized_http_client(workspace_client)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using a sync http client here to make endpoint calls to gateway since it does a blocking http_client.get call in init

@jennsun jennsun marked this pull request as ready for review March 16, 2026 21:06
@jennsun jennsun requested a review from aravind-segu March 16, 2026 21:09
self,
workspace_client: WorkspaceClient | None = None,
base_url: str | None = None,
use_ai_gateway: bool = True,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should set this to be False so existing queries to model serving endpoint agents don't break, see testing in notebook: https://dogfood.staging.databricks.com/editor/notebooks/713384057499604?o=6051921418418893#command/5345838133151630

Copy link
Copy Markdown
Contributor

@aravind-segu aravind-segu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick comment, and will approve after the logging messages are removed

if use_ai_gateway:
gateway_url = _get_ai_gateway_base_url(http_client, workspace_client.config.host)
if gateway_url:
return gateway_url
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Instead of falling through, lets throw an error here. If the user is specifically asing for use_ai_gateway and its not enabled or something, we should not silently swallow it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch - i'll raise a value error here

Copy link
Copy Markdown
Contributor

@aravind-segu aravind-segu Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we say:

"Please ensure AI Gateway V2 is enabled for the workspace when use_ai_gateway is set to True

Copy link
Copy Markdown
Contributor

@aravind-segu aravind-segu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit but lgtm

@jennsun jennsun merged commit a6039c0 into main Mar 20, 2026
45 of 46 checks passed
@jennsun jennsun deleted the ai-gateway-databricks-openai branch March 20, 2026 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants