fix(qwen_vl_api): Fix temporary files being deleted prematurely for multi-image inputs #856
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.
📝 Description
This PR fixes a bug in the
qwen_vl_api
model wrapper where requests with multiple images would fail because temporary image files were being deleted prematurely.🐛 The Bug
The previous implementation used
tempfile.NamedTemporaryFile(delete=True)
to create a temporary file for each image within a loop. The variable holding the file object (temp_file
) was reassigned in each iteration.This caused the file objects from previous iterations to lose all references, making them eligible for Python's garbage collection. When the garbage collector reclaimed an object, its file handle was closed, and because
delete=True
was set, the underlying file was immediately deleted from the disk.As a result, only the temporary file for the very last image in the loop would persist, leading to "file not found" errors when the API call was made.
✅ The Fix
This fix addresses the issue by implementing the following changes:
temp_files
) is introduced to hold references to allNamedTemporaryFile
objects created during the loop. This prevents them from being garbage collected until they are no longer needed.try...finally
block. Thefinally
block ensures that.close()
is called on every file object in thetemp_files
list, regardless of whether the API call succeeded or failed. This guarantees that all temporary files are properly cleaned up, preventing file leaks.This fix ensures the stability and reliability of the model when handling complex, multi-image inputs.