@@ -107,8 +107,8 @@ def get_start_span_function():
107107def _truncate_single_message_content_if_present (message , max_chars ):
108108 # type: (Dict[str, Any], int) -> Dict[str, Any]
109109 """
110- Truncate a single message to fit within max_chars.
111- If the message is too large, truncate the content field .
110+ Truncate a message's content to at most ` max_chars` characters and append an
111+ ellipsis if truncation occurs .
112112 """
113113 if not isinstance (message , dict ) or "content" not in message :
114114 return message
@@ -146,10 +146,13 @@ def truncate_messages_by_size(
146146 # type: (List[Dict[str, Any]], int, int) -> Tuple[List[Dict[str, Any]], int]
147147 """
148148 Returns a truncated messages array, consisting of
149- - the last message, with the messages's content truncated to `max_single_message_chars` characters,
149+ - the last message, with its content truncated to `max_single_message_chars` characters,
150150 if the last message's size exceeds `max_bytes`; otherwise,
151151 - the maximum number of messages, starting from the end of the `messages` array, whose total
152152 serialized size does not exceed `max_bytes` bytes.
153+
154+ In the single message case, the serialized message size may exceed `max_bytes`, because
155+ truncation is based only on character count in that case.
153156 """
154157 serialized_json = json .dumps (messages , separators = ("," , ":" ))
155158 current_size = len (serialized_json .encode ("utf-8" ))
@@ -158,11 +161,12 @@ def truncate_messages_by_size(
158161 return messages , 0
159162
160163 truncation_index = _find_truncation_index (messages , max_bytes )
161- truncated_messages = (
162- messages [truncation_index :]
163- if truncation_index < len (messages )
164- else messages [- 1 :]
165- )
164+ if truncation_index < len (messages ):
165+ truncated_messages = messages [truncation_index :]
166+ else :
167+ truncation_index = len (messages ) - 1
168+ truncated_messages = messages [- 1 :]
169+
166170 if len (truncated_messages ) == 1 :
167171 truncated_messages [0 ] = _truncate_single_message_content_if_present (
168172 deepcopy (truncated_messages [0 ]), max_chars = max_single_message_chars
0 commit comments