This is an automated email from the ASF dual-hosted git repository.

kaxil pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 3f46c5de96f Let `OpenAIHook` build an `AsyncOpenAI` client from sync 
code (#77139)
3f46c5de96f is described below

commit 3f46c5de96f92a6c5ae6a04720c554144f7f1f9f
Author: Kaxil Naik <[email protected]>
AuthorDate: Fri Nov 20 00:43:43 2026 +0000

    Let `OpenAIHook` build an `AsyncOpenAI` client from sync code (#77139)
    
    OpenAIHook.get_conn(async_client=True) returns an AsyncOpenAI configured
    from the connection exactly like get_conn() and aget_conn(), including
    openai_client_kwargs and workload identity. Sync tasks can hand it to a
    library that takes an AsyncOpenAI, such as the OpenAI Agents SDK, instead
    of building one by hand from the connection's password and host.
---
 .../src/airflow/providers/openai/hooks/openai.py      | 16 ++++++++++++++--
 .../openai/tests/unit/openai/hooks/test_openai.py     | 19 +++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/providers/openai/src/airflow/providers/openai/hooks/openai.py 
b/providers/openai/src/airflow/providers/openai/hooks/openai.py
index 753b5f0f885..28f78c93208 100644
--- a/providers/openai/src/airflow/providers/openai/hooks/openai.py
+++ b/providers/openai/src/airflow/providers/openai/hooks/openai.py
@@ -199,7 +199,13 @@ class OpenAIHook(BaseHook):
         """Return an OpenAI connection object."""
         return self.get_conn()
 
-    def get_conn(self) -> OpenAI:
+    @overload
+    def get_conn(self, *, async_client: Literal[False] = False) -> OpenAI: ...
+
+    @overload
+    def get_conn(self, *, async_client: Literal[True]) -> AsyncOpenAI: ...
+
+    def get_conn(self, *, async_client: bool = False) -> OpenAI | AsyncOpenAI:
         """
         Return an OpenAI connection object.
 
@@ -209,9 +215,15 @@ class OpenAIHook(BaseHook):
         * ``"api_key"`` -- use the API key from the connection password (or 
``openai_client_kwargs``).
         * ``"workload_identity"`` -- exchange a short-lived identity token for 
API access using the
           OpenAI client's workload identity support. See 
:meth:`_build_workload_identity`.
+
+        :param async_client: Return an ``AsyncOpenAI`` instead, for sync code 
that passes one to
+            an async library. The connection lookup blocks, so in async code 
use :meth:`aget_conn`.
+            Each call builds a new client. An async client is tied to the 
event loop it first
+            runs in, so use it inside one loop and get a new client for each 
``asyncio.run`` call.
         """
         conn = self.get_connection(self.conn_id)
-        return OpenAI(**self._client_kwargs(conn, conn.extra_dejson))
+        client_class = AsyncOpenAI if async_client else OpenAI
+        return client_class(**self._client_kwargs(conn, conn.extra_dejson))
 
     async def aget_conn(self) -> AsyncOpenAI:
         """
diff --git a/providers/openai/tests/unit/openai/hooks/test_openai.py 
b/providers/openai/tests/unit/openai/hooks/test_openai.py
index 3b8f9d97d59..56f1aab8feb 100644
--- a/providers/openai/tests/unit/openai/hooks/test_openai.py
+++ b/providers/openai/tests/unit/openai/hooks/test_openai.py
@@ -844,6 +844,25 @@ def test_get_conn_with_openai_client_kwargs(mock_client):
     )
 
 
+def 
test_get_conn_async_client_builds_a_real_async_openai(create_connection_without_db):
+    """The kwargs reach the real ``AsyncOpenAI`` constructor, which validates 
them."""
+    create_connection_without_db(
+        Connection(
+            conn_id="openai_async_client",
+            conn_type="openai",
+            password="api_key",
+            extra={"openai_client_kwargs": {"base_url": 
"https://gateway.example/v1";, "timeout": 7}},
+        )
+    )
+
+    client = 
OpenAIHook(conn_id="openai_async_client").get_conn(async_client=True)
+
+    assert isinstance(client, AsyncOpenAI)
+    assert str(client.base_url) == "https://gateway.example/v1/";
+    assert client.timeout == 7
+    assert client.api_key == "api_key"
+
+
 def _make_workload_identity_conn(conn_id, extra):
     conn = Connection(
         conn_id=conn_id,

Reply via email to