OpenAI cures structured data headache for developers

1 month ago 39

Ryan Daws is a senior editor at TechForge Media with over a decade of experience in crafting compelling narratives and making complex topics accessible. His articles and interviews with industry leaders have earned him recognition as a key influencer by organisations like Onalytica. Under his leadership, publications have been praised by analyst firms such as Forrester for their excellence and performance. Connect with him on X (@gadget_ry) or Mastodon (@gadgetry@techhub.social)


OpenAI has unveiled “Structured Outputs”, a new API feature designed to address the long-standing challenge of reliably generating structured data from large language models (LLMs). The feature, available now, guarantees that model-generated outputs will adhere to developer-defined JSON Schemas.

Generating structured data from unstructured input is a cornerstone of many AI applications today. Developers leverage the OpenAI API to build sophisticated assistants capable of fetching data, answering complex questions via function calling, extracting structured data for seamless data entry, and enabling multi-step workflows where LLMs can take specific actions.

However, the inherent limitations of LLMs in consistently producing structured output have led developers to employ workarounds such as open-source tooling, intricate prompting techniques, and repeated request retries. These workarounds, while functional, add complexity and compromise efficiency.

OpenAI’s Structured Outputs promises to eliminate these workarounds. It achieves this by constraining OpenAI models to match developer-supplied schemas and by training models to better understand and adhere to complex data structures.

“Structured Outputs solves this problem by constraining OpenAI models to match developer-supplied schemas and by training our models to better understand complicated schemas,” OpenAI said in a blog post.

Internal evaluations using complex JSON schemas have shown remarkable results. The latest model, gpt-4o-2024-08-06, achieved a perfect 100% score in adherence to structured outputs, a significant improvement over the previous gpt-4-0613, which scored less than 40%.

Structured Outputs is offered in two key implementations:

  1. Function calling: This method, enabled by setting strict: true within a function definition, allows developers to define the precise structure of data returned by functions called by the model. This feature is compatible with all models supporting tools, including gpt-4-0613, gpt-3.5-turbo-0613, and later versions.
  2. Response format parameter: This approach allows developers to provide a JSON Schema via the new json_schema option within the response_format parameter. This is particularly useful when the model needs to respond directly to users in a structured format without invoking tools. Currently, this feature is supported by the latest GPT-4o models: gpt-4o-2024-08-06 and gpt-4o-mini-2024-07-18. Setting strict: true within the response_format ensures the model output conforms to the provided schema.

OpenAI has prioritised safety in Structured Outputs, ensuring it aligns with pre-existing safety policies. The model retains the ability to refuse unsafe requests, indicated to developers by a new refusal string value in API responses. This allows for programmatic detection of refusals, ensuring predictable behaviour and simplified error handling. Notably, the absence of a refusal value and a successful generation process (signified by finish_reason) guarantee a valid JSON output matching the developer-defined schema.

Native support for Structured Outputs has been integrated into OpenAI’s Python and Node SDKs, simplifying its use. Developers can define schemas for tools or response formats by providing a Pydantic or Zod object, which the SDKs automatically convert to JSON Schema. The SDKs also manage the deserialisation of JSON responses into typed data structures and handle potential refusals.

Our newest GPT-4o model is 50% cheaper for input tokens and 33% cheaper for output tokens.

It also supports Structured Outputs, which ensures model outputs exactly match your JSON Schemas. pic.twitter.com/9HXp5UAHth

— OpenAI Developers (@OpenAIDevs) August 7, 2024

Developers wanting to get started with Structured Outputs can check out OpenAI’s docs here.

(Photo by Growtika)

See also: Hugging Face partners with NVIDIA to democratise AI inference

Want to learn more about AI and big data from industry leaders? Check out AI & Big Data Expo taking place in Amsterdam, California, and London. The comprehensive event is co-located with other leading events including Intelligent Automation Conference, BlockX, Digital Transformation Week, and Cyber Security & Cloud Expo.

Explore other upcoming enterprise technology events and webinars powered by TechForge here.

Tags: , , , , , , , ,

Read Entire Article