Using Office365 SearchEmailsInput in LangChain

Posted: Feb 1, 2025.

When working with Office365 email integration in LangChain, the SearchEmailsInput class provides a structured way to search and filter emails using Microsoft Graph API queries. This guide will show you how to effectively use this class to search through your Office365 emails.

What is SearchEmailsInput?

SearchEmailsInput is a Pydantic model class that defines the input parameters for searching emails in Office365 using LangChain. It provides a structured way to specify search criteria, including query filters, folder selection, and result limitations.

Reference

Here are the available parameters for SearchEmailsInput:

ParameterTypeDefaultDescription
querystrRequiredMicrosoft Graph v1.0 search query string
folderstrNoneTarget folder to search in (e.g., "inbox", "drafts")
max_resultsint10Maximum number of results to return
truncateboolTrueWhether to truncate email body content

How to Use SearchEmailsInput

Let's explore different ways to use SearchEmailsInput for searching emails.

Basic Search Query

from langchain_community.tools.office365.messages_search import SearchEmailsInput

# Create a basic search for emails from a specific sender
search_input = SearchEmailsInput(
    query="from:john.doe@company.com"
)

Advanced Filtering

# Search with multiple criteria
search_input = SearchEmailsInput(
    query="from:amy OR from:david subject:meeting received:2023-06-08..2023-06-09",
    max_results=5,
    folder="inbox"
)

Search with Attachments Filter

# Search for emails with specific attachments
search_input = SearchEmailsInput(
    query="hasAttachments:true attachment:report.pdf",
    truncate=False  # Don't truncate email bodies
)

Combined Search Parameters

# Complex search combining multiple filters
search_input = SearchEmailsInput(
    query="importance:high subject:urgent received>2023-01-01",
    folder="inbox",
    max_results=20,
    truncate=True
)

Query Syntax Examples

The query parameter supports various search filters. Here are some useful examples:

  • Date Filtering:

    # Emails received in a date range
    SearchEmailsInput(query="received:2023-06-08..2023-06-09")
    
    # Emails sent after a specific date
    SearchEmailsInput(query="sent>2023-01-01")
  • Recipient Filtering:

    # Search by recipients
    SearchEmailsInput(query="to:recipient@company.com")
    
    # Search by CC
    SearchEmailsInput(query="cc:person@company.com")
  • Content Filtering:

    # Search in email body
    SearchEmailsInput(query="body:important meeting")
    
    # Search by subject
    SearchEmailsInput(query="subject:quarterly report")

Best Practices

  1. Set truncate=True when dealing with large email bodies to avoid token limits
  2. Use specific folders when possible to narrow down search scope
  3. Combine multiple filters to create precise search queries
  4. Use reasonable max_results values to manage response size

Remember that the actual email search will need to be performed using an Office365 email tool that accepts this SearchEmailsInput instance. This class only defines the search parameters and doesn't perform the actual search operation.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs