Processing patterns
Short documents
Extract text and pass it directly as context when the document is short and well structured.
Long documents
Split by section, page, or content chunk to make information easier to locate and reference in a conversation.
Suggested workflow
1
Extract text and page references
Keep headings, page numbers, and table context so answers can cite their source.
2
Chunk long documents
Split large PDFs by topic or page range to avoid sending unrelated context.
3
Choose an output format
Use prose for summaries, JSON for extraction, and Markdown tables for table cleanup.
API example
For API calls, a common approach is to extract PDF text in your backend first, then include the relevant content inmessages.content. The examples below read contract.pdf, summarize it, and extract key fields.
Put the PDF in the project root: the folder containing the .py or .mjs file and where you run the command. PDF_PATH = "contract.pdf" is the filename to read. If your file is named invoice.pdf, change it to PDF_PATH = "invoice.pdf". The name, extension, and letter case must match the file exactly. Use the full local path when the file is outside the project root. You do not upload the PDF separately: the code reads the file and sends its extracted text in the request.
PdfReader is not built into Python. It is imported from the pypdf package. In the Node.js example, PDFParse is imported from pdf-parse. Python and Node.js use different package managers: run the following command in your project folder for Python; for Node.js, choose either npm or pnpm.
The Python example requires
openai and pypdf. If py is unavailable on Windows, use python -m pip install openai pypdf. The Node.js example requires openai and pdf-parse installed through npm or pnpm. If the PDF is scanned, run OCR before sending the extracted text.