-
Notifications
You must be signed in to change notification settings - Fork 572
fix(integrations): langchain add multimodal content transformation functions for images, audio, and files #5278
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
Open
constantinius
wants to merge
7
commits into
master
Choose a base branch
from
constantinius/fix/integrations/langchain-report-binary-data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+363
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f32952
fix(ai): redact message parts content of type blob
constantinius 795bcea
fix(ai): skip non dict messages
constantinius a623e13
fix(ai): typing
constantinius 3d3ce5b
fix(ai): content items may not be dicts
constantinius c606b66
fix(integrations): langchain add multimodal content transformation fu…
constantinius c650799
fix(integrations): ensure URL check for data URIs handles empty strings
constantinius 71f2084
Merge branch 'master' into constantinius/fix/integrations/langchain-r…
constantinius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,124 @@ | |
| "top_p": SPANDATA.GEN_AI_REQUEST_TOP_P, | ||
| } | ||
|
|
||
| # Map LangChain content types to Sentry modalities | ||
| LANGCHAIN_TYPE_TO_MODALITY = { | ||
| "image": "image", | ||
| "image_url": "image", | ||
| "audio": "audio", | ||
| "video": "video", | ||
| "file": "document", | ||
| } | ||
|
|
||
|
|
||
| def _transform_langchain_content_block( | ||
| content_block: "Dict[str, Any]", | ||
| ) -> "Dict[str, Any]": | ||
| """ | ||
| Transform a LangChain content block to Sentry-compatible format. | ||
|
|
||
| Handles multimodal content (images, audio, video, documents) by converting them | ||
| to the standardized format: | ||
| - base64 encoded data -> type: "blob" | ||
| - URL references -> type: "uri" | ||
| - file_id references -> type: "file" | ||
| """ | ||
| if not isinstance(content_block, dict): | ||
| return content_block | ||
|
|
||
| block_type = content_block.get("type") | ||
|
|
||
| # Handle standard multimodal content types (image, audio, video, file) | ||
| if block_type in ("image", "audio", "video", "file"): | ||
| modality = LANGCHAIN_TYPE_TO_MODALITY.get(block_type, block_type) | ||
| mime_type = content_block.get("mime_type", "") | ||
|
|
||
| # Check for base64 encoded content | ||
| if "base64" in content_block: | ||
| return { | ||
| "type": "blob", | ||
| "modality": modality, | ||
| "mime_type": mime_type, | ||
| "content": content_block.get("base64", ""), | ||
| } | ||
| # Check for URL reference | ||
| elif "url" in content_block: | ||
| return { | ||
| "type": "uri", | ||
| "modality": modality, | ||
| "mime_type": mime_type, | ||
| "uri": content_block.get("url", ""), | ||
| } | ||
| # Check for file_id reference | ||
| elif "file_id" in content_block: | ||
| return { | ||
| "type": "file", | ||
| "modality": modality, | ||
| "mime_type": mime_type, | ||
| "file_id": content_block.get("file_id", ""), | ||
| } | ||
|
|
||
| # Handle legacy image_url format (OpenAI style) | ||
| elif block_type == "image_url": | ||
| image_url_data = content_block.get("image_url", {}) | ||
| if isinstance(image_url_data, dict): | ||
| url = image_url_data.get("url", "") | ||
| else: | ||
| url = str(image_url_data) | ||
|
|
||
| # Check if it's a data URI (base64 encoded) | ||
| if url and url.startswith("data:"): | ||
| # Parse data URI: data:mime_type;base64,content | ||
| try: | ||
| # Format: data:image/jpeg;base64,/9j/4AAQ... | ||
| header, content = url.split(",", 1) | ||
| mime_type = header.split(":")[1].split(";")[0] if ":" in header else "" | ||
|
Comment on lines
+189
to
+190
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. I'm worried about inconsistencies in the URI decoding across different AI integrations. Can this be centralized? |
||
| return { | ||
| "type": "blob", | ||
| "modality": "image", | ||
| "mime_type": mime_type, | ||
| "content": content, | ||
| } | ||
| except (ValueError, IndexError): | ||
| # If parsing fails, return as URI | ||
| return { | ||
| "type": "uri", | ||
| "modality": "image", | ||
| "mime_type": "", | ||
| "uri": url, | ||
| } | ||
| else: | ||
| # Regular URL | ||
| return { | ||
| "type": "uri", | ||
| "modality": "image", | ||
| "mime_type": "", | ||
| "uri": url, | ||
| } | ||
|
|
||
| # For text blocks and other types, return as-is | ||
| return content_block | ||
|
|
||
|
|
||
| def _transform_langchain_message_content(content: "Any") -> "Any": | ||
| """ | ||
| Transform LangChain message content, handling both string content and | ||
| list of content blocks. | ||
| """ | ||
| if isinstance(content, str): | ||
| return content | ||
|
|
||
| if isinstance(content, (list, tuple)): | ||
| transformed = [] | ||
| for block in content: | ||
| if isinstance(block, dict): | ||
| transformed.append(_transform_langchain_content_block(block)) | ||
| else: | ||
| transformed.append(block) | ||
| return transformed | ||
|
|
||
| return content | ||
|
|
||
|
|
||
| # Contextvar to track agent names in a stack for re-entrant agent support | ||
| _agent_stack: "contextvars.ContextVar[Optional[List[Optional[str]]]]" = ( | ||
|
|
@@ -234,7 +352,9 @@ def _handle_error(self, run_id: "UUID", error: "Any") -> None: | |
| del self.span_map[run_id] | ||
|
|
||
| def _normalize_langchain_message(self, message: "BaseMessage") -> "Any": | ||
| parsed = {"role": message.type, "content": message.content} | ||
| # Transform content to handle multimodal data (images, audio, video, files) | ||
| transformed_content = _transform_langchain_message_content(message.content) | ||
| parsed = {"role": message.type, "content": transformed_content} | ||
| parsed.update(message.additional_kwargs) | ||
| return parsed | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
langchain supports providing content in a provider-native format.
So, I'm wondering whether we are deliberately only handling langchain-standard blocks and OpenAI-formatted blocks?
See https://docs.langchain.com/oss/python/langchain/messages#message-content