I’ve searched everywhere and asked chatgpt, and find that there are many different way to get structured outputs, which should I use on the earth?
from openai import OpenAI
from pydantic import BaseModel, Field
client = OpenAI(api_key=vllm_key, base_url=vllm_url)
class Car(BaseModel):
brand: str = Field(description="Brand name, usually same as the company name")
model: str = Field(description="Model Name")
class Cars(BaseModel):
car_list: list[Car]
messages = [{"role": "user", "content": "list 10 kinds of famous cars"}]
model_and_messages = {"model": "qwen3-32b-bnb-4bit", "messages": messages}
# type 1
response1 = client.responses.parse(
model="qwen3-32b-bnb-4bit",
input=messages,
text_format=Cars,
)
# type 2
response2 = client.chat.completions.parse(
**model_and_messages,
response_format=Cars,
)
# type 3
response3 = client.chat.completions.create(
**model_and_messages,
extra_body={"structured_outputs": {"json": Cars.model_json_schema()}},
)
# type 4
response4 = client.chat.completions.parse(
**model_and_messages,
response_format={
"type": "json_schema",
"json_schema": {
"name": "prediction_response",
"strict": True,
"schema": Cars.model_json_schema()
}
},
)
print(response1.output_parsed)
print(response2.choices[0].message.content)
print(response3.choices[0].message.content)
print(response4.choices[0].message.content)
car_list=[Car(brand='Ford', model='Mustang'), Car(brand='Chevrolet', model='Corvette'), Car(brand='Porsche', model='911'), Car(brand='BMW', model='M3'), Car(brand='Mercedes-Benz', model='E-Class'), Car(brand='Toyota', model='Supra'), Car(brand='Lamborghini', model='Aventador'), Car(brand='Ferrari', model='488 GTB'), Car(brand='Audi', model='RS 6'), Car(brand='Mazda', model='Miata')]
{"car_list": [{"brand": "Ferrari", "model": "F40"}, {"brand": "Porsche", "model": "911"}, {"brand": "Ford", "model": "Mustang"}, {"brand": "Chevrolet", "model": "Corvette"}, {"brand": "Lamborghini", "model": "Diablo"}, {"brand": "Mercedes-Benz", "model": "S-Class"}, {"brand": "BMW", "model": "M3"}, {"brand": "Toyota", "model": "Supra"}, {"brand": "Dodge", "model": "Challenger"}, {"brand": "Aston Martin", "model": "DB"}] }
{"car_list": [{"brand": "Ferrari", "model": "F40"}, {"brand": "Porsche", "model": "911"}, {"brand": "Ford", "model": "Mustang"}, {"brand": "Chevrolet", "model": "Corvette"}, {"brand": "Lamborghini", "model": "Countach"}, {"brand": "Mercedes-Benz", "model": "300 SL"}, {"brand": "Aston Martin", "model": "DB5"}, {"brand": "Toyota", "model": "Supra"}, {"brand": "Nissan", "model": "Skyline GT-R"}, {"brand": "BMW", "model": "E30 M3"}] }
{"car_list": [{"brand": "Ferrari", "model": "F40"}, {"brand": "Porsche", "model": "911"}, {"brand": "Ford", "model": "Mustang"}, {"brand": "Chevrolet", "model": "Corvette"}, {"brand": "Lamborghini", "model": "Countach"}, {"brand": "BMW", "model": "M3"}, {"brand": "Mercedes-Benz", "model": "S-Class"}, {"brand": "Toyota", "model": "Supra"}, {"brand": "Audi", "model": "R8"}, {"brand": "Mazda", "model": "Miata"}] }