Repository: run-llama/llama_index
Pull request: Fix: GmailToolSpec.load_data() applies GmailTool...#18414
# Description

Fixes #18414 GmailToolSpec.load_data() applies GmailToolSpec.search_messages() without arguments.

GmailToolSpec.load_data() calls GmailToolSpec.search_messages() without any parameters when the Agent is asked to query for emails without any filters

```
def load_data(self) -> List[Document]:
      """Load emails from the user's account."""
      self._cache_service()

      return self.search_messages()
```

The change allows the agent to pass in an empty query to the gmail client, which in turn returns the last `max_results` number of emails.

## New Package?
Did I fill in the tool.llamahub section in the pyproject.toml and provide a detailed README.md for my new integration or package?

- [ ] Yes
- [x] No (Not required)

## Version Bump?
Did I bump the version in the pyproject.toml file of the package I am updating? (Except for the llama-index-core package)
- [ ] Yes
- [x] No (the changes are in llama-index-core)

## Type of Change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested?

Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.

- [ ] I added new unit tests to cover this change
- [x] I believe this change is already covered by existing unit tests

## Suggested Checklist:

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] New and existing unit tests pass locally with my changes
- [x] I ran `uv run make format; uv run make lint` to appease the lint gods


Problem statement:
[Bug]: `GmailToolSpec.load_data()` applies `GmailToolSpec.search_messages()` without arguments
### Bug Description

`GmailToolSpec.load_data()` applies `GmailToolSpec.search_messages()` without arguments:
```
  def load_data(self) -> List[Document]:
      """Load emails from the user's account."""
      self._cache_service()

      return self.search_messages()
```
### Version

latest

### Steps to Reproduce

```
from llama_hub.tools.gmail.base import GmailToolSpec

tool_spec = GmailToolSpec()

agent = OpenAIAgent.from_tools(tool_spec.to_tool_list())
agent.chat("...")
```

### Relevant Logs/Tracbacks

```shell
in `load_data() `search_messages()` missing 1 required positional argument: 'query'
```

Hints from issue discussion:
<!-- Greeting -->
Hello @nathan-cicero! 👋 I'm [Dosu](https://dosu.dev), a bot here to help you solve bugs, answer questions, and assist you in becoming a contributor. I'm ready to assist you while you wait for a human maintainer!

<!-- Answer -->
The error you're encountering indicates that the `search_messages` method is being called without the required `query` argument. The method signature for `search_messages` is:

```python
def search_messages(self, query: str, max_results: Optional[int] = None):
```

This means that the `query` argument is mandatory and must be provided when calling the method <sup>[[1]](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/gmail/base.py)</sup>. To resolve this issue, ensure that you pass a valid query string when invoking `search_messages`.


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*

---
<sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/eb0bc91b-7083-4b02-a43d-fcbd69c77c30?feedback_type=other)</sup>

Seems like the LLM just didn't write a query kwarg for the tool?
> Seems like the LLM just didn't write a query kwarg for the tool?

Edited the bug.

Commit message: Merge branch 'main' into fix-gmailtoolspec-load-data-no-args-error

Patch to review:
diff --git a/llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/gmail/base.py b/llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/gmail/base.py
index fdbab97621..ba5775a9ea 100644
--- a/llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/gmail/base.py
+++ b/llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/gmail/base.py
@@ -84,14 +84,14 @@ def _get_credentials(self) -> Any:
 
         return creds
 
-    def search_messages(self, query: str, max_results: Optional[int] = None):
+    def search_messages(self, query: Optional[str] = None, max_results: Optional[int] = None):
         """
         Searches email messages given a query string and the maximum number
         of results requested by the user
            Returns: List of relevant message objects up to the maximum number of results.
 
         Args:
-            query[str]: The user's query
+            query (Optional[str]): The user's query
             max_results (Optional[int]): The maximum number of search results
             to return.
 


Code review comments:
diff_hunk: @@ -84,14 +84,14 @@ def _get_credentials(self) -> Any:
 
         return creds
 
-    def search_messages(self, query: str, max_results: Optional[int] = None):
+    def search_messages(self, query: Optional[str] = None, max_results: Optional[int] = None):
path: llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/gmail/base.py
text: @user1:
This will change how the LLM calls `search_messages` -- by making `query` optional, LLMs are very lazy and will rarely fill it in

Better fix is to pass in an empty string from `load_data()` and handle that properly 

@user1:
Applied this idea