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 <dev.dev2049@gmail.com>
This commit is contained in:
Jakub Misiło 2023-06-21 10:25:49 +02:00 committed by GitHub
parent 94c7899257
commit 5d149e4d50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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."""