From 5d149e4d50325d2821263e59bac667f781c48f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Misi=C5=82o?= Date: Wed, 21 Jun 2023 10:25:49 +0200 Subject: [PATCH] Fix issue with non-list `To` header in GmailSendMessage Tool (#6242) Fixing the problem of feeding `str` instead of `List[str]` to the email tool. Fixes #6234 --------- Co-authored-by: Dev 2049 --- langchain/tools/gmail/send_message.py | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/langchain/tools/gmail/send_message.py b/langchain/tools/gmail/send_message.py index c8b76979..6a805b1a 100644 --- a/langchain/tools/gmail/send_message.py +++ b/langchain/tools/gmail/send_message.py @@ -2,7 +2,7 @@ import base64 from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, Union from pydantic import BaseModel, Field @@ -18,7 +18,7 @@ class SendMessageSchema(BaseModel): ..., description="The message to send.", ) - to: List[str] = Field( + to: Union[str, List[str]] = Field( ..., description="The list of recipients.", ) @@ -26,11 +26,11 @@ class SendMessageSchema(BaseModel): ..., description="The subject of the message.", ) - cc: Optional[List[str]] = Field( + cc: Optional[Union[str, List[str]]] = Field( None, description="The list of CC recipients.", ) - bcc: Optional[List[str]] = Field( + bcc: Optional[Union[str, List[str]]] = Field( None, description="The list of BCC recipients.", ) @@ -45,22 +45,22 @@ class GmailSendMessage(GmailBaseTool): def _prepare_message( self, message: str, - to: List[str], + to: Union[str, List[str]], subject: str, - cc: Optional[List[str]] = None, - bcc: Optional[List[str]] = None, + cc: Optional[Union[str, List[str]]] = None, + bcc: Optional[Union[str, List[str]]] = None, ) -> Dict[str, Any]: """Create a message for an email.""" mime_message = MIMEMultipart() mime_message.attach(MIMEText(message, "html")) - mime_message["To"] = ", ".join(to) + mime_message["To"] = ", ".join(to if isinstance(to, list) else [to]) mime_message["Subject"] = subject if cc is not None: - mime_message["Cc"] = ", ".join(cc) + mime_message["Cc"] = ", ".join(cc if isinstance(cc, list) else [cc]) if bcc is not None: - mime_message["Bcc"] = ", ".join(bcc) + mime_message["Bcc"] = ", ".join(bcc if isinstance(bcc, list) else [bcc]) encoded_message = base64.urlsafe_b64encode(mime_message.as_bytes()).decode() return {"raw": encoded_message} @@ -68,10 +68,10 @@ class GmailSendMessage(GmailBaseTool): def _run( self, message: str, - to: List[str], + to: Union[str, List[str]], subject: str, - cc: Optional[List[str]] = None, - bcc: Optional[List[str]] = None, + cc: Optional[Union[str, List[str]]] = None, + bcc: Optional[Union[str, List[str]]] = None, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Run the tool.""" @@ -90,10 +90,10 @@ class GmailSendMessage(GmailBaseTool): async def _arun( self, message: str, - to: List[str], + to: Union[str, List[str]], subject: str, - cc: Optional[List[str]] = None, - bcc: Optional[List[str]] = None, + cc: Optional[Union[str, List[str]]] = None, + bcc: Optional[Union[str, List[str]]] = None, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Run the tool asynchronously."""