(
image_paths: Union[str, List[str], None],
)
| 28 | |
| 29 | |
| 30 | def prepare_images_for_prompt( |
| 31 | image_paths: Union[str, List[str], None], |
| 32 | ) -> List[Dict[str, Any]]: |
| 33 | if not image_paths: |
| 34 | return [] |
| 35 | |
| 36 | # Ensure it's a list format |
| 37 | if isinstance(image_paths, str): |
| 38 | image_paths = [image_paths] |
| 39 | |
| 40 | image_data = [] |
| 41 | for image_path in image_paths: |
| 42 | if not image_path or not image_path.strip(): |
| 43 | continue |
| 44 | |
| 45 | path = Path(image_path) |
| 46 | if not path.exists(): |
| 47 | print(f"⚠️ Warning: Image file not found: {image_path}") |
| 48 | continue |
| 49 | |
| 50 | try: |
| 51 | base64_image = encode_image_to_base64(image_path) |
| 52 | mime_type = get_image_mime_type(image_path) |
| 53 | |
| 54 | image_info = { |
| 55 | "type": "image_url", |
| 56 | "image_url": {"url": f"data:{mime_type};base64,{base64_image}"}, |
| 57 | } |
| 58 | image_data.append(image_info) |
| 59 | |
| 60 | except Exception as e: |
| 61 | print(f"⚠️ Warning: Failed to process image {image_path}: {e}") |
| 62 | continue |
| 63 | |
| 64 | return image_data |
| 65 | |
| 66 | |
| 67 | def normalize_image_paths(image_paths_field: Any) -> Optional[List[str]]: |
no test coverage detected