Closed Sjlver closed 2 months ago
Here is some code which tries to reproduce the issue:
import base64
import litellm
image_file = Path("tmp.jpg")
encoded_string = base64.b64encode(open(image_file, 'rb').read()).decode("utf-8")
mime_type = "image/jpeg"
image_url = f"data:{mime_type};base64,{encoded_string}"
messages = [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_url,
"detail": "high"
}
}
]
},
]
litellm.token_counter(model="gpt-4o", messages=messages)
But it throws a different error:
Traceback (most recent call last):
File "/Users/gauthier/Projects/aider/./tmp.py", line 53, in <module>
litellm.token_counter(model="gpt-4o", messages=messages)
File "/Users/gauthier/Projects/aider/.venv/lib/python3.12/site-packages/litellm/utils.py", line 1994, in token_counter
num_tokens += calculage_img_tokens(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/gauthier/Projects/aider/.venv/lib/python3.12/site-packages/litellm/utils.py", line 1840, in calculage_img_tokens
width, height = get_image_dimensions(data=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/gauthier/Projects/aider/.venv/lib/python3.12/site-packages/litellm/utils.py", line 1823, in get_image_dimensions
height, width = struct.unpack(">HH", sof.group()[5:9])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
struct.error: unpack requires a buffer of 4 bytes
Here is a the small tmp.jpg
file:
Thanks for the rapid fix! 💙
What happened?
I encounter a problem when using litellm through aider.chat... I've initially described this here: https://github.com/paul-gauthier/aider/issues/1088, but it is probably more of a litellm issue than an aider issue.
I get the following error message after having added a .jpg image to the chat:
My suspicion is that litellm couldn't handle my perfectly valid jpg file. Here's the code that computes width and height:
It is possible that my JPG file does not have the corresponding marker? I appreciate it that litellm tries to reduce its dependencies... but it might make sense to use an image library for this :)
You can find the jpg file at https://blog.purpureus.net/assets/blog/personal_key_rotation/simplified-asset-graph.jpg
Relevant log output
No response
Twitter / LinkedIn details
@sjlver