Skip to content

Conversation

@christian-bromann
Copy link
Member

This PR adds support for OpenAI's Moderation API to the ChatOpenAI class, allowing users to check content for policy violations directly through the LangChain OpenAI provider.

Changes

New Method: moderateContent

Added a new moderateContent method to the BaseChatOpenAI class that provides direct access to OpenAI's Moderation API. This method:

  • Accepts a single text string or an array of texts to moderate
  • Supports custom moderation models (defaults to omni-moderation-latest)
  • Returns detailed moderation results including flagged status, categories, and scores
  • Properly handles errors using LangChain's error wrapping utilities

Usage Example

const model = new ChatOpenAI({ model: "gpt-4o-mini" });

// Moderate a single text
const result = await model.moderateContent("This is a test message");
console.log(result.results[0].flagged); // false
console.log(result.results[0].categories); // { hate: false, harassment: false, ... }

// Moderate multiple texts
const results = await model.moderateContent([
  "Hello, how are you?",
  "This is inappropriate content"
]);
results.results.forEach((result, index) => {
  console.log(`Text ${index + 1} flagged:`, result.flagged);
});

// Use a specific moderation model
const stableResult = await model.moderateContent(
  "Test content",
  { model: "text-moderation-stable" }
);

@changeset-bot
Copy link

changeset-bot bot commented Nov 14, 2025

⚠️ No Changeset found

Latest commit: d2680bb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@christian-bromann
Copy link
Member Author

Moving this back to draft. If anyone is interested in this feature, please give us a ping.

@christian-bromann christian-bromann marked this pull request as ready for review November 20, 2025 15:57
@christian-bromann christian-bromann marked this pull request as draft November 20, 2025 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants