OpenAI API with Structured Output not including a required parameter

I have the following definition:

from typing import Literal, TypedDict
from pydantic import BaseModel

class Number(BaseModel):
    type: Literal["number"]
    name: str
    class Options(TypedDict):
        precision: int
    options: Options

When I pass this class in response_format, the API responds back with output as follows:

        {
          "type": "number",
          "name": "ID"
        }

Note that options and options.precision are missing even though they are required. What am I doing wrong here?

1 Like

The reason for that is likely that current pydantic model class that’s being passed is devoid of any explicit description of the contents of attributes and the attributes’ names aren’t intuitive enough to convey their intended purpose inside the pydantic model.

1 Like

Python versions. That typing works only if you are using Python 3.12 - which is a continued source of problems for those who are trying structured outputs with the openai library.

Python 3.11:

from typing import Literal
from typing_extensions import TypedDict  # Required on Python < 3.12 version which openai likes
from pydantic import BaseModel

class Number(BaseModel):
    type: Literal["number"]
    name: str
    class Options(TypedDict):
        precision: int
    options: Options

response to random prompt:

{'type': 'number', 'name': 'Winning Score', 'options': {'precision': 0}} 

Also producing the same object:

class Options(BaseModel):
    precision: int

class Number(BaseModel):
    type: Literal["number"]
    name: str
    options: Options

If you just want the schema sent as response_format:

    response_format={ "type": "json_schema", "json_schema":
        {
        "name": "get_precision_info",
        "description": "obtains a precision",
        "strict": True,
        "schema":
            {"name": "schema", '$defs': {'Options': {'properties': {'precision': {'title': 'Precision', 'type': 'integer'}}, 'required': ['precision'], 'title': 'Options', 'type': 'object', 'additionalProperties': False}}, 'properties': {'type': {'const': 'number', 'enum': ['number'], 'title': 'Type', 'type': 'string'}, 'name': {'title': 'Name', 'type': 'string'}, 'options': {'$ref': '#/$defs/Options'}}, 'required': ['type', 'name', 'options'], 'title': 'Number', 'type': 'object', 'additionalProperties': False}
        }
    }

Many thanks! I will try this using python 3.11.

That fixed it. Thank you very much.

1 Like