Skip to content

Conversation

@genevera
Copy link

@genevera genevera commented Dec 8, 2025

Improve image compression by adding error handling for unsupported media types and enhancing logging for failures. Adjust parameters for requests to improve performance and reliability.

Copilot AI review requested due to automatic review settings December 8, 2025 18:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to enhance image compression handling by adding null checks, error logging, and improving media type validation. It also adjusts LLM request parameters (temperature, min_p, top_p) for OpenAI-compatible backends and adds trailing commas to Ollama options objects.

Key Changes:

  • Added null safety checks and error logging to the CompressImageForVision method
  • Updated media type validation to check for MediaMetaType.Image and MediaMetaType.Animation
  • Modified OpenAI-compatible request parameters: increased temperature to 1.1, added min_p=0.05, and adjusted top_p values

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Image image = Image.FromDataString($"data:{media.MediaType};base64,{media.Data}");
// Skip compression for videos etc..
if (image.Type != Image.ImageType.IMAGE)
Image image = Image.FromDataString($"data:{media.MediaType};base64,{media.Data}") as SwarmUI.Utils.Image;
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The redundant cast as SwarmUI.Utils.Image is unnecessary since Image.FromDataString() already returns a SwarmUI.Utils.Image type (as defined by the using alias on line 4). This cast will always succeed or return null, making the null check on line 71 potentially misleading about the actual failure mode.

Suggested change
Image image = Image.FromDataString($"data:{media.MediaType};base64,{media.Data}") as SwarmUI.Utils.Image;
Image image = Image.FromDataString($"data:{media.MediaType};base64,{media.Data}");

Copilot uses AI. Check for mistakes.
Comment on lines +91 to +98
var imageFile = new Image(img) as SwarmUI.Media.ImageFile;
if (imageFile == null)
{
Logs.Error("Failed to convert image to ImageFile.");
return media.Data;
}
var compressedImage = imageFile.ConvertTo(targetFormat, quality: quality);
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast from Image to SwarmUI.Media.ImageFile appears problematic. Based on line 4, Image is aliased to SwarmUI.Utils.Image, not SwarmUI.Media.ImageFile. This cast will likely always fail and return null, causing the compression to always fall back to returning the original uncompressed data. The previous code created a new Image and called ConvertTo directly on it, which was the correct approach.

Suggested change
var imageFile = new Image(img) as SwarmUI.Media.ImageFile;
if (imageFile == null)
{
Logs.Error("Failed to convert image to ImageFile.");
return media.Data;
}
var compressedImage = imageFile.ConvertTo(targetFormat, quality: quality);
var compressedImage = image.ConvertTo(targetFormat, quality: quality);

Copilot uses AI. Check for mistakes.
BackendSchema.cs Outdated
temperature = 1.0,
temperature = 1.1,
min_p=0.05,
top_p=0.95,
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after the equals sign. Should be top_p = 0.95 for consistency with the spacing used in other parameters.

Suggested change
top_p=0.95,
top_p = 0.95,

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@kalebbroo
Copy link
Collaborator

Branch has merge conflicts?

@genevera
Copy link
Author

Yeah sorry about that. I vibe coded this when the compile was failing under v0.9.7.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants