-
Notifications
You must be signed in to change notification settings - Fork 327
fix(community): Handle UnicodeDecodeError in GmailSearch and ensure header robustness
#1226
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
Merged
lkuligin
merged 5 commits into
langchain-ai:main
from
RONAK-AI647:RONAK-A1647/fix(gmail)-Handle-UnicodeDecodeError-in-GmailSearch-and-ensure-header-robustness-(Fixes-#1030)-
Oct 10, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f16776f
update search.py
RONAK-AI647 c53d6b9
mypy error fix for .decode
RONAK-AI647 05f2c22
Update search.py
RONAK-AI647 654cfdc
update serach.py to a better pythonic approach
RONAK-AI647 8d8a26a
Merge branch 'main' into RONAK-A1647/fix(gmail)-Handle-UnicodeDecodeE…
RONAK-AI647 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 |
|---|---|---|
|
|
@@ -89,8 +89,8 @@ def _parse_messages(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any] | |
|
|
||
| email_msg = email.message_from_bytes(raw_message) | ||
|
|
||
| subject = email_msg["Subject"] | ||
| sender = email_msg["From"] | ||
| subject = email_msg.get("Subject", "") | ||
| sender = email_msg.get("From", "") | ||
|
|
||
| message_body = "" | ||
| if email_msg.is_multipart(): | ||
|
|
@@ -99,18 +99,22 @@ def _parse_messages(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any] | |
| cdispo = str(part.get("Content-Disposition")) | ||
| if ctype == "text/plain" and "attachment" not in cdispo: | ||
| try: | ||
| message_body = part.get_payload(decode=True).decode("utf-8") # type: ignore[union-attr] | ||
| message_body = part.get_payload(decode=True).decode( # type: ignore[union-attr] | ||
| "utf-8", errors="replace" | ||
| ) | ||
| except UnicodeDecodeError: | ||
| message_body = part.get_payload(decode=True).decode( # type: ignore[union-attr] | ||
| "latin-1" | ||
| "latin-1", errors="replace" | ||
| ) | ||
| break | ||
| else: | ||
| try: | ||
| message_body = email_msg.get_payload(decode=True).decode("utf-8") # type: ignore[union-attr] | ||
| message_body = email_msg.get_payload(decode=True).decode( # type: ignore[union-attr] | ||
| "utf-8", errors="replace" | ||
| ) | ||
| except UnicodeDecodeError: | ||
| message_body = email_msg.get_payload(decode=True).decode( # type: ignore[union-attr] | ||
| "latin-1" | ||
| "latin-1", errors="replace" | ||
| ) | ||
|
Comment on lines
101
to
118
|
||
|
|
||
| body = clean_email_body(message_body) | ||
|
|
||
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.
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.
The fallback to latin-1 encoding with errors='replace' is redundant since the primary utf-8 decode now also uses errors='replace'. Consider removing this exception handling block or using a different fallback encoding strategy.