from typing import TypeVar T = TypeVar("T") def truncate_output(text: str, max_bytes: int) -> str: encoded = text.encode("utf-8") if len(encoded) <= max_bytes: return text return encoded[:max_bytes].decode("utf-8", errors="ignore") + "\n...[truncated]" def is_list_of( value: object, typ: type[T], ) -> bool: """Check if the value is a list of the given type.""" return isinstance(value, list) and all(isinstance(item, typ) for item in value) def find_common_prefix(s1: str, s2: str) -> str: """Finds a common prefix that is shared between two strings. This function is provided as a UTILITY for extracting information from JSON generated by partial_json_parser, to help in ensuring that the right tokens are returned in streaming, so that close-quotes, close-brackets and close-braces are not returned prematurely. e.g. find_common_prefix('{"fruit": "ap"}', '{"fruit": "apple"}') -> '{"fruit": "ap' """ prefix = "" min_length = min(len(s1), len(s2)) for i in range(0, min_length): if s1[i] == s2[i]: prefix += s1[i] else: break return prefix