-
Notifications
You must be signed in to change notification settings - Fork 568
feat(ai): add single message truncation #5079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,30 @@ def get_start_span_function(): | |
| return sentry_sdk.start_span if transaction_exists else sentry_sdk.start_transaction | ||
|
|
||
|
|
||
| def _truncate_single_message(message, max_bytes): | ||
| # type: (Dict[str, Any], int) -> Dict[str, Any] | ||
| """ | ||
| Truncate a single message to fit within max_bytes. | ||
| If the message is too large, truncate the content field. | ||
| """ | ||
| if not isinstance(message, dict) or "content" not in message: | ||
| return message | ||
| content = message.get("content", "") | ||
|
|
||
| if not isinstance(content, str) or len(content) <= max_bytes: | ||
| return message | ||
|
|
||
| overhead_message = message.copy() | ||
| overhead_message["content"] = "" | ||
| overhead_size = len( | ||
| json.dumps(overhead_message, separators=(",", ":")).encode("utf-8") | ||
| ) | ||
|
|
||
| available_content_bytes = max_bytes - overhead_size - 20 | ||
| message["content"] = content[:available_content_bytes] + "..." | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: UTF-8 Truncation Mismatch Causes Encoding ErrorsThe truncation uses |
||
| return message | ||
|
|
||
|
|
||
| def _find_truncation_index(messages, max_bytes): | ||
| # type: (List[Dict[str, Any]], int) -> int | ||
| """ | ||
|
|
@@ -120,14 +144,20 @@ def _find_truncation_index(messages, max_bytes): | |
|
|
||
| def truncate_messages_by_size(messages, max_bytes=MAX_GEN_AI_MESSAGE_BYTES): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we instead check if the returned array has one element, and if so, truncate the single message? Let me know if I am missing some edge case that wouldn't be covered that way. |
||
| # type: (List[Dict[str, Any]], int) -> Tuple[List[Dict[str, Any]], int] | ||
| serialized_json = json.dumps(messages, separators=(",", ":")) | ||
| messages_with_truncated_content = [ | ||
| _truncate_single_message(msg, max_bytes) for msg in messages | ||
| ] | ||
|
|
||
| serialized_json = json.dumps(messages_with_truncated_content, separators=(",", ":")) | ||
| current_size = len(serialized_json.encode("utf-8")) | ||
shellmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if current_size <= max_bytes: | ||
| return messages, 0 | ||
| return messages_with_truncated_content, 0 | ||
|
|
||
| truncation_index = _find_truncation_index(messages, max_bytes) | ||
| return messages[truncation_index:], truncation_index | ||
| truncation_index = _find_truncation_index( | ||
| messages_with_truncated_content, max_bytes | ||
| ) | ||
| return messages_with_truncated_content[truncation_index:], truncation_index | ||
|
|
||
|
|
||
| def truncate_and_annotate_messages( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: In-place Mutation of Message Content Causes Side Effects
The function mutates the original
messagedict instead of creating a copy before modification. This causes unintended side effects where the caller's input data is modified. Similar tonormalize_message_roles, this function should create a copy of the message before modifying itscontentfield.