@@ -137,10 +137,7 @@ def _get_moderation_client() -> AsyncOpenAI:
137137 return AsyncOpenAI ()
138138
139139
140- async def _call_moderation_api_async (
141- client : AsyncOpenAI | AsyncAzureOpenAI ,
142- data : str , # type: ignore
143- ) -> Any :
140+ async def _call_moderation_api_async (client : Any , data : str ) -> Any :
144141 """Call the OpenAI moderation API asynchronously.
145142
146143 Args:
@@ -156,7 +153,7 @@ async def _call_moderation_api_async(
156153 )
157154
158155
159- def _call_moderation_api_sync (client : OpenAI | AzureOpenAI , data : str ) -> Any : # type: ignore
156+ def _call_moderation_api_sync (client : Any , data : str ) -> Any :
160157 """Call the OpenAI moderation API synchronously.
161158
162159 Args:
@@ -191,30 +188,30 @@ async def moderation(
191188 Returns:
192189 GuardrailResult: Indicates if tripwire was triggered, and details of flagged categories.
193190 """
194- # Try the context client first, fall back if moderation endpoint doesn't exist
191+ # Try context client first (if provided) , fall back on 404
195192 client = getattr (ctx , "guardrail_llm" , None ) if ctx is not None else None
196193
197194 if client is not None :
198- # Determine if client is async or sync and call appropriately
199- is_async_client = isinstance (client , AsyncOpenAI | AsyncAzureOpenAI )
195+ # Determine if client is async or sync
196+ is_async = isinstance (client , AsyncOpenAI )
200197
201198 try :
202- if is_async_client :
199+ if is_async :
203200 resp = await _call_moderation_api_async (client , data )
204201 else :
205202 # Sync client - run in thread pool to avoid blocking event loop
206203 resp = await asyncio .to_thread (_call_moderation_api_sync , client , data )
207204 except NotFoundError as e :
208- # Moderation endpoint doesn't exist on this provider (e.g., third-party)
209- # Fall back to the OpenAI client
205+ # Moderation endpoint doesn't exist (e.g., Azure , third-party)
206+ # Fall back to OpenAI client with OPENAI_API_KEY env var
210207 logger .debug (
211208 "Moderation endpoint not available on context client, falling back to OpenAI: %s" ,
212209 e ,
213210 )
214211 client = _get_moderation_client ()
215212 resp = await _call_moderation_api_async (client , data )
216213 else :
217- # No context client, use fallback
214+ # No context client - use fallback OpenAI client
218215 client = _get_moderation_client ()
219216 resp = await _call_moderation_api_async (client , data )
220217 results = resp .results or []
0 commit comments