kaxil opened a new pull request, #77139:
URL: https://github.com/apache/airflow/pull/77139

   `OpenAIHook` builds an `AsyncOpenAI` only through 
[`aget_conn()`](https://github.com/apache/airflow/blob/d099acc6a4061d1f364785d3b6224c99c5cea704/providers/openai/src/airflow/providers/openai/hooks/openai.py#L216-L228),
 which is a coroutine. Sync code that needs one has no way to get it from the 
hook. The OpenAI Agents SDK's models take an `AsyncOpenAI`, and common.ai's 
`run_durably_sync` runs an agent from a sync task. So a sync task builds the 
client by hand from the connection's password and host, which drops the 
connection's `openai_client_kwargs` and workload identity. A connection that 
sets `base_url` only in `openai_client_kwargs` then sends its key to 
api.openai.com. Async tasks need Airflow 3.2.1+, and this provider supports 
2.11+, so on older Airflow a sync task is the only option.
   
   `get_conn(async_client=True)` returns an `AsyncOpenAI` built from the same 
[`_client_kwargs`](https://github.com/apache/airflow/blob/d099acc6a4061d1f364785d3b6224c99c5cea704/providers/openai/src/airflow/providers/openai/hooks/openai.py#L230)
 as `get_conn()` and `aget_conn()`:
   
   ```python
   @task
   def answer() -> str:
       client = OpenAIHook("openai_default").get_conn(async_client=True)
       agent = Agent(name="geo", 
model=OpenAIChatCompletionsModel(model="gpt-5.4-mini", openai_client=client), 
tools=[...])
       return run_durably_sync(agent, "What is the capital of 
France?").final_output
   ```
   
   ## Design rationale
   
   **A flag on `get_conn`, not a new method.** The names a separate sync method 
would take are already coroutines in other provider hooks: 
[`get_async_client`](https://github.com/apache/airflow/blob/d099acc6a4061d1f364785d3b6224c99c5cea704/providers/slack/src/airflow/providers/slack/hooks/slack.py#L157)
 in Slack and Google GenAI, and 
[`get_async_conn`](https://github.com/apache/airflow/blob/d099acc6a4061d1f364785d3b6224c99c5cea704/providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py#L469)
 in Anthropic and AWS. A sync method under one of those names would contradict 
that convention. `get_conn` is the hook's client builder, and the `Literal` 
overloads keep the return type exact: `get_conn()` is still typed `OpenAI`, and 
`get_conn(async_client=True)` is typed `AsyncOpenAI`.
   
   **This is not AWS's deprecated `async_conn`.** 
[`AwsBaseHook.async_conn`](https://github.com/apache/airflow/blob/d099acc6a4061d1f364785d3b6224c99c5cea704/providers/amazon/src/airflow/providers/amazon/aws/hooks/base_aws.py#L796-L808)
 also built an async client with a sync connection lookup. It was deprecated 
because it was used inside async code, where it blocked the event loop. This 
flag is for code with no running loop, and its docstring sends async callers to 
`aget_conn`.
   
   **Why `_client_kwargs` stays private.** Exposing it would let callers build 
either client themselves. But it would freeze an internal dict as API, 
including the workload-identity provider object.
   
   ## Validation
   
   I ran this end to end on Airflow main (LocalExecutor, Postgres) against a 
real model, `gpt-5.4-mini` behind an OpenAI-compatible gateway. The connection 
held the gateway token in its password and `base_url` only in 
`openai_client_kwargs`, with no host. A sync `@task` used the code above, with 
a function tool. The tool ran, and the task returned "The capital of France is 
Paris." On the same connection, the hand-built 
`AsyncOpenAI(api_key=conn.password, base_url=conn.host)` got `401 
AuthenticationError`, because the key went to api.openai.com (#77071 has that 
run). I did not run this on Airflow 2.11. The change has no version-specific 
code.
   
   ## Follow-ups
   
   Once this is released, the common.ai OpenAI Agents SDK guide can replace its 
"older Airflow" fallback (#77071) with a sync example that uses this.
   


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