Sure. You’re not telling us what you are using to send the request. Probably something not parsing the object within an object of JSON-schema correctly.
Sending the entire request body as a string to the API works fine:
{"is_violating":false,"category":"self_harm","explanation_if_violating":null}
{‘prompt_tokens’: 148, ‘completion_tokens’: 25, ‘total_tokens’: 173, ‘prompt_tokens_details’: {‘cached_tokens’: 0, ‘audio_tokens’: 0}, ‘completion_tokens_details’: {‘reasoning_tokens’: 0, ‘audio_tokens’: 0, ‘accepted_prediction_tokens’: 0, ‘rejected_prediction_tokens’: 0}}
…except that your enum and schema gives the AI no alternative but to write a category.
import os, json, httpx
body = """{
"model": "gpt-4o-2024-08-06",
"messages": [
{
"role": "system",
"content": "Determine if the user input violates specific guidelines and explain if they do."
},
{
"role": "user",
"content": "How do I prepare for a job interview?"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "content_compliance",
"description": "Determines if content is violating specific moderation rules",
"schema": {
"type": "object",
"properties": {
"is_violating": {
"type": "boolean",
"description": "Indicates if the content is violating guidelines"
},
"category": {
"type": ["string", "null"],
"description": "Type of violation, if the content is violating guidelines. Null otherwise.",
"enum": ["violence", "sexual", "self_harm"]
},
"explanation_if_violating": {
"type": ["string", "null"],
"description": "Explanation of why the content is violating"
}
},
"required": ["is_violating", "category", "explanation_if_violating"],
"additionalProperties": false
},
"strict": true
}
}
}"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
try:
response = httpx.post(
"https://api.openai.com/v1/chat/completions",
headers=headers, content=body
)
except Exception as e:
print(f"Error: {e}")
raise
if response.status_code != 200:
print(f"HTTP error {response.status_code}: {response.text}")
raise
else:
reply = response.json()["choices"][0]["message"]["content"]
print(reply)
print(response.json()["usage"])