Created
December 25, 2025 01:31
-
-
Save rjurney/9d47b192d709703fe6f46e9cffb88033 to your computer and use it in GitHub Desktop.
A Christmas UUID to integer ID mapper for BAML - LLMs hate UUIDs, but love integers :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Optional | |
| class UUIDMapper: | |
| """Maps UUIDs to integer IDs for BAML processing and back.""" | |
| def __init__(self) -> None: | |
| """Initialize the UUID mapper.""" | |
| self.uuid_to_int: dict[str, int] = {} | |
| self.int_to_uuid: dict[int, str] = {} | |
| self.next_id = 1 | |
| def add_uuid(self, uuid: str) -> int: | |
| """ | |
| Add a UUID to the mapping and return its integer ID. | |
| Args: | |
| uuid: The UUID string to map | |
| Returns: | |
| The integer ID for this UUID | |
| """ | |
| if uuid not in self.uuid_to_int: | |
| self.uuid_to_int[uuid] = self.next_id | |
| self.int_to_uuid[self.next_id] = uuid | |
| self.next_id += 1 | |
| return self.uuid_to_int[uuid] | |
| def get_uuid(self, int_id: int) -> Optional[str]: | |
| """ | |
| Get the UUID for a given integer ID. | |
| Args: | |
| int_id: The integer ID to look up | |
| Returns: | |
| The UUID string, or None if not found | |
| """ | |
| return self.int_to_uuid.get(int_id) | |
| def map_ids_to_uuids(self, ids: Optional[list[int]]) -> Optional[list[str]]: | |
| """ | |
| Map a list of integer IDs back to UUIDs. | |
| Args: | |
| ids: List of integer IDs | |
| Returns: | |
| List of UUID strings, or None if input is None | |
| """ | |
| if ids is None: | |
| return None | |
| uuids = [] | |
| for id_val in ids: | |
| uuid = self.get_uuid(id_val) | |
| if uuid: | |
| uuids.append(uuid) | |
| else: | |
| logger.warning(f"No UUID found for integer ID {id_val}") | |
| return uuids if uuids else None | |
| def map_uuids_to_ids(self, uuids: Optional[list[str]]) -> Optional[list[int]]: | |
| """ | |
| Map a list of UUIDs to their integer IDs. | |
| Args: | |
| uuids: List of UUID strings | |
| Returns: | |
| List of integer IDs, or None if input is None | |
| """ | |
| if uuids is None: | |
| return None | |
| ids = [self.add_uuid(uuid) for uuid in uuids if uuid] | |
| return ids if ids else None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment