Closed keyboardAnt closed 1 year ago
Internally, the field order of the dictionary already is in the correct order since Python preserved order by default since 3.6, and orjson.loads
does it correctly when taking in the structured response.
I added that note in that notebook because certain implementations of pretty printing reorder the field names lexigraphically which was annoying to demo. So nothing to change here.
In the schema_ttrpg.ipynb example, it's mentioned that "
orjson.dumps
preserves field order from the ChatGPT API". This order preservation could be beneficial for certain use-cases, where the order of fields could carry meaningful information or improve readability.Considering this, I propose that we replace
pydantic.BaseModel
with a subclass that inherits from it but also ensures that field order is preserved by default. This change would ensure that field order is preserved without needing to rely onorjson.dumps
.To implement this, we could consider using
collections.OrderedDict
in our subclass to store model attributes.