Skip to content

Commit 217436b

Browse files
committed
fix image format
1 parent 7f23faf commit 217436b

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

lib/idp_common_pkg/idp_common/extraction/agentic_idp.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@
4747
view_todo_list,
4848
)
4949

50+
# Supported image formats for Bedrock API
51+
SUPPORTED_IMAGE_FORMATS = {"jpeg", "png", "gif", "webp"}
52+
53+
54+
def detect_image_format(image_bytes: bytes) -> str:
55+
"""
56+
Detect the image format from raw bytes.
57+
58+
Args:
59+
image_bytes: Raw image bytes
60+
61+
Returns:
62+
Image format string suitable for Bedrock API ('jpeg', 'png', 'gif', 'webp')
63+
64+
Raises:
65+
ValueError: If the image format is unsupported or cannot be detected
66+
"""
67+
img = Image.open(io.BytesIO(image_bytes))
68+
69+
if not img.format or img.format.lower() not in SUPPORTED_IMAGE_FORMATS:
70+
raise ValueError(
71+
f"Unsupported image format: {img.format}. "
72+
f"Supported formats: {', '.join(SUPPORTED_IMAGE_FORMATS)}"
73+
)
74+
return img.format.lower()
75+
76+
5077
# Use AWS Lambda Powertools Logger for structured logging
5178
# Automatically logs as JSON with Lambda context, request_id, timestamp, etc.
5279
# In Lambda: Full JSON structured logs
@@ -207,11 +234,15 @@ def view_image(image_index: int, agent: Agent) -> dict:
207234
# Get the base image (already has grid overlay)
208235
img_bytes = page_images[image_index]
209236

237+
# Detect actual image format from bytes
238+
img_format = detect_image_format(img_bytes)
239+
210240
logger.info(
211241
"Returning image to agent",
212242
extra={
213243
"image_index": image_index,
214244
"image_size_bytes": len(img_bytes),
245+
"image_format": img_format,
215246
},
216247
)
217248

@@ -220,7 +251,7 @@ def view_image(image_index: int, agent: Agent) -> dict:
220251
"content": [
221252
{
222253
"image": {
223-
"format": "png",
254+
"format": img_format,
224255
"source": {
225256
"bytes": img_bytes,
226257
},
@@ -775,12 +806,17 @@ def _prepare_prompt_content(
775806
extra={"image_count": len(page_images)},
776807
)
777808

778-
prompt_content += [
779-
ContentBlock(
780-
image=ImageContent(format="png", source=ImageSource(bytes=img_bytes))
809+
for img_bytes in page_images:
810+
# Detect actual image format from bytes
811+
img_format = detect_image_format(img_bytes)
812+
prompt_content.append(
813+
ContentBlock(
814+
image=ImageContent(
815+
format=img_format, # pyright: ignore[reportArgumentType]
816+
source=ImageSource(bytes=img_bytes),
817+
)
818+
)
781819
)
782-
for img_bytes in page_images
783-
]
784820

785821
# Add existing data context if provided
786822
if existing_data:

0 commit comments

Comments
 (0)