-
Notifications
You must be signed in to change notification settings - Fork 569
fix(litellm): fix gen_ai.request.messages to be as expected
#5255
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: constantinius/fix/redact-message-parts-type-blob
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 |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| from sentry_sdk.utils import event_from_exception | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Dict | ||
| from typing import Any, Dict, List | ||
| from datetime import datetime | ||
|
|
||
| try: | ||
|
|
@@ -35,6 +35,68 @@ def _get_metadata_dict(kwargs: "Dict[str, Any]") -> "Dict[str, Any]": | |
| return metadata | ||
|
|
||
|
|
||
| def _convert_message_parts(messages: "List[Dict[str, Any]]") -> "List[Dict[str, Any]]": | ||
| """ | ||
| Convert the message parts from OpenAI format to the `gen_ai.request.messages` format. | ||
| e.g: | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "text": "How many ponies do you see in the image?", | ||
| "type": "text" | ||
| }, | ||
| { | ||
| "type": "image_url", | ||
| "image_url": { | ||
| "url": "data:image/jpeg;base64,...", | ||
| "detail": "high" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| becomes: | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "text": "How many ponies do you see in the image?", | ||
| "type": "text" | ||
| }, | ||
| { | ||
| "type": "blob", | ||
| "modality": "image", | ||
| "mime_type": "image/jpeg", | ||
| "content": "data:image/jpeg;base64,..." | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
|
|
||
| def _map_item(item: "Dict[str, Any]") -> "Dict[str, Any]": | ||
| if item.get("type") == "image_url": | ||
| image_url = item.get("image_url") or {} | ||
| if image_url.get("url", "").startswith("data:"): | ||
| return { | ||
| "type": "blob", | ||
| "modality": "image", | ||
| "mime_type": item["image_url"]["url"].split(";base64,")[0], | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
| "content": item["image_url"]["url"].split(";base64,")[1], | ||
|
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: IndexError when data URL lacks base64 encoding markerThe code assumes all data URLs contain |
||
| } | ||
|
Comment on lines
+84
to
+85
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: The data URI parsing in 🔍 Detailed AnalysisIn the 💡 Suggested FixImplement robust parsing for the data URI. First, validate that the 🤖 Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| else: | ||
| return { | ||
| "type": "uri", | ||
| "uri": item["image_url"]["url"], | ||
|
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: Unsafe direct access bypasses null-safe variable checkLine 78 creates a null-safe Additional Locations (1) |
||
| } | ||
|
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: Else branch crashes when
|
||
| return item | ||
|
|
||
| for message in messages: | ||
| content = message.get("content") | ||
| if isinstance(content, list): | ||
| message["content"] = [_map_item(item) for item in content] | ||
|
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: Function mutates original kwargs messages corrupting API requestThe |
||
| return messages | ||
|
|
||
|
|
||
| def _input_callback(kwargs: "Dict[str, Any]") -> None: | ||
| """Handle the start of a request.""" | ||
| integration = sentry_sdk.get_client().get_integration(LiteLLMIntegration) | ||
|
|
@@ -101,6 +163,7 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: | |
| messages = kwargs.get("messages", []) | ||
| if messages: | ||
| scope = sentry_sdk.get_current_scope() | ||
| messages = _convert_message_parts(messages) | ||
| messages_data = truncate_and_annotate_messages(messages, span, scope) | ||
| if messages_data is not None: | ||
| set_data_normalized( | ||
|
|
||
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: Incorrect mime_type includes
data:prefixThe
mime_typeextraction is incorrect. Usingsplit(";base64,")[0]on a URL like"data:image/jpeg;base64,..."returns"data:image/jpeg"instead of"image/jpeg"as documented in the function's docstring. The"data:"prefix needs to be removed to get the proper MIME type.