-
Notifications
You must be signed in to change notification settings - Fork 572
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: master
Are you sure you want to change the base?
Changes from all commits
1f32952
795bcea
a623e13
3d3ce5b
36fcaf9
d9d1264
4a17806
db071c2
280202f
97cc614
8cde746
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 |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import copy | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk import consts | ||
| from sentry_sdk.ai.monitoring import record_token_usage | ||
| from sentry_sdk.ai.utils import ( | ||
| get_start_span_function, | ||
| parse_data_uri, | ||
| set_data_normalized, | ||
| truncate_and_annotate_messages, | ||
| ) | ||
|
|
@@ -14,7 +16,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: | ||
|
|
@@ -36,6 +38,79 @@ 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,..." | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
| # Deep copy to avoid mutating original messages from kwargs | ||
| messages = copy.deepcopy(messages) | ||
|
|
||
| def _map_item(item: "Dict[str, Any]") -> "Dict[str, Any]": | ||
| if not isinstance(item, dict): | ||
| return item | ||
| if item.get("type") == "image_url": | ||
| image_url = item.get("image_url") or {} | ||
| url = image_url.get("url", "") | ||
| if url.startswith("data:"): | ||
| try: | ||
| mime_type, content = parse_data_uri(url) | ||
| return { | ||
| "type": "blob", | ||
| "modality": "image", | ||
| "mime_type": mime_type, | ||
| "content": 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. Non-base64 data URIs incorrectly converted to blob typeMedium Severity The |
||
| except ValueError: | ||
| pass | ||
| if url: | ||
| return { | ||
| "type": "uri", | ||
| "uri": url, | ||
| } | ||
constantinius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return item | ||
|
|
||
| for message in messages: | ||
| if not isinstance(message, dict): | ||
| continue | ||
| content = message.get("content") | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if isinstance(content, list): | ||
| message["content"] = [_map_item(item) for item in content] | ||
constantinius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return messages | ||
|
|
||
|
|
||
| def _input_callback(kwargs: "Dict[str, Any]") -> None: | ||
| """Handle the start of a request.""" | ||
| integration = sentry_sdk.get_client().get_integration(LiteLLMIntegration) | ||
|
|
@@ -102,6 +177,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( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.