-
Notifications
You must be signed in to change notification settings - Fork 799
Bugfix/image encoding #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ceac123
Fix image serialization
ParthSareen b1af792
Fix tests
ParthSareen ec88ad4
Commented tests
ParthSareen d712676
Remove pytest global fixture
ParthSareen 899a7c2
Fix generate tests
ParthSareen 1c073e4
Handle edge case for path strings getting decoded as b64
ParthSareen a408678
Update tests, removed silent failure
ParthSareen f54c9dc
Updated error handling to manage edge case, updated tests
ParthSareen f2f413e
Undo test changes
ParthSareen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,48 @@ | ||
from base64 import b64decode, b64encode | ||
|
||
from base64 import b64encode | ||
from pathlib import Path | ||
|
||
import pytest | ||
from ollama._types import Image | ||
import tempfile | ||
|
||
|
||
def test_image_serialization(): | ||
# Test bytes serialization | ||
def test_image_serialization_bytes(): | ||
image_bytes = b'test image bytes' | ||
encoded_string = b64encode(image_bytes).decode() | ||
img = Image(value=image_bytes) | ||
assert img.model_dump() == b64encode(image_bytes).decode() | ||
assert img.model_dump() == encoded_string | ||
|
||
|
||
# Test base64 string serialization | ||
def test_image_serialization_base64_string(): | ||
b64_str = 'dGVzdCBiYXNlNjQgc3RyaW5n' | ||
img = Image(value=b64_str) | ||
assert img.model_dump() == b64decode(b64_str).decode() | ||
assert img.model_dump() == b64_str # Should return as-is if valid base64 | ||
|
||
|
||
def test_image_serialization_plain_string(): | ||
img = Image(value='not a path or base64') | ||
assert img.model_dump() == 'not a path or base64' # Should return as-is | ||
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently just a passthrough. Since spaces are valid b64 I'm hesitant to split on it and check the length. although we could do a heuristic to catch it greedily |
||
|
||
|
||
def test_image_serialization_path(): | ||
with tempfile.NamedTemporaryFile() as temp_file: | ||
temp_file.write(b'test file content') | ||
temp_file.flush() | ||
img = Image(value=Path(temp_file.name)) | ||
assert img.model_dump() == b64encode(b'test file content').decode() | ||
|
||
|
||
def test_image_serialization_string_path(): | ||
with tempfile.NamedTemporaryFile() as temp_file: | ||
temp_file.write(b'test file content') | ||
temp_file.flush() | ||
img = Image(value=temp_file.name) | ||
assert img.model_dump() == b64encode(b'test file content').decode() | ||
|
||
with pytest.raises(ValueError): | ||
img = Image(value='some_path/that/does/not/exist.png') | ||
img.model_dump() | ||
|
||
with pytest.raises(ValueError): | ||
img = Image(value='not an image') | ||
img.model_dump() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to handle the edge case where a path does not exist but could be a valid b64 string - would lead to weird behavior