feat: placeholder for image that can't be decoded to prevent 400 (#6773)

This commit is contained in:
jif-oai
2025-11-17 17:10:53 +01:00
committed by GitHub
parent 497fb4a19c
commit 7c8d333980
2 changed files with 30 additions and 2 deletions

View File

@@ -158,6 +158,19 @@ fn local_image_error_placeholder(
}
}
fn invalid_image_error_placeholder(
path: &std::path::Path,
error: impl std::fmt::Display,
) -> ContentItem {
ContentItem::InputText {
text: format!(
"Image located at `{}` is invalid: {}",
path.display(),
error
),
}
}
impl From<ResponseInputItem> for ResponseItem {
fn from(item: ResponseInputItem) -> Self {
match item {
@@ -247,9 +260,10 @@ impl From<Vec<UserInput>> for ResponseInputItem {
image_url: image.into_data_url(),
},
Err(err) => {
tracing::warn!("Failed to resize image {}: {}", path.display(), err);
if matches!(&err, ImageProcessingError::Read { .. }) {
local_image_error_placeholder(&path, &err)
} else if err.is_invalid_image() {
invalid_image_error_placeholder(&path, &err)
} else {
match std::fs::read(&path) {
Ok(bytes) => {
@@ -365,6 +379,7 @@ impl Serialize for FunctionCallOutputPayload {
where
S: Serializer,
{
tracing::error!("Payload: {:?}", self);
if let Some(items) = &self.content_items {
items.serialize(serializer)
} else {
@@ -452,7 +467,7 @@ fn convert_content_blocks_to_items(
) -> Option<Vec<FunctionCallOutputContentItem>> {
let mut saw_image = false;
let mut items = Vec::with_capacity(blocks.len());
tracing::warn!("Blocks: {:?}", blocks);
for block in blocks {
match block {
ContentBlock::TextContent(text) => {

View File

@@ -1,3 +1,4 @@
use image::ImageError;
use image::ImageFormat;
use std::path::PathBuf;
use thiserror::Error;
@@ -23,3 +24,15 @@ pub enum ImageProcessingError {
source: image::ImageError,
},
}
impl ImageProcessingError {
pub fn is_invalid_image(&self) -> bool {
matches!(
self,
ImageProcessingError::Decode {
source: ImageError::Decoding(_),
..
}
)
}
}