Skip to main content

Combining File and Text Inputs

In this lesson, you'll learn how to send a file along with a user prompt to GPT-4.1 using the input_file and input_text types.

Prerequisites

  • Have a file ready to upload (PDF, TXT, etc.)
  • Install the OpenAI SDK:
pip install openai

The Code

from openai import OpenAI

client = OpenAI()

file = client.files.create(
file=open("../files/Nexovious Project Info.pdf", "rb"),
purpose="user_data"
)

response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": file.id
},
{
"type": "input_text",
"text": "Which services does Nexovious provide?"
}
]
}
]
)

print(response.output_text)

Explanation

  • client.files.create(...): Uploads the file to OpenAI and returns a file ID.
  • type: input_file: Tells the model to look into the uploaded file.
  • type: input_text: Combines with a user question referencing the file.
  • response.output_text: Gives the assistant’s reply based on both sources.

Use Case

Great for:

  • Asking questions about user-uploaded PDFs or documents
  • Creating assistants that summarize or search file content
  • Mixing file context with specific user queries