5. Send Direct File Input
Direct file input is separate from application-managed tools. A file can be included in the current Responses request as an input_file item alongside text instructions, which is useful when the model should read the file for this turn without setting up a retrieval index.
5.1 Attach a PDF as input_file
This cell generates a tiny PDF transcript in memory, attaches it as base64 file data, and asks for exact JSON fields from the document. Inspect the PDF preview, expected fields, parsed response, and usage summary.
python
from __future__ import annotations
def make_simple_pdf(lines: list[str]) -> bytes:
def pdf_escape(text: str) -> str:
return text.replace("\\", "\\\\").replace("(", "\\(").replace(")", "\\)")
stream_lines = ["BT", "/F1 11 Tf", "72 740 Td", "15 TL"]
for idx, line in enumerate(lines):
if idx:
stream_lines.append("T*")
stream_lines.append(f"({pdf_escape(line)}) Tj")
stream_lines.append("ET")
stream = "\n".join(stream_lines).encode("latin-1", "replace")
objects = [
b"<< /Type /Catalog /Pages 2 0 R >>",
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>",
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
b"<< /Length " + builtins.str(len(stream)).encode("ascii") + b" >>\nstream\n" + stream + b"\nendstream",
]
pdf = b"%PDF-1.4\n"
offsets = [0]
for idx, obj in enumerate(objects, start=1):
offsets.append(len(pdf))
pdf += f"{idx} 0 obj\n".encode("ascii") + obj + b"\nendobj\n"
xref_offset = len(pdf)
pdf += f"xref\n0 {len(objects) + 1}\n0000000000 65535 f \n".encode("ascii")
for offset in offsets[1:]:
pdf += f"{offset:010d} 00000 n \n".encode("ascii")
pdf += f"trailer\n<< /Size {len(objects) + 1} /Root 1 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n".encode("ascii")
return pdf
file_lines = [
"BrightCart support transcript",
"Ticket: TICKET-7429",
"Customer: Maya Chen",
"Order: ORDER-8831",
"Product: Standing desk replacement",
"Issue: Replacement for a damaged item is delayed and carrier scan has not moved",
"Customer request: Supervisor callback and refund options",
"Policy options: expedited replacement or 15% concession with agent approval after 48-hour delay",
]
file_text = "\n".join(file_lines)
pdf_data = base64.b64encode(make_simple_pdf(file_lines)).decode("utf-8")
expected_direct_file_fields = {
"ticket_id": "TICKET-7429",
"customer": "Maya Chen",
"order_id": "ORDER-8831",
"product": "Standing desk replacement",
}
direct_file_request = {
"model": MODEL_ID,
"input": [
{
"role": "user",
"content": [
{
"type": "input_file",
"filename": "brightcart-support-transcript.pdf",
"file_data": f"data:application/pdf;base64,{pdf_data}",
},
{
"type": "input_text",
"text": (
"Read the attached PDF support transcript and return JSON with keys "
"ticket_id, customer, order_id, product, issue, requested_resolution, and policy_options. "
"Use exact values from the file. Do not return null for fields that are present in the file."
),
},
],
}
],
"text": {"format": {"type": "json_object"}},
"max_output_tokens": 1024,
"store": False,
}
print_labeled_text("Result: PDF transcript preview", file_text)
print_request_shape(direct_file_request)
print_labeled_json("Result: expected fields", expected_direct_file_fields)
try:
direct_file_response = create_response(**direct_file_request)
raw_direct_file_output = output_text(direct_file_response).strip()
try:
direct_file_payload = json.loads(raw_direct_file_output)
missing_or_empty = [
key for key, expected in expected_direct_file_fields.items()
if builtins.str(direct_file_payload.get(key, "")).strip().lower() != expected.lower()
]
null_fields = [key for key, value in direct_file_payload.items() if value in {None, "", []}]
if missing_or_empty or null_fields:
record_check("Direct file inputs", "warn", {
"message": "The request completed, but the model did not extract the expected values from the attached PDF.",
"missing_or_unexpected_fields": missing_or_empty,
"empty_fields": null_fields,
"payload": direct_file_payload,
})
record_response("Support transcript extraction returned by model", "json", direct_file_payload)
print_labeled_text("Result", "The request completed, but the model did not extract the expected values from the attached PDF.")
print_labeled_json("Result: returned JSON", direct_file_payload)
else:
record_check("Direct file inputs", "pass", direct_file_payload)
record_response("Support transcript extraction", "json", direct_file_payload)
print_labeled_json("Result", direct_file_payload)
except Exception as parse_exc:
record_check("Direct file inputs", "warn", {
"message": "The request completed, but the response was not valid JSON.",
"text_sample": raw_direct_file_output[:600],
"error": builtins.str(parse_exc),
})
record_response("Support transcript extraction text", "text", raw_direct_file_output[:1200])
print_labeled_text("Result", raw_direct_file_output[:1200])
print_response_summary(direct_file_response)
print_key_takeaway('Direct file input is useful when the file should be read in the current request context.')
except Exception as exc:
handle_example_error("Direct file inputs", exc)Result: PDF transcript preview
BrightCart support transcript Ticket: TICKET-7429 Customer: Maya Chen Order: ORDER-8831 Product: Standing desk replacement Issue: Replacement for a damaged item is delayed and carrier scan has not moved Customer request: Supervisor callback and refund options Policy options: expedited replacement or 15% concession with agent approval after 48-hour delay
Request shape
| field | value |
|---|---|
| model | openai.gpt-5.4 |
| max_output_tokens | 1024 |
| store | False |
| text format | json_object |
| input | 1 item(s): user: input_file: brightcart-support-transcript.pdf; input_text: Read the attached PDF support transcript and return JSON with keys ticket_id, customer, order_id, product, issue, reques... |
Result: expected fields
\{ "ticket_id": "TICKET-7429", "customer": "Maya Chen", "order_id": "ORDER-8831", "product": "Standing desk replacement" \}
Result
\{"ticket_id":"TICKET-7429","customer":"Maya Chen","order_id":"ORDER-8831","product":"Standing desk replacement","issue":"Replacement for a damaged item is delayed and carrier scan has not moved","requested_resolution":"Supervisor callback and refund options","policy_options":"expedited replacement or 15% concession with agent approval after 48-hour delay"\}
Response summary
| field | value |
|---|---|
| id | resp_colsvndmpjd6qczpemdjscmsbjefmgl5vh6i7alqt52jflentfna |
| model | openai.gpt-5.4 |
| status | completed |
| output_item_types | ['message'] |
| input_tokens | 713 |
| cached_input_tokens | 0 |
| output_tokens | 82 |
| total_tokens | 795 |
| reasoning_output_tokens | 0 |
| service_tier | default |
Key takeaway: Direct file input is useful when the file should be read in the current request context.