Run batch text moderation
Install dependencies in the documentation directory using the Python guide, then replace the address, token, and moderation model. The script submits two fixed texts and printsallow or review in input order without printing source text or scores.
examples/moderations.py uses the SDK’s generic HTTP method to read raw dictionaries so missing fields or string booleans cannot become valid decisions.
Apply the result
resultsfollows input order; there is no document index to replace that position. The example checks the count and validates every result before printing decisions.flaggedis the overall signal,categoriescontains category signals, andcategory_scorescontains confidence scores. The sample application routes any positive signal to review;allowis not proof of safety.- Categories can evolve with the model, so the example does not freeze an enum. Evaluate thresholds for your language, use case, and model instead of copying thresholds across models. See the OpenAI moderation guide for field semantics.
- HTTP errors, empty results, and invalid scores mean no usable decision. They do not mean approval. The script exits unsuccessfully; the application must choose review, delayed publication, or a retry message.
Check inputs and outputs
Check user input before deciding whether to invoke generation, and check generated content before publication. If a workflow requires reviewing the complete output, buffer it on the server before publishing. Do not assume streamed content already shown to a user can be recalled. Image moderation requires separate confirmation of the model, channel, and content block format. This guide and example cover only text and text batches. Moderation does not replace permissions, tool execution authorization, or RAG document access controls.Gateway adaptation caveat
The current OpenAI adapter can rebuild a response as a generic text response when the channel enablesForceFormat, losing moderation-specific results. For HTTP 200 without results, ask the administrator to inspect channel response formatting and the original upstream structure. A registered route does not guarantee every channel preserves moderation fields.
Errors and retries · Production integration
阅读中文