|
| 1 | +""" |
| 2 | +OpenAI Agents SDK Client Configuration |
| 3 | +
|
| 4 | +Configures custom OpenAI clients specifically for the OpenAI Agents SDK. |
| 5 | +This affects ONLY the OpenAI Agents SDK integration (Agent and Runner). |
| 6 | +
|
| 7 | +For LiteLLM integration, use separate LiteLLM configuration methods. |
| 8 | +""" |
| 9 | + |
| 10 | +from typing import Union |
| 11 | +from openai import AsyncOpenAI, AsyncAzureOpenAI |
| 12 | +from agents import set_default_openai_client |
| 13 | +from agentex.lib.utils.logging import make_logger |
| 14 | + |
| 15 | +logger = make_logger(__name__) |
| 16 | + |
| 17 | +# Type alias for supported async OpenAI clients |
| 18 | +AsyncOpenAIClient = Union[AsyncOpenAI, AsyncAzureOpenAI] |
| 19 | + |
| 20 | +_client_initialized = False |
| 21 | + |
| 22 | + |
| 23 | +def initialize_openai_agents_client(client: AsyncOpenAIClient) -> None: |
| 24 | + """ |
| 25 | + Initialize custom OpenAI client for OpenAI Agents SDK operations. |
| 26 | +
|
| 27 | + ⚠️ IMPORTANT: This ONLY affects the OpenAI Agents SDK integration |
| 28 | + (Agent and Runner classes). It does NOT affect: |
| 29 | + - LiteLLM integration (use LiteLLM configuration separately) |
| 30 | + - SGP integration |
| 31 | + - Direct OpenAI API calls |
| 32 | +
|
| 33 | + This should be called ONCE at application or worker startup, before any |
| 34 | + agents are created using the OpenAI Agents SDK. |
| 35 | +
|
| 36 | + Args: |
| 37 | + client: Pre-configured async OpenAI client (AsyncOpenAI or AsyncAzureOpenAI) |
| 38 | +
|
| 39 | + Raises: |
| 40 | + TypeError: If a non-async client is passed (will be caught by type checker) |
| 41 | +
|
| 42 | + Examples: |
| 43 | + # Custom endpoint (e.g., LiteLLM proxy for cost tracking): |
| 44 | + from openai import AsyncOpenAI |
| 45 | + from agentex.lib.adk.providers._modules.openai_agents_config import ( |
| 46 | + initialize_openai_agents_client |
| 47 | + ) |
| 48 | +
|
| 49 | + client = AsyncOpenAI( |
| 50 | + base_url="https://your-proxy.com/v1", |
| 51 | + api_key=os.getenv("CUSTOM_API_KEY") |
| 52 | + ) |
| 53 | + initialize_openai_agents_client(client) |
| 54 | +
|
| 55 | + # Azure OpenAI: |
| 56 | + from openai import AsyncAzureOpenAI |
| 57 | +
|
| 58 | + client = AsyncAzureOpenAI( |
| 59 | + azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), |
| 60 | + api_key=os.getenv("AZURE_OPENAI_KEY"), |
| 61 | + api_version="2024-02-01" |
| 62 | + ) |
| 63 | + initialize_openai_agents_client(client) |
| 64 | +
|
| 65 | + # For Temporal workers, call this in worker startup: |
| 66 | + # run_worker.py: |
| 67 | + # initialize_openai_agents_client(client) |
| 68 | + # # Then start worker... |
| 69 | +
|
| 70 | + Note: |
| 71 | + If not called, the OpenAI Agents SDK uses default OpenAI configuration |
| 72 | + via the OPENAI_API_KEY environment variable. |
| 73 | + """ |
| 74 | + global _client_initialized |
| 75 | + |
| 76 | + if _client_initialized: |
| 77 | + logger.warning( |
| 78 | + "OpenAI Agents SDK client already initialized. " |
| 79 | + "Ignoring subsequent initialization. " |
| 80 | + "This may indicate multiple workers or initialization calls." |
| 81 | + ) |
| 82 | + return |
| 83 | + |
| 84 | + # Runtime validation for async client |
| 85 | + if not isinstance(client, (AsyncOpenAI, AsyncAzureOpenAI)): |
| 86 | + raise TypeError( |
| 87 | + f"Client must be AsyncOpenAI or AsyncAzureOpenAI, got {type(client).__name__}. " |
| 88 | + f"The OpenAI Agents SDK requires async clients. " |
| 89 | + f"Use AsyncOpenAI instead of OpenAI." |
| 90 | + ) |
| 91 | + |
| 92 | + set_default_openai_client(client) |
| 93 | + logger.info(f"OpenAI Agents SDK client configured: {type(client).__name__}") |
| 94 | + |
| 95 | + _client_initialized = True |
| 96 | + |
| 97 | + |
| 98 | +def is_openai_agents_client_initialized() -> bool: |
| 99 | + """ |
| 100 | + Check if custom OpenAI Agents SDK client has been initialized. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + bool: True if initialize_openai_agents_client() has been called |
| 104 | + """ |
| 105 | + return _client_initialized |
| 106 | + |
| 107 | + |
| 108 | +def reset_openai_agents_client_initialization() -> None: |
| 109 | + """ |
| 110 | + Reset initialization flag (primarily for testing). |
| 111 | +
|
| 112 | + Warning: This does NOT reset the actual client in the agents library, |
| 113 | + only the initialization tracking flag in this module. |
| 114 | + """ |
| 115 | + global _client_initialized |
| 116 | + _client_initialized = False |
0 commit comments