ChatCompletion wrapper to output Pydantic models

GPT-3.5-turbo and GPT-4 are great at converting unstructured text to structured data. This simple wrapper around the openai.ChatCompletion.create() allows you to specify a Pydantic model that you want as output, allowing you to go from unstructured text to validated data output in one line of code.

It works by inserting a tuned prompt message that includes the Pydantic model’s json schema. Any text description fields in the Pydantic model definition are naturally included in the schema for the language model’s benefit.

Post-completion it validates the language model output and remediates errors by providing feedback to the language model if errors are encountered during json parsing or during Pydantic model validation.

See the example directory for several different examples.

1 Like