Pre-encode all PIL images to base64 strings in parallel. Encodes images before multi-threaded API calls to avoid PIL thread-safety issues. Uses ``id``-based deduplication to encode each unique image object only once. Args: messages: List of message dict
(self, messages)
| 427 | return processed_msg |
| 428 | |
| 429 | def _pre_encode_images(self, messages): |
| 430 | """Pre-encode all PIL images to base64 strings in parallel. |
| 431 | |
| 432 | Encodes images before multi-threaded API calls to avoid PIL |
| 433 | thread-safety issues. Uses ``id``-based deduplication to encode |
| 434 | each unique image object only once. |
| 435 | |
| 436 | Args: |
| 437 | messages: List of message dicts containing potential PIL images. |
| 438 | Modified in place (PIL objects replaced with base64 strings). |
| 439 | """ |
| 440 | # Collect unique images |
| 441 | images_to_encode = {} # id -> image object |
| 442 | |
| 443 | for msg in messages: |
| 444 | if not isinstance(msg, list): |
| 445 | continue |
| 446 | for item in msg: |
| 447 | if not isinstance(item, dict): |
| 448 | continue |
| 449 | if item.get("type") in ("image", "image_url") and "value" in item: |
| 450 | value = item["value"] |
| 451 | if isinstance(value, list): |
| 452 | for img in value: |
| 453 | if isinstance(img, Image.Image): |
| 454 | images_to_encode[id(img)] = img |
| 455 | elif isinstance(value, Image.Image): |
| 456 | images_to_encode[id(value)] = value |
| 457 | |
| 458 | if not images_to_encode: |
| 459 | return |
| 460 | |
| 461 | if self.logger: |
| 462 | self.logger.info(f"Pre-encoding {len(images_to_encode)} images...") |
| 463 | |
| 464 | # Encode images in parallel |
| 465 | img_items = list(images_to_encode.items()) |
| 466 | encoded_cache = {} |
| 467 | |
| 468 | def encode_task(item): |
| 469 | img_id, img = item |
| 470 | return img_id, self.encode_image_directly(img) |
| 471 | |
| 472 | try: |
| 473 | with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: |
| 474 | futures = { |
| 475 | executor.submit(encode_task, item): item for item in img_items |
| 476 | } |
| 477 | |
| 478 | with tqdm( |
| 479 | total=len(img_items), desc="Encoding images", leave=False |
| 480 | ) as pbar: |
| 481 | for future in concurrent.futures.as_completed(futures): |
| 482 | img_id, encoded = future.result() |
| 483 | encoded_cache[img_id] = encoded |
| 484 | pbar.update(1) |
| 485 | except RuntimeError: |
| 486 | # Fallback: if thread creation still fails, encode sequentially |