Skip to content

Commit

Permalink
Fix issue with non-list To header in GmailSendMessage Tool (#6242)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jmisilo and dev2049 committed Jun 21, 2023
1 parent 94c7899 commit 5d149e4
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions langchain/tools/gmail/send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,19 +18,19 @@ class SendMessageSchema(BaseModel):
...,
description="The message to send.",
)
to: List[str] = Field(
to: Union[str, List[str]] = Field(
...,
description="The list of recipients.",
)
subject: str = Field(
...,
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.",
)
Expand All @@ -45,33 +45,33 @@ 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}

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."""
Expand All @@ -90,10 +90,10 @@ def _run(
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."""
Expand Down

1 comment on commit 5d149e4

@vercel
Copy link

@vercel vercel bot commented on 5d149e4 Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchain – ./

langchain-git-master-langchain.vercel.app
langchain-langchain.vercel.app
python.langchain.com

Please sign in to comment.