Skip to content

feat: add OpenAIModerationHook - #14031

Draft
sjrl wants to merge 4 commits into
mainfrom
feat/openai-moderation-hook
Draft

sjrl wants to merge 4 commits into
mainfrom
feat/openai-moderation-hook

Conversation

@sjrl

@sjrl sjrl commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Adds OpenAIModerationHook in haystack.hooks.moderation, which checks the messages an Agent sends to its LLM, and its final reply, with the OpenAI Moderation API.

  • Register at before_llm and after_run. Each run checks only the latest messages in one request: the new user input on the first LLM call, the tool results on later calls, and the final reply at after_run. Earlier history, system messages and system reminders are not sent.
  • A flagged message's text is replaced with a fixed note naming the flagged categories, e.g. "[This message was removed because content moderation flagged it for: violence.]", so flagged input and tool results never reach the LLM.
  • categories selects which categories count; category_thresholds sets per-category minimum scores that replace OpenAI's own flag.
  • Moderation API errors are raised, like the chat generators do.
  • Sync and async clients, created in warm_up / warm_up_async.
  • Decisions recorded as haystack.agent.hook.moderation.* span tags: messages checked, whether anything was flagged, and the flagged categories with their scores. The checked text isn't recorded, since the Agent's own spans already trace it when content tracing is on.

How did you test it?

  • Unit tests on State, inside an Agent (flagged input and tool results never reach the LLM, flagged final reply is replaced), async, and tracing; hatch run test:types passes.
  • Live run against the Moderation API with harmless text only, sync and async: "How do I bake bread?" passes unchanged, and "The knight drew his sword." is blocked when forced with category_thresholds={"violence": 0.0}.

Notes for the reviewer

  • With streaming, after_run comes too late to keep a flagged reply from the user; the before_llm check is unaffected. Moderating streamed output is tracked separately (https://github.com/deepset-ai/haystack-private/issues/629).
  • Kept deliberately small: no stop action, no error fallback, and no custom block text for now. Each can be added later without breaking changes if users ask for it.
  • Docs page and guardrails table entry will follow in a separate PR.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@vercel

vercel Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
haystack-docs Ignored Ignored Preview Dec 1, 2026 11:23am UTC

Request Review

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/core/pipeline
  pipeline.py
  haystack/hooks/moderation
  __init__.py
  hooks.py 66, 102, 295, 385
Project Total  

This report was generated by python-coverage-comment-action

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant