Lee-W commented on code in PR #76797:
URL: https://github.com/apache/airflow/pull/76797#discussion_r4627129146


##########
providers/databricks/src/airflow/providers/databricks/hooks/model_serving.py:
##########
@@ -0,0 +1,253 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Expose Databricks Model Serving's OpenAI-compatible chat endpoint as a 
pydantic-ai model hook."""
+
+from __future__ import annotations
+
+import asyncio
+import concurrent.futures
+import contextvars
+import functools
+import inspect
+from typing import TYPE_CHECKING, Any
+from urllib.parse import urlsplit
+
+from airflow.providers.common.compat.sdk import 
AirflowOptionalProviderFeatureException
+from airflow.providers.databricks.hooks.databricks_base import 
BaseDatabricksHook
+
+_COMMON_AI_REQUIREMENT_MESSAGE = (
+    "This feature requires apache-airflow-providers-common-ai>=1.0.0 (a 
PydanticAIHook with "
+    "'fallback_conn_ids'). "
+    "Install with apache-airflow-providers-databricks[common.ai]."
+)
+
+try:
+    import httpx2
+
+    from airflow.providers.common.ai.hooks.pydantic_ai import PydanticAIHook
+except ImportError:
+    raise 
AirflowOptionalProviderFeatureException(_COMMON_AI_REQUIREMENT_MESSAGE)
+
+if "fallback_conn_ids" not in 
inspect.signature(PydanticAIHook.__init__).parameters:
+    raise 
AirflowOptionalProviderFeatureException(_COMMON_AI_REQUIREMENT_MESSAGE)
+
+if TYPE_CHECKING:
+    from collections.abc import AsyncGenerator, Generator
+
+    from httpx2 import Request, Response
+
+# OpenAIProvider rejects an empty api_key, but _DatabricksServingAuth 
overwrites Authorization on
+# every request, so this value is never sent.
+_UNUSED_TOKEN_PLACEHOLDER = "unused-overridden-per-request"
+
+# Token refreshes run here, never on asyncio's default executor: ``asend()`` 
reads its reply on that
+# executor while holding the comms lock, so blocking lookups that fill it 
leave nothing to run the
+# read and the task hangs (common.ai>=1.1.0 guards against the same thing with 
``_executor`` in
+# ``hooks/pydantic_ai.py``). Some ``_get_token`` paths do such lookups at 
request time (AWS
+# federation resolves a second Airflow connection through ``StsHook``). Kept 
local: common.ai's
+# ``_run_off_loop`` only exists from 1.1.0.
+_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1, 
thread_name_prefix="databricks-serving-auth")
+
+
+def _is_valid_host(host: str | None) -> bool:
+    """Return whether ``host`` is a bare ``hostname[:port]`` as left by 
``BaseDatabricksHook._parse_host``."""
+    if not host or host.endswith(":") or any(char.isspace() for char in host):
+        return False
+    parsed = urlsplit(f"//{host}")
+    if not parsed.hostname or parsed.netloc != host or parsed.username is not 
None:
+        return False
+    try:
+        parsed.port
+    except ValueError:
+        return False
+    return True
+
+
+class _DatabricksServingAuth(httpx2.Auth):
+    """
+    Refresh the ``Authorization`` header on every request from a Databricks 
hook.
+
+    ``_get_token()`` may block: the OAuth service principal, Azure AD and 
federation paths make
+    HTTP or SDK calls, with retries, to fetch an expiring token. 
``async_auth_flow`` is overridden
+    to run the refresh on a worker thread instead of the httpx2 default of 
driving the sync
+    ``auth_flow`` inline on the event loop. ``auth_flow`` itself is kept for 
sync ``httpx2.Client``
+    callers, which have no event loop to block.
+    """
+
+    def __init__(self, databricks_hook: BaseDatabricksHook) -> None:
+        self._databricks_hook = databricks_hook
+
+    def _get_auth_header(self) -> str:
+        return f"Bearer {self._databricks_hook._get_token(raise_error=True)}"

Review Comment:
   A token fetch that exhausts its retries now raises `httpx2.ConnectError`, so 
the chain fails over; missing or rejected credentials keep their exception and 
fail the task. There is a test running a real `FallbackModel` chain for both 
cases.
   
   I left the older-openai wrapping alone: `pydantic-ai-slim[openai]>=2.33` 
already asks for openai>=3, so the 2.47 floor only matters to an install that 
overrides it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to