Skip to content

Commit d6528cf

Browse files
authored
Fix image serialization for long image string (#348)
1 parent b50a65b commit d6528cf

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

ollama/_types.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,14 @@ def serialize_model(self):
166166
return b64encode(self.value.read_bytes() if isinstance(self.value, Path) else self.value).decode()
167167

168168
if isinstance(self.value, str):
169-
if Path(self.value).exists():
170-
return b64encode(Path(self.value).read_bytes()).decode()
169+
try:
170+
if Path(self.value).exists():
171+
return b64encode(Path(self.value).read_bytes()).decode()
172+
except Exception:
173+
# Long base64 string can't be wrapped in Path, so try to treat as base64 string
174+
pass
171175

176+
# String might be a file path, but might not exist
172177
if self.value.split('.')[-1] in ('png', 'jpg', 'jpeg', 'webp'):
173178
raise ValueError(f'File {self.value} does not exist')
174179

tests/test_type_serialization.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def test_image_serialization_base64_string():
1919
assert img.model_dump() == b64_str # Should return as-is if valid base64
2020

2121

22+
def test_image_serialization_long_base64_string():
23+
b64_str = 'dGVzdCBiYXNlNjQgc3RyaW5n' * 1000
24+
img = Image(value=b64_str)
25+
assert img.model_dump() == b64_str # Should return as-is if valid base64
26+
27+
2228
def test_image_serialization_plain_string():
2329
img = Image(value='not a path or base64')
2430
assert img.model_dump() == 'not a path or base64' # Should return as-is

0 commit comments

Comments
 (0)