Getting Started with Structured Output in Azure OpenAI
An implementation in Python with GPT-4o-mini
Recently, OpenAI announced a new feature for developers, which allows them to “force” complex structured outputs when it comes to GPT-generated content. Soon after, Microsoft announced the same new features available also for some of its Azure OpenAI models, including the latest GPT-4o-mini.
Structured output allows for getting a pre-defined, deterministic schema of the generated text output, making it easier to analyse, categorize, and act upon.
Now you might wander: why do I need structured output, if I can tell the LLM to generate the output in a specific format directly in the system message?
system_message = """ You are a sentiment analyzer. You analyze reviews and assess whether the sentiment is positive or negative.
The output should have the following schema:
- Category of the review
- Sentiment
- Age and gender of the reviewer
"""
The answer is that we are still dealing with stochastic engines, and you cannot rely 100% on them following the instructions all the times. If you prompt your model to always output your answer with a pre-defined json schema (which you provided in your system message) so that, for example, you can then insert it into a database, it might…