Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Gemini with files

Hello!

Is it possible to do this:

Process a PDF file with Gemini

passing the PDF as a base-64 encoded string instead of as a GS URI?

Thanks!

0 3 973
3 REPLIES 3

Hello,

Thank you for contacting Google Cloud Community!

Yes, it is possible to process a PDF file with Gemini by passing the PDF as a base-64 encoded string instead of as a GS URI. Here's how you can do it:

Encode the PDF file to base-64:

  • Use a programming language or online tool to convert the PDF file into a base-64 encoded string.
  • Make sure the base-64 string includes the data:application/pdf;base64, prefix.


Create a Gemini request:

  • Construct a Gemini request that includes the base-64 encoded PDF string as the input.
  • Specify the appropriate prompt or instructions for Gemini to process the PDF content.


Send the request to Gemini:

  • Use the Gemini API or a client library to send the request and receive the response.
  • Here's an example of how you might do this using Python and the requests library:

Python
import requests

# Replace with your Gemini API endpoint and API key
api_endpoint = "https://api.gemini.com/v1/chat"
api_key = "YOUR_API_KEY"

# Base-64 encoded PDF string
pdf_base64 = "data:application/pdf;base64,YOUR_BASE64_ENCODED_PDF_HERE"

# Gemini request payload
payload = {
"prompt": "Analyze the content of this PDF document:",
"input": pdf_base64
}

headers = {
"Authorization": f"Bearer {api_key}"
}

response = requests.post(api_endpoint, json=payload, headers=headers)

if response.status_code == 200:
print(response.json())
else:
print("Error:", response.text)

Hello,

Thank you for your engagement regarding this issue. We haven’t heard back from you regarding this issue for sometime now. Hence, I'm going to close this issue which will no longer be monitored. However, if you have any new issues, Please don’t hesitate to create a new issue. We will be happy to assist you on the same.

Regards,
Jai Ade

Jaja,

Just want to confirm, are there extra costs associated with using uploaded file vs. base64 encoded string? For ex, storage usage or ingress/egress?