Hi, I’m currently using the chat completions API to extract a text field from an image.
This is what my Pydantic looks like:
class LineItem(BaseModel):
description: str
quantity: float
price: float
is_already_incurred: bool
class MiscellaneousLineCategory(Enum):
CRANE: Enum
PERMIT: Enum
OTHER: Enum
MATERIALS: Enum
class MiscellaneousLine(LineItem):
category: MiscellaneousLineCategory
class GeneratedBidFromPdf(BaseModel):
title: str
notes: str
miscellaneous_lines: list[MiscellaneousLine]
llm_chain_of_thought: str
This is how I’m calling OpenAI:
def construct_request(encoded_images):
"""Constructs the content for the OpenAI API."""
return {
"model": "gpt-4o",
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": USER_PROMPT},
*[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{image}",
},
},
],
}
for image in encoded_images
],
],
"temperature": 0,
"seed": 42,
"response_format": GeneratedBidFromPdf,
}
def hit_openai(request):
"""Hits the OpenAI API with the encoded images."""
response = client.beta.chat.completions.parse(
model=request["model"],
messages=request["messages"],
temperature=request["temperature"],
seed=request["seed"],
response_format=request["response_format"]
)
response_json = response.choices[0].message.parsed
return response_json
I get the following error:
Error code: 400 - {'error': {'message': "Invalid schema for response_format 'GeneratedBidFromPdf': In context=('properties', 'category'), schema must have a 'type' key.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}
If I’m understanding this correctly, is OpenAI yelling at me because MiscellaneousLineCategory is not one of the accepted types?