liveBook · Manning

Structured data

As explained in Build Python Web Apps with Streamlit

Structured Outputs is an OpenAI API feature that constrains a model response to conform to a developer-provided schema.

Structured Outputs makes an LLM response conform to a developer-provided schema, so an application can parse fields reliably instead of interpreting free-form text. section 9.3.1

A practical implementation follows three steps:

  • Define a Pydantic BaseModel with typed fields such as is_correct: bool and correct_answer: str. section 9.3.1
  • Pass the model as response_format to the OpenAI parsing endpoint, which returns a parsed object. section 9.3.1
  • Use the returned attributes in application logic, such as branching between a success message and an explanation. section 9.3.1

Evaluating a quiz answer

A quiz application sends the question and player answer to an LLM with an AnswerEvaluation schema. The app reads evaluation.is_correct to show success or failure and evaluation.correct_answer to reveal the answer. section 9.3.1

Listing 9.5 The AnswerEvaluation class in answer_evaluation.py
from pydantic import BaseModel

class AnswerEvaluation(BaseModel):
  is_correct: bool
  correct_answer: str

Inspect the minimal AnswerEvaluation schema used to validate a boolean result and a string answer. section 9.3.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains schemas, typed validation, parsing, and usage, but defers the internals of inheritance and Pydantic implementation.
Examples
Uses a Pydantic AnswerEvaluation model and a quiz interface that evaluates answers and displays the correct response.
What it assumes
Assumes basic Python and LLM API use while introducing Pydantic schemas, type validation, and parsed responses.
What you can do after
Build it with Pydantic and the OpenAI parsing endpoint, then consume validated fields in application logic.
How it connects
Connects Structured Outputs with the OpenAI API, Pydantic, type hints, data validation, and Streamlit conditional logic.

Also explained in

Compare how 24 books explain structured data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.