Skip to content

[Bug] AI chat fails to search Microsoft/Outlook inbox #2195

Description

@lukoerfer

When using the chat assistant to search emails on a Microsoft/Outlook account, the request fails with a BadRequest error:

web-1   | [chat]: Building Outlook request {
web-1   |   "maxResults": 5,
web-1   |   "hasSearchQuery": true,
web-1   |   "hasDateFilters": false,
web-1   |   "queryWasSanitized": true
web-1   | } {
web-1   |   "requestId": "d41c81fa-3b3d-4df7-8a1c-8a2fd5da40d8",
web-1   |   "url": "https://837b90b9570e:3000/api/chat",
web-1   |   "userId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "emailAccountId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "email": "xxxxx@xxxxxxx.com",
web-1   |   "provider": "microsoft"
web-1   | }
web-1   | [chat]: Using search path {
web-1   |   "rawSearchQuery": "from:\"John Doe\"",
web-1   |   "effectiveSearchQuery": "from:\"\\\"John Doe\\\"\"",
web-1   |   "queryWasSanitized": true
web-1   | } {
web-1   |   "requestId": "d41c81fa-3b3d-4df7-8a1c-8a2fd5da40d8",
web-1   |   "url": "https://837b90b9570e:3000/api/chat",
web-1   |   "userId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "emailAccountId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "email": "xxxxx@xxxxxxx.com",
web-1   |   "provider": "microsoft"
web-1   | }
web-1   | [chat]: Non-retryable error encountered {
web-1   |   "error": {
web-1   |     "error": "Syntax error: character ':' is not valid at position 4 in 'from:\"\\\"John Doe\\\"\"'.",
web-1   |     "attemptNumber": 1,
web-1   |     "retriesLeft": 5,
web-1   |     "retriesConsumed": 0,
web-1   |     "retryDelay": 1000
web-1   |   },
web-1   |   "code": "BadRequest"
web-1   | } {
web-1   |   "requestId": "d41c81fa-3b3d-4df7-8a1c-8a2fd5da40d8",
web-1   |   "url": "https://837b90b9570e:3000/api/chat",
web-1   |   "userId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "emailAccountId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "email": "xxxxx@xxxxxxx.com",
web-1   |   "provider": "microsoft"
web-1   | }
web-1   | [chat]: Failed to search inbox {
web-1   |   "error": {
web-1   |     "error": "Syntax error: character ':' is not valid at position 4 in 'from:\"\\\"John Doe\\\"\"'.",
web-1   |     "attemptNumber": 1,
web-1   |     "retriesLeft": 5,
web-1   |     "retriesConsumed": 0,
web-1   |     "retryDelay": 1000
web-1   |   }
web-1   | } {
web-1   |   "requestId": "d41c81fa-3b3d-4df7-8a1c-8a2fd5da40d8",
web-1   |   "url": "https://837b90b9570e:3000/api/chat",
web-1   |   "userId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "emailAccountId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
web-1   |   "email": "xxxxx@xxxxxxx.com"
web-1   | }

Steps to Reproduce

  1. Connect an Outlook/Microsoft account
  2. Open the chat assistant
  3. Ask something like "Show me emails from johndoe@example.com" or "Show me emails from John Doe"

Possible explanation

It seems like the search functionality of Microsoft/Outlook does not support the colon in a query like from:"John Doe" and this is the reason why the BadRequest is returned. In the code of the Microsoft provider, there is even a function stripGmailPrefixes that seems to fix a similar issue for a prefix like subject:, because it is not supported by Microsoft:

// IMPORTANT: This is intentionally lossy!
// Gmail-style prefixes like "subject:" can't be translated to Microsoft Graph because:
// 1. $filter with contains(subject, ...) can't be combined with $search or date filters
//    (causes "InefficientFilter" error)
// 2. $search doesn't support field-specific syntax like "subject:term"
//
// We strip the prefixes and use plain $search which searches subject AND body.
// This is broader than intended but still finds relevant messages.
// If subject-specific search is needed in the future, add a dedicated method
// that uses only $filter without $search or date filters.
function stripGmailPrefixes(query: string): string {
  return query
    .replace(/\b(subject|from|to|label):(?:"[^"]*"|\S+)/gi, (match) => {
      // Extract the value without the prefix for searching
      const colonIndex = match.indexOf(":");
      const value = match.slice(colonIndex + 1);
      // Remove quotes if present
      return value.replace(/^"|"$/g, "");
    })
    .replace(/\s+/g, " ")
    .trim();
}

However, this function is only called in getMessagesWithPagination, not in searchMessages which also takes a query parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions