Skip to content

Commit 0d45ef0

Browse files
author
Daniel Lorch
committed
feat: convert image_uri to image bytes from custom lambda invocation
1 parent adb5417 commit 0d45ef0

File tree

1 file changed

+47
-0
lines changed
  • lib/idp_common_pkg/idp_common/extraction

1 file changed

+47
-0
lines changed

lib/idp_common_pkg/idp_common/extraction/service.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,46 @@ def _make_json_serializable(self, obj: Any) -> Any:
433433
# Convert non-serializable objects to string representation
434434
return str(obj)
435435

436+
def _convert_image_uris_to_bytes_in_content(
437+
self, content: list[dict[str, Any]]
438+
) -> list[dict[str, Any]]:
439+
"""
440+
Convert image URIs back to bytes in content array after Lambda processing.
441+
442+
Args:
443+
content: Content array from Lambda that may contain image URIs
444+
445+
Returns:
446+
Content array with image bytes restored
447+
"""
448+
converted_content = []
449+
450+
for item in content:
451+
if "image_uri" in item:
452+
image_uri = item["image_uri"]
453+
454+
# Load image content
455+
if image_uri.startswith("s3://"):
456+
# Direct S3 URI
457+
logger.info(f"Retrieving image {image_uri}")
458+
image_bytes = s3.get_binary_content(image_uri)
459+
else:
460+
raise ValueError(
461+
f"Invalid file path {image_uri} - expecting S3 path"
462+
)
463+
464+
converted_item = image.prepare_bedrock_image_attachment(image_bytes)
465+
elif "image" in item:
466+
# Keep existing image objects as-is
467+
converted_item = item.copy()
468+
else:
469+
# Keep non-image items as-is
470+
converted_item = item.copy()
471+
472+
converted_content.append(converted_item)
473+
474+
return converted_content
475+
436476
def _invoke_custom_prompt_lambda(
437477
self, lambda_arn: str, payload: dict[str, Any]
438478
) -> dict[str, Any]:
@@ -486,6 +526,13 @@ def _invoke_custom_prompt_lambda(
486526
logger.error(error_msg)
487527
raise Exception(error_msg)
488528

529+
# Convert image URIs to bytes in the response
530+
result["task_prompt_content"] = (
531+
self._convert_image_uris_to_bytes_in_content(
532+
result["task_prompt_content"]
533+
)
534+
)
535+
489536
return result
490537

491538
except Exception as e:

0 commit comments

Comments
 (0)