kaxil commented on code in PR #73932:
URL: https://github.com/apache/airflow/pull/73932#discussion_r4156674695


##########
providers/snowflake/src/airflow/providers/snowflake/hooks/snowflake_cortex_model.py:
##########
@@ -0,0 +1,182 @@
+# 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 Snowflake Cortex's OpenAI-compatible chat endpoint as a pydantic-ai 
model hook."""
+
+from __future__ import annotations
+
+import asyncio
+from typing import TYPE_CHECKING, Any
+
+from airflow.providers.common.compat.sdk import 
AirflowOptionalProviderFeatureException
+from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook
+from airflow.providers.snowflake.utils.rest_auth import 
SnowflakeRestTokenProvider, get_cortex_base_url
+
+try:
+    import httpx2
+    from pydantic_ai.providers import snowflake as 
_pydantic_ai_snowflake_provider  # noqa: F401
+
+    from airflow.providers.common.ai.hooks.pydantic_ai import PydanticAIHook
+except ImportError:
+    raise AirflowOptionalProviderFeatureException(
+        "This feature requires the 'common.ai' provider, in a version that 
ships a Snowflake "
+        "pydantic-ai provider. Install with 
apache-airflow-providers-snowflake[common.ai]."
+    )
+
+if TYPE_CHECKING:
+    from httpx2 import Request
+
+CORTEX_CHAT_COMPLETIONS_PATH = "/api/v2/cortex/v1"
+
+
+class _SnowflakeCortexAuth(httpx2.Auth):
+    """
+    Refresh the ``Authorization`` header on every request from a shared token 
provider.
+
+    ``build_auth_headers()`` may block: it can call ``requests.post`` with 
retries for an
+    expiring OAuth token, or -- for ``azure_conn_id`` -- resolve an Airflow 
connection and fetch
+    an Azure token on every call. Resolving a connection synchronously from 
the event-loop
+    thread while an async send is in flight raises ``DeadlockImminentError`` 
(see
+    ``task-sdk`` ``execution_time/comms.py``), so ``async_auth_flow`` is 
overridden to run the
+    refresh in a worker thread instead of the httpx2 default of driving the 
sync ``auth_flow``
+    inline on the loop. ``auth_flow`` itself is kept for sync 
``httpx2.Client`` callers, which
+    have no event loop to block.
+    """
+
+    def __init__(self, token_provider: SnowflakeRestTokenProvider) -> None:
+        self._token_provider = token_provider
+
+    def auth_flow(self, request: Request) -> Any:
+        request.headers.update(self._token_provider.build_auth_headers())
+        yield request
+
+    async def async_auth_flow(self, request: Request) -> Any:
+        request.headers.update(await 
asyncio.to_thread(self._token_provider.build_auth_headers))
+        yield request
+
+
+class PydanticAISnowflakeHook(PydanticAIHook):

Review Comment:
   Same point @eladkal  raised on `snowflake_cortex_agent.py` in #73815: this 
module repeats the `snowflake_` prefix that AIP-21 removed inside provider 
packages. Nothing imports it yet, so `hooks/cortex_model.py` (or similar) is 
free now and needs a deprecation cycle after release.



-- 
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