Working with freeform text from LLMs isn’t optimal when you know how you want the reply formatted.

Using structured outputs, you define a class, based on Pydantic’s BaseModel, for the reply format you want and attach it to the LLM configuration. Replies from agent using that configuration will be in a matching JSON format.

In earlier examples, we created a classroom lesson plan and provided guidance in the agent’s system message to put the content in tags, like <title> and <learning_objectives>. Using structured outputs we can ensure that our lesson plans are formatted.

import json

from pydantic import BaseModel

from autogen import ConversableAgent


# 1. Define our lesson plan structure, a lesson with a number of objectives
class LearningObjective(BaseModel):
    title: str
    description: str


class LessonPlan(BaseModel):
    title: str
    learning_objectives: list[LearningObjective]
    script: str


# 2. Add our lesson plan structure to the LLM configuration
llm_config = {
    "model": "gpt-4o-mini",
    "response_format": LessonPlan,
}

# 3. The agent's system message doesn't need any formatting instructions
system_message = """You are a classroom lesson agent.
Given a topic, write a lesson plan for a fourth grade class.
"""

my_agent = ConversableAgent(name="lesson_agent", llm_config=llm_config, system_message=system_message)

# 4. Chat directly with our agent
chat_result = my_agent.run("In one sentence, what's the big deal about AI?")

# 5. Get and print our lesson plan
lesson_plan_json = json.loads(chat_result.chat_history[-1]["content"])
print(json.dumps(lesson_plan_json, indent=2))
{
  "title": "Exploring the Solar System",
  "learning_objectives": [
    {
      "title": "Understanding the Solar System",
      "description": "Students will learn the names and order of the planets in the solar system."
    },
    {
      "title": "Identifying Planet Characteristics",
      "description": "Students will be able to describe at least one distinctive feature of each planet."
    },
    {
      "title": "Creating a Planet Fact Sheet",
      "description": "Students will create a fact sheet for one planet of their choice."
    }
  ],
  "script": "Introduction (10 minutes):\nBegin the class by asking students what they already know about the solar system. Write their responses on the board. \n\nIntroduce the topic by explaining that today they will be learning about the solar system, which includes the Sun, planets, moons, and other celestial objects.\n\nDirect Instruction (20 minutes):\nUse a visual aid (such as a poster or video) to show the solar system's structure. \n\nDiscuss the eight planets: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune. \n\nFor each planet, mention:\n- Its position from the Sun.\n- Key characteristics (e.g., size, color, temperature).\n- Any notable features (e.g., rings, atmosphere). \n\nInteractive Activity (15 minutes):\nSplit the class into small groups. Give each group a set of planet cards that include pictures and information. Have them work together to put the planets in order from the Sun. Each group will present their order and one interesting fact about each planet they discussed."
}

Add a format function to the LessonPlan class in the example to convert the returned value into a string. Example here.