Skip to main content
When providing your Agent with a structured output format to follow, you will normally prefer to do so using a Pydantic model. You can read more about structured outputs here. If you need to use provider-specific schema formats, you can also use a simple JSON dict as schema, instead of a Pydantic model. For example:
1

Add the following code to your Python file

json_schema_output.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from rich.pretty import pprint

person_schema = {
    "type": "json_schema",
    "json_schema": {
        "name": "PersonInfo",
        "schema": {
            "type": "object",
            "properties": {
                "name": {"type": "string", "description": "Person's full name"},
                "age": {"type": "integer", "description": "Person's age"},
                "occupation": {"type": "string", "description": "Person's occupation"},
            },
            "required": ["name", "age", "occupation"],
            "additionalProperties": False,
        },
    },
}

agent = Agent(model=OpenAIChat(id="gpt-4o-mini"), markdown=False)

response = agent.run(
    "Tell me about Albert Einstein",
    output_schema=person_schema,
    stream=False,
)

pprint(response.content)
2

Create a virtual environment

Open the Terminal and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
3

Install libraries

pip install -U agno openai rich
4

Export your OpenAI API key

  export OPENAI_API_KEY="your_openai_api_key_here"
5

Run the example

python json_schema_output.py