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

Having trouble adding PDF to 1.5 pro, TypeError "Could not create `Blob`"

I am using `models/gemini-1.5-pro-latest`

I'm trying to do a multimodal prompt with string and PDF. This is a snippet of my code:  

 

import google.generativeai as genai
model = genai.GenerativeModel(model_name='models/gemini-1.5-pro-latest',
                              generation_config=generation_config,
                              safety_settings=safety_settings)

with open("document.pdf", "rb") as pdf:
  pdf = pdf.read()
prompt_parts = [ context_text , pdf ]

response = model.generate_content(prompt_parts)

 

Assume all my configs etc are correct, the example was working fine for only the `context_text`

The error I get is:

 

---> response = model.generate_content(prompt_parts)
...
/usr/local/lib/python3.10/dist-packages/google/generativeai/types/content_types.py in to_blob(blob)
...
TypeError: Could not create `Blob`, expected `Blob`, `dict` or an `Image` type(`PIL.Image.Image` or `IPython.display.Image`).
Got a: <class 'bytes'>
...

 

Solved Solved
6 2 3,107
1 ACCEPTED SOLUTION

I believe you need to wrap your PDF into a `Part` object.

Something like the following:

import google.generativeai as genai
from vertexai.generative_models import Part

model = genai.GenerativeModel(model_name='models/gemini-1.5-pro-latest',
                              generation_config=generation_config,
                              safety_settings=safety_settings)

# NEW SECTION, LOAD INTO PART OBJECT
pdf_file_uri = "gs://path/to/your/document.pdf"
pdf = Part.from_uri(pdf_file_uri, mime_type="application/pdf")
###

prompt_parts = [ context_text , pdf ]

response = model.generate_content(prompt_parts)



View solution in original post

2 REPLIES 2