The documentation states that enums are supported:
https://platform.openai.com/docs/guides/structured-outputs/supported-schemas
I’ve tried it with a simple test case but the API rejects the generated schema:
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'ColorDetection': In context=('properties', 'color'), 'allOf' is not permitted", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}`Preformatted text`
test case:
from enum import Enum
from openai import OpenAI
from pydantic import BaseModel, Field
class Color(Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
class ColorDetection(BaseModel):
color: Color = Field(description="The detected color")
hex_color_code: str = Field(description="The hex color code of the detected color")
def test_detect_color():
client = OpenAI()
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "user",
"content": "What color is a Coke can?"
}
],
response_format=ColorDetection,
)
detected = completion.choices[0].message.parsed
assert detected.color == Color.RED, f"Invalid color detected: {detected}"
generated schema:
{
"$defs": {
"Color": {
"enum": [
"red",
"blue",
"green"
],
"title": "Color",
"type": "string"
}
},
"properties": {
"color": {
"allOf": [
{
"$ref": "#/$defs/Color"
}
],
"description": "The detected color"
},
"hex_color_code": {
"description": "The hex color code of the detected color",
"title": "Hex Color Code",
"type": "string"
}
},
"required": [
"color",
"hex_color_code"
],
"title": "ColorDetection",
"type": "object"
}