A production-grade multi-agent Pydantic AI system that combines web research capabilities with professional email drafting. The system features a research agent that can search the web using Tavily API and delegate email creation tasks to an email agent with Gmail API integration.
- Multi-Agent Architecture: Research agent delegates to email agent as a tool
- Web Research: Uses Tavily API for comprehensive web searches
- Email Drafting: Creates professional email drafts with Gmail API
- Streaming CLI: Real-time streaming interface using Rich and Typer
- Security First: Comprehensive security patterns and input validation
- Production Ready: Error handling, retry mechanisms, and monitoring
Before you begin, ensure you have the following:
- Python 3.9 or higher
- UV package manager (recommended) or pip
- API keys for:
- OpenAI or compatible LLM provider
- Tavily for web search
- Google Cloud Console project with Gmail API enabled
git clone git@github.com:pydevup/research-email-multi-agent-system.git
cd research-email-multi-agent-systemUsing UV (recommended):
uv syncUsing pip:
pip install -e .Copy the environment template:
cp .env.example .envEdit .env with your API keys and configuration:
# LLM Configuration
LLM_PROVIDER=openai
LLM_API_KEY=your_openai_api_key_here
LLM_MODEL=gpt-4o
LLM_BASE_URL=https://api.openai.com/v1
# Tavily API Configuration
TAVILY_API_KEY=your_tavily_api_key_here
# Gmail API Configuration
GMAIL_CREDENTIALS_PATH=credentials/credentials.json
GMAIL_TOKEN_PATH=credentials/token.json
# Application Configuration
APP_ENV=development
LOG_LEVEL=INFO
DEBUG=false- Visit OpenAI Platform
- Create an account or sign in
- Navigate to API Keys section
- Create a new API key
- Copy the key to your
.envfile asLLM_API_KEY
- Visit Tavily
- Sign up for a free account
- Get your API key from the dashboard
- Copy the key to your
.envfile asTAVILY_API_KEY
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the Gmail API:
- Navigate to "APIs & Services" > "Library"
- Search for "Gmail API"
- Click "Enable"
- Go to "APIs & Services" > "OAuth consent screen"
- Choose "External" user type
- Fill in required information:
- App name: "Research Email Agent"
- User support email: your email
- Developer contact information: your email
- Add scopes:
https://www.googleapis.com/auth/gmail.compose
- Add test users (your email address)
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth 2.0 Client IDs"
- Choose "Desktop application" as application type
- Download the credentials file
- Save it as
credentials/credentials.jsonin your project
mkdir -p credentials
chmod 700 credentials # Secure the directory
# Move your downloaded credentials.json to credentials/The system provides a streaming CLI with real-time output:
# Start the CLI
python cli.py chat
# Or to get help
python cli.py --help-
Research and Email Drafting:
User: Research the latest AI trends and draft an email to my team about them Agent: I'll search for the latest AI trends and create a draft email for your team. [Searching Tavily...] [Found 8 relevant articles] [Creating email draft...] [Draft created successfully!] -
Web Research Only:
User: Search for information about climate change solutions Agent: I'll search for information about climate change solutions. [Searching Tavily...] [Found 12 relevant articles] Here's what I found about climate change solutions... -
Email Drafting Only:
User: Draft an email to john@example.com about our meeting tomorrow Agent: I'll create a draft email to john@example.com about your meeting. [Creating email draft...] [Draft created successfully!]
Reseach-Email-Multi-Agent/
├── research_agent.py # Main research agent with Tavily integration
├── email_agent.py # Email drafting agent with Gmail integration
├── tools.py # Pure tool functions for both agents
├── models.py # Pydantic models for structured outputs
├── dependencies.py # External service dependencies
├── settings.py # Environment-based configuration
├── providers.py # Model provider abstraction
├── cli.py # Streaming CLI interface
├── scripts/ # Utility scripts
│ ├── validate_security.py
│ └── validate_gmail_oauth.py
├── credentials/ # OAuth2 credentials (gitignored)
│ ├── credentials.json
│ └── token.json
├── tests/ # Test suite
├── .env.example # Environment template
└── pyproject.toml # Project configuration
| Variable | Description | Default |
|---|---|---|
LLM_PROVIDER |
LLM provider (openai, anthropic) | openai |
LLM_API_KEY |
API key for LLM provider | Required |
LLM_MODEL |
Model name | gpt-4o |
LLM_BASE_URL |
Base URL for API | OpenAI URL |
TAVILY_API_KEY |
Tavily API key | Required |
GMAIL_CREDENTIALS_PATH |
Path to OAuth2 credentials | credentials/credentials.json |
GMAIL_TOKEN_PATH |
Path to OAuth2 token | credentials/token.json |
APP_ENV |
Application environment | development |
LOG_LEVEL |
Logging level | INFO |
DEBUG |
Debug mode | false |
The system includes comprehensive security measures:
- Rate Limiting: Prevents API abuse
- Input Sanitization: Protects against injection attacks
- Secure Logging: No sensitive data in logs
- File Permissions: Secure credential storage
- Error Handling: Comprehensive retry mechanisms
Run security validation:
python scripts/validate_security.pyRun the test suite:
# Run all tests
python -m pytest tests/ -v
# Run specific test files
python -m pytest tests/test_research_agent.py -v
python -m pytest tests/test_email_agent.py -v
python -m pytest tests/test_tools.py -v1. Gmail Authentication Fails
- Ensure
credentials/credentials.jsonexists - Check OAuth2 consent screen is configured
- Verify scopes include
gmail.compose
2. Tavily API Errors
- Verify API key is correct
- Check rate limits (10 requests/minute)
- Ensure internet connectivity
3. LLM Provider Issues
- Verify API key and base URL
- Check model compatibility
- Ensure sufficient API credits
4. File Permission Errors
chmod 600 credentials/*.json
chmod 700 credentials/Enable debug mode for detailed logging:
DEBUG=true python cli.py chat- Never commit API keys - Use environment variables
- Secure credential files - Set proper file permissions
- Regular security audits - Use the validation script
- Monitor API usage - Watch for unusual activity
- Keep dependencies updated - Regular security updates
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and security validation
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Pydantic AI for the agent framework
- Tavily for web search capabilities
- Google Gmail API for email integration
- Rich for beautiful CLI output