Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Use the GPT-4o API in Python for Text and Images

Make a GPT-4o request in Python, send a local image, transcribe an audio file separately and troubleshoot API access errors.

Table of Contents

The gpt-4o API model accepts text and images and returns text. You can use the OpenAI Python SDK to send both types of input through the Responses API. For a recording, use a transcription model first, then pass the transcript into a text request. OpenAI now marks GPT-4o as deprecated. Check the GPT-4o model page before depending on it for a new project.

Choose an endpoint for the input you have

GPT-4o API Guide: Getting Started with OpenAI's API Picture 1
GPT-4o was introduced as a multimodal model. The API model and endpoint determine which inputs you can send.
Common inputs and what the API returns
InputAPI requestExample result
TextResponsesA summary or an explanation
Image with a text promptResponses with a vision-capable modelA description or a reading of visible labels
Recorded audioAudio Transcriptions, followed by Responses if neededA transcript and optional written summary

Set up the OpenAI SDK and make a text request

Create an API key for your project in the OpenAI platform. Set it as the OPENAI_API_KEY environment variable in the environment that runs Python. Keep the key out of source files, screenshots and client-side code. Use a virtual environment and install the SDK:

GPT-4o API Guide: Getting Started with OpenAI's API Picture 2
The API keys page in the OpenAI platform.
GPT-4o API Guide: Getting Started with OpenAI's API Picture 3
The key creation screen in an earlier version of the OpenAI platform.
python -m pip install --upgrade openai

Save this example as example.py and run python example.py. The SDK reads the key from the environment.

from openai import OpenAI

client = OpenAI()
response = client.responses.create(
    model="gpt-4o",
    input="Explain how to calculate 20 times 5 in one sentence.",
)
print(response.output_text)
GPT-4o API Guide: Getting Started with OpenAI's API Picture 4
Example output from a GPT-4o text request using the earlier Chat Completions interface.

If you get an authentication error, check that the key is available to the Python process and belongs to the project you intend to use. If you get a model access error, check the model ID and your project’s access. The Python learning guide covers local setup, and the HTTP and APIs course explains status codes and request debugging.

Transcribe a recording, then summarize it

The base gpt-4o model does not take audio files in the request above. Use the file transcription endpoint with a supported model. Add this to a script after initializing client:

with open("meeting.mp3", "rb") as recording:
    transcript = client.audio.transcriptions.create(
        model="gpt-transcribe",
        file=recording,
    )

response = client.responses.create(
    model="gpt-4o",
    instructions="Summarize this transcript and list any explicit action items.",
    input=transcript.text,
)
print(response.output_text)

Send a local image with a question

The vision documentation shows how to send an image using a URL or an encoded local file. This example reads a PNG next to your script. Use the MIME type that matches your file.

import base64
from pathlib import Path
from openai import OpenAI

client = OpenAI()
encoded = base64.b64encode(Path("shape.png").read_bytes()).decode("ascii")
response = client.responses.create(
    model="gpt-4o",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "List the labeled dimensions and show how to calculate the area."},
            {"type": "input_image", "image_url": f"data:image/png;base64,{encoded}"},
        ],
    }],
)
print(response.output_text)
GPT-4o API Guide: Getting Started with OpenAI's API Picture 5
First sample shape from the original API walkthrough.
GPT-4o API Guide: Getting Started with OpenAI's API Picture 6
Example output for the first shape.
GPT-4o API Guide: Getting Started with OpenAI's API Picture 7
Second sample shape, with labels that can be misread.
GPT-4o API Guide: Getting Started with OpenAI's API Picture 8
Example output showing why it is important to check the dimensions against the source image.

To use a public image URL instead, put the URL in image_url. Ask the model to list what it can read before calculating. Verify the transcription and arithmetic yourself when a label is small or the diagram is drawn out of proportion.

Check pricing and model availability

Check the model page for input and output rates before running requests at volume. Test a few actual inputs and inspect token usage and response time. If you migrate an integration, try the replacement model on known examples and verify the outputs your application depends on.

GPT-4o API Guide: Getting Started with OpenAI's API Picture 9
Historical pricing comparison from the original walkthrough. Check current provider pricing before making a decision.
Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.