Skip to content

Lesson 4 of 6

Send a long document through the API

Read a text file in Python and pass its full contents to the Gemini API with your prompt, then check that long context works.

7 minClaude, Gemini and ChatGPT

After this lesson, you will be able to read a long text file in Python, send its full contents to the Gemini API with a prompt that asks for quoted evidence, and check that the answer uses the long context correctly.

Start from the working request

In the previous lesson you wrote first_request.py, which sends a short text to Gemini and prints the response. This lesson extends that script to read a long document from a file and send it in one request. Keep the same setup: the API key from the environment, the client, and the model name constant.

Create a new file called long_document.py and copy the beginning of first_request.py into it:

import os
from google import genai

api_key = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=api_key)

# Replace with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"

Read the document file

You need a long document to test with. Any plain text file of at least a few thousand words will do. If you do not have one, create a file called report.txt and paste in a long article or report. The file must be saved as UTF-8 text.

Add a function that reads a file and returns its contents as one string:

def read_file(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

Call this function with the path to your document. For this lesson, assume the file is called report.txt and is in the same folder as the script:

document_text = read_file("report.txt")

Build the prompt with the document

The prompt from lesson 2 asked Gemini to answer a question, give a short quote for each point, and write a summary. That prompt works well with the API because it asks for evidence you can check. Use the same structure here, but put the document text into the prompt string.

Write a prompt that includes the document and asks for the main challenges, with a quote for each, and a short summary:

prompt = f"""Read the document carefully. First, answer this question: What are the main challenges mentioned? Give each challenge as a short sentence. Then, for each challenge, give a short quote from the document that supports it. Finally, write a summary of the whole document in no more than 50 words.

Document:
{document_text}"""

The f before the string makes it an f-string, so {document_text} is replaced with the file contents. The document can be many thousands of words; Gemini's long context window can handle it in one request.

Send the request and print the answer

Send the prompt exactly as before:

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=prompt,
)
print(response.text)

The full script long_document.py now looks like this:

import os
from google import genai

def read_file(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

api_key = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=api_key)

# Replace with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"

document_text = read_file("report.txt")

prompt = f"""Read the document carefully. First, answer this question: What are the main challenges mentioned? Give each challenge as a short sentence. Then, for each challenge, give a short quote from the document that supports it. Finally, write a summary of the whole document in no more than 50 words.

Document:
{document_text}"""

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=prompt,
)
print(response.text)

Run it from the terminal:

python long_document.py

The script prints Gemini's answer. It should list several challenges, each with a short quote in quotation marks, and end with a summary of 50 words or fewer.

Check that long context works

Long context means the model can see the whole document at once, not just the first part. To check this, look at the quotes in the answer. Open report.txt and search for each quote. The quotes should appear exactly in the document, and they should come from different parts of it, including near the end. If the quotes are all from the first page, the model may not have used the full document.

Also check the summary. It should mention ideas from across the document, not only the opening. If the summary seems to ignore the second half, the document may be longer than the model's context window for this request. In that case, try a shorter document or a model with a larger context window from the provider's list.

The prompt asks for a summary of no more than 50 words. Count the words in the summary part of the answer. If it is longer, the model did not follow that instruction. You can make the instruction stricter by saying "in exactly 50 words or fewer" or "no more than 50 words, counted strictly".

Keep the script for the next lesson

You will build on long_document.py in the next lesson, where you turn it into a command-line tool. Keep the file as it is now, with the document path hard-coded. The next lesson replaces that with a command-line argument.

Key takeaways

  • Read a file with open and pass its full text into the prompt string using an f-string.
  • Gemini's long context window can handle a document of many thousands of words in one request.
  • Ask for short quotes as evidence, then check that the quotes appear in the document and come from different parts.
  • A summary that mentions ideas from across the document is a sign that long context worked.

Try it

Take a long document of your own, at least 2000 words, and save it as my_report.txt. Modify long_document.py to read that file instead of report.txt. Change the prompt to ask for the three most important recommendations in the document, with a short quote for each, and a summary of no more than 60 words. Run the script. A good result has exactly three recommendations, each with an exact quote from the document, and a summary of 60 words or fewer. Check the quotes against the document and make sure at least one quote comes from the second half of the document.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.