Skip to content

Conversation

@steven10a
Copy link
Collaborator

Removes unneeded check that the client is OpenAI. This is handled by directly trying the moderation endpoint, if it doesn't exist we create a fallback client

  • Fixes a potential bug that was identified in the TS version. The isinstance check is fragile and if it fails to match will cause us to incorrectly set client=None

CopilotAI review requested due to automatic review settings November 3, 2025 17:38
Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR simplifies the client extraction logic in the moderation function by removing redundant type checking. The original code checked if the context's guardrail_llm attribute was specifically an AsyncOpenAI instance before using it, but this check is unnecessary since the code later handles API incompatibilities gracefully.

  • Simplified client extraction from context by removing unnecessary type check
  • Reduced lines of code from 7 to 2 while maintaining the same functionality

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/guardrails/checks/text/moderation.py:36

  • Missing import for sync OpenAI class. Line 190 uses isinstance(client, AsyncOpenAI) to detect async clients, implying sync clients are supported, but the sync OpenAI class is not imported. This will cause the code to work accidentally (since non-AsyncOpenAI clients will be treated as sync), but type checking will fail and the code is not explicit about its dependencies. Add OpenAI to the import: from openai import AsyncOpenAI, NotFoundError, OpenAI
from openai import AsyncOpenAI, NotFoundError 

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# Try the context client first, fall back if moderation endpoint doesn't exist
ifclientisnotNone:
# Determine if client is async or sync
is_async=isinstance(client, AsyncOpenAI)
Copy link

CopilotAINov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client type detection assumes any non-AsyncOpenAI client is synchronous, which may not be accurate for Azure clients or other variants. Consider using a more explicit check like hasattr(client.moderations.create, '__call__') and not inspect.iscoroutinefunction(client.moderations.create) similar to the pattern in llm_base.py (line 231), or import and check against specific sync client types (OpenAI, AzureOpenAI).

Copilot uses AI. Check for mistakes.
Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WAI. Only OpenAI clients are valid for moderation so we will catch the other types and fallback to generating an OAI client.

Copy link
Collaborator

@gabor-openaigabor-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY

@gabor-openaigabor-openai merged commit 66b40d0 into mainNov 3, 2025
9 checks passed
@gabor-openaigabor-openai deleted the dev/steven/moderation_fix branch November 3, 2025 22:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@steven10a@gabor-openai