Draft

Testing OpenAI 4.1-mini on images

playbook
Author

Cam Avelis

Published

June 11, 2025

from openai import OpenAI
from pydantic import BaseModel

client = OpenAI()

# Function to create a file with the Files API
def create_file(file_path):
  with open(file_path, "rb") as file_content:
    result = client.files.create(
        file=file_content,
        purpose="vision",
    )
    return result.id

# Getting the file ID
file_id = create_file("images/passport-sample.jpg")

response = client.responses.create(
    model="gpt-4.1-mini",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "what's in this image?"},
            {
                "type": "input_image",
                "file_id": file_id,
            },
        ],
    }],
)

print(response.output_text)
This image is of an Iranian passport belonging to a person named Fatemeh Irani. It includes personal details such as:

- Father's name: Rohollah
- Date and place of birth: 11/02/1979, Tehran
- Sex: Female
- Passport number: S00002812
- Date of issue: 09/10/2014
- Date of expiry: 09/10/2019

The document also features a photo of the passport holder wearing a headscarf. The word "SPECIMEN" is stamped on the passport, indicating that it is a sample image.
class Passport(BaseModel):
    passport_type: str
    name: str
    number: str

response = client.responses.parse(
    model="gpt-4.1-mini",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "Please identify all of the information in this passport using the provided schema"},
            {
                "type": "input_image",
                "file_id": file_id,
            },
        ],
    }],
    text_format=Passport
)

print(response.output_text)
{"passport_type":"P","name":"FATEMEH IRANI","number":"S00002812"}
class PassportFull(BaseModel):
    passport_type: str
    country_code: str
    number: str
    name: str
    nationality: str
    date_of_birth: str
    sex: str
    place_of_birth: str
    date_of_issue: str
    date_of_expiration: str
    issuing_authority: str

response = client.responses.parse(
    model="gpt-4.1-mini",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "Please identify all of the information in this passport using the provided schema"},
            {
                "type": "input_image",
                "file_id": file_id,
            },
        ],
    }],
    text_format=PassportFull
)

print(response.output_text)
{"passport_type":"P","country_code":"IRN","number":"S00002812","name":"FATEMEH","nationality":"IRAN","date_of_birth":"11/02/1979","sex":"F","place_of_birth":"TEHRAN","date_of_issue":"09/10/2014","date_of_expiration":"09/10/2019","issuing_authority":"ISLAMIC REPUBLIC OF IRAN"}
import json
from pprint import pprint
pprint(json.loads(response.output_text))
{'country_code': 'IRN',
 'date_of_birth': '11/02/1979',
 'date_of_expiration': '09/10/2019',
 'date_of_issue': '09/10/2014',
 'issuing_authority': 'ISLAMIC REPUBLIC OF IRAN',
 'name': 'FATEMEH',
 'nationality': 'IRAN',
 'number': 'S00002812',
 'passport_type': 'P',
 'place_of_birth': 'TEHRAN',
 'sex': 'F'}
file_i797 = create_file("images/i-797c-sample.jpg")

response = client.responses.create(
    model="gpt-4.1-mini",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "what's in this image?"},
            {
                "type": "input_image",
                "file_id": file_i797,
            },
        ],
    }],
)

print(response.output_text)
This image is a document from the Department of Homeland Security, U.S. Citizenship and Immigration Services, titled "I-797, Notice of Action." It serves as an approval notice for an I-130 Petition for Alien Relative.

Key details on the document include:

- Receipt Number: WAC-1
- Case Type: I130 Petition for Alien Relative
- Receipt Date: November 1, 2010
- Priority Date: October 28, 2010
- Notice Date: January 29, 2015
- Page: 1 of 1
- Petitioner: John Doe
- Beneficiary: Jane Doe
- Notice Type: Approval Notice
- Section: Brother or sister of US citizen, 203(a)(4) INA

The notice states that the petition has been approved and the original visa petition has been sent to the Department of State National Visa Center (NVC) in Portsmouth, NH. It also provides instructions regarding further visa processing steps and contact information for the NVC.