How to Intermediate · 4 min read

How to analyze PDF with Gemini API

Quick answer
Use the gemini-1.5-pro model with the Google Gemini API to analyze PDFs by first extracting text from the PDF, then sending that text as input in a chat completion request. The API does not directly ingest PDFs, so use a Python PDF loader like PyPDF2 or pdfplumber to extract text before calling client.generate_text.

PREREQUISITES

  • Python 3.8+
  • Google Cloud account with Gemini API access
  • Set environment variable GOOGLE_API_KEY with your API key
  • pip install google-ai-generativelanguage PyPDF2

Setup

Install the required Python packages and set your Google API key as an environment variable.

  • Install packages: pip install google-ai-generativelanguage PyPDF2
  • Set environment variable in your shell: export GOOGLE_API_KEY='your_api_key_here'
bash
pip install google-ai-generativelanguage PyPDF2

Step by step

Extract text from a PDF file using PyPDF2 and send it to the Gemini API for analysis with the gemini-1.5-pro model.

python
import os
from google.ai import generativelanguage as gl
from PyPDF2 import PdfReader

# Initialize Gemini client
client = gl.TextServiceClient(client_options={"api_key": os.environ["GOOGLE_API_KEY"]})

# Load PDF and extract text
def extract_pdf_text(pdf_path):
    reader = PdfReader(pdf_path)
    text = []
    for page in reader.pages:
        text.append(page.extract_text())
    return "\n".join(text)

pdf_text = extract_pdf_text("sample.pdf")

# Prepare prompt for analysis
prompt = f"Analyze the following PDF content:\n{pdf_text}"

# Create a text generation request
response = client.generate_text(
    model="gemini-1.5-pro",
    prompt=prompt,
    temperature=0.2,
    max_tokens=1024
)

print("Analysis result:\n", response.text)
output
Analysis result:
 <Gemini API response text analyzing the PDF content>

Common variations

You can use other PDF extraction libraries like pdfplumber for more accurate text extraction. For asynchronous calls, use the async client methods if supported. You may also choose different Gemini models such as gemini-2.0-flash for faster responses or gemini-1.5-flash for cost efficiency.

python
import pdfplumber

# Alternative PDF text extraction
with pdfplumber.open("sample.pdf") as pdf:
    text = "\n".join(page.extract_text() for page in pdf.pages)

# Then use the same Gemini client call as above with 'text' as input

Troubleshooting

  • If you get empty or garbled text, verify your PDF extraction method and try a different library.
  • If the API returns errors, check your API key and quota in Google Cloud Console.
  • For very large PDFs, split the text into chunks before sending to avoid token limits.

Key Takeaways

  • Gemini API requires text input, so extract PDF text before calling the API.
  • Use gemini-1.5-pro for high-quality analysis of PDF content.
  • Handle large PDFs by chunking text to stay within token limits.
  • Use reliable PDF extraction libraries like PyPDF2 or pdfplumber.
  • Always secure your API key via environment variables.
Verified 2026-04 · gemini-1.5-pro, gemini-1.5-flash, gemini-2.0-flash
Verify ↗