import openai
import os
from pydantic import BaseModel
# This is the model for latitude and longitude
class Location(BaseModel):
latitude: float
longitude: float
OPENROUTER_API_KEY = os.environ["OPENROUTER_API_KEY"]
client = openai.OpenAI(
base_url='https://openrouter.ai/api/v1',
api_key=OPENROUTER_API_KEY
)
MODEL = "openai/gpt-oss-120b:free" #@param ["openai/gpt-oss-120b:free"]
response = client.beta.chat.completions.parse(
model=MODEL,
messages=[
{"role": "system", "content": "You help students with their geography lessons."},
{"role": "user", "content": "What are the GPS coordinates for Ulaanbaatar?"},
],
response_format=Location
)
result = response.choices[0].message
print(result)Structured Output
Sometimes the output from an LLM can be unpredictable. It might just give you the answer (42) or it might add extra words and sentences (The answer is 42 or The answer is 42. Let me know if you have another question.)
In this notebook, we use a technique called structured output to require the output to be in a particular Python format. This makes it easier to use that data in other programs.
Now that we have the completion result, we can parse it to extract the exact latitude and longitude.
location = result.parsed
print(location.latitude)
print(location.longitude)Now that you’ve seen how structured output works, take a moment to think about how you might use it.
Think about a program you’d like to build that uses AI. What kind of structured data would be most useful to get back — a list of steps, a set of numbers, a yes/no answer? Describe your idea here.
{ “question_type”: “freeform”, “question”: “What Python library is used to define the data model (like the Location class) for structured output?”, “answer”: “pydantic”, “submitted_answer”: “” }
{ “question_type”: “multiple_choice”, “question”: “Which method replaces .create() when using structured output with the OpenAI SDK?”, “options”: [ { “key”: “a”, “text”: “.parse()” }, { “key”: “b”, “text”: “.format()” }, { “key”: “c”, “text”: “.structure()” }, { “key”: “d”, “text”: “.extract()” } ], “answer”: “a”, “submitted_answer”: “” }
{ “question_type”: “true_false”, “question”: “Without structured output, an LLM might return the same information in a different format each time it is called.”, “answer”: “True”, “submitted_answer”: “” }