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}
}
}