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

This commit is contained in:
jif-oai authored and GitHub committed 2025-11-17 16:10:53 +00:00
1 parent 497fb4a19c
commit 7c8d333980
2 files changed
+30 -2

No files matched your search

+17 -2
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 { impl From<ResponseInputItem> for ResponseItem {
fn from(item: ResponseInputItem) -> Self { fn from(item: ResponseInputItem) -> Self {
match item { match item {
@@ -247,9 +260,10 @@ impl From<Vec<UserInput>> for ResponseInputItem {
image_url: image.into_data_url(), image_url: image.into_data_url(),
}, },
Err(err) => { Err(err) => {
tracing::warn!("Failed to resize image {}: {}", path.display(), err);
if matches!(&err, ImageProcessingError::Read { .. }) { if matches!(&err, ImageProcessingError::Read { .. }) {
local_image_error_placeholder(&path, &err) local_image_error_placeholder(&path, &err)
} else if err.is_invalid_image() {
invalid_image_error_placeholder(&path, &err)
} else { } else {
match std::fs::read(&path) { match std::fs::read(&path) {
Ok(bytes) => { Ok(bytes) => {
@@ -365,6 +379,7 @@ impl Serialize for FunctionCallOutputPayload {
where where
S: Serializer, S: Serializer,
{ {
tracing::error!("Payload: {:?}", self);
if let Some(items) = &self.content_items { if let Some(items) = &self.content_items {
items.serialize(serializer) items.serialize(serializer)
} else { } else {
@@ -452,7 +467,7 @@ fn convert_content_blocks_to_items(
) -> Option<Vec<FunctionCallOutputContentItem>> { ) -> Option<Vec<FunctionCallOutputContentItem>> {
let mut saw_image = false; let mut saw_image = false;
let mut items = Vec::with_capacity(blocks.len()); let mut items = Vec::with_capacity(blocks.len());
tracing::warn!("Blocks: {:?}", blocks);
for block in blocks { for block in blocks {
match block { match block {
ContentBlock::TextContent(text) => { ContentBlock::TextContent(text) => {
+13
View File
@@ -1,3 +1,4 @@
use image::ImageError;
use image::ImageFormat; use image::ImageFormat;
use std::path::PathBuf; use std::path::PathBuf;
use thiserror::Error; use thiserror::Error;
@@ -23,3 +24,15 @@ pub enum ImageProcessingError {
source: image::ImageError, source: image::ImageError,
}, },
} }
impl ImageProcessingError {
pub fn is_invalid_image(&self) -> bool {
matches!(
self,
ImageProcessingError::Decode {
source: ImageError::Decoding(_),
..
}
)
}
}