GPT structured output and PDF
This page covers structured output and PDF input for the GPT family. For model IDs and endpoint setup, see the GPT family overview.
Structured output
Pass a JSON Schema in response_format; with strict: true, the output strictly conforms to the schema:
gpt_json_schema.py
import json
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ofox.io/v1", api_key=os.environ["OFOX_API_KEY"])
resp = client.chat.completions.create(
model="openai/gpt-5.4-mini",
response_format={
"type": "json_schema",
"json_schema": {
"name": "price",
"strict": True,
"schema": {
"type": "object",
"properties": {"price": {"type": "number"}, "currency": {"type": "string"}},
"required": ["price", "currency"],
"additionalProperties": False,
},
},
},
messages=[{"role": "user", "content": "A bat and a ball cost $1.10 in total. The bat costs $1 more than the ball. How much does the ball cost?"}],
)
print(json.loads(resp.choices[0].message.content))For more patterns, see Structured output.
PDF input
Chat Completions
gpt_pdf.py
import base64
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ofox.io/v1", api_key=os.environ["OFOX_API_KEY"])
pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode()
resp = client.chat.completions.create(
model="openai/gpt-5.4-mini",
messages=[{"role": "user", "content": [
{"type": "file", "file": {"filename": "invoice.pdf", "file_data": f"data:application/pdf;base64,{pdf_b64}"}},
{"type": "text", "text": "What is the total amount on this invoice?"},
]}],
)
print(resp.choices[0].message.content)For other errors, see Common errors.
Last updated on