PydanticAI Research Agent
Pydantic AI Research Agent Built with the PRP Framework Template
Install / Use
npx skills add coleam00/PydanticAI-Research-AgentInstalls into whichever agent you are using.
README
PydanticAI Research & Email Agent System
A production-ready AI agent system built with PydanticAI that combines web research capabilities with Gmail email drafting, featuring a beautiful streaming CLI interface. This repository also demonstrates automated AI-powered issue fixing and PR review using multiple AI coding assistants.
🚀 Features
- Web Research: Uses Brave Search API for current, relevant information
- Email Drafting: Creates professional Gmail drafts based on research findings
- Agent Delegation: Research agent delegates email tasks to specialized email agent
- Streaming CLI: Beautiful real-time output using Rich library and PydanticAI's
.iter()method - OAuth2 Integration: Secure Gmail authentication with guided setup wizard
- Mock Testing: Comprehensive test suite with TestModel and mock services
- Production Ready: Environment-based configuration, error handling, and logging
- AI-Powered Workflows: Automated issue fixing and PR reviews via Claude Code, Codex, and Cursor
🤖 AI Coding Assistants (GitHub Actions)
This repository showcases automated issue handling and code review using three leading AI coding assistants. Simply mention them in issue or PR comments to trigger automated workflows.
Available Commands
- Claude Code:
@claude-fixor@claude-review - OpenAI Codex:
@codex-fixor@codex-review - Cursor:
@cursor-fixor@cursor-review
Setup Instructions
Add the following secrets to your GitHub repository (Settings → Secrets and variables → Actions → New repository secret):
- Claude Code:
CLAUDE_CODE_OAUTH_TOKEN- Install Claude CLI:
npm install -g @anthropic-ai/claude-code - Generate token:
claude setup-token(creates a 1-year token starting withsk-ant-oat01-) - Copy the generated token
- Install Claude CLI:
- OpenAI Codex:
OPENAI_API_KEY- Get from OpenAI platform - Cursor:
CURSOR_API_KEY- Generate from Cursor dashboard
How It Works
The workflows use reusable prompt templates (.github/issue_fix_prompt.md and .github/pr_review_prompt.md) that define the fix and review processes. Each AI assistant workflow loads these templates and customizes them with the appropriate branch naming suffix (-claude, -codex, or -cursor). This ensures consistency across all AI assistants while maintaining separate attribution for fixes and reviews.
Workflow Files:
.github/workflows/claude_code/- Claude Code workflows.github/workflows/codex/- OpenAI Codex workflows.github/workflows/cursor/- Cursor workflows
🏗️ Pydantic AI Agent Architecture
├── agents/ # PydanticAI agents
│ ├── research_agent.py # Main research agent with Brave search
│ └── email_agent.py # Gmail draft creation agent
├── config/ # Settings and model providers
│ ├── settings.py # Environment-based configuration
│ └── providers.py # LLM model setup
├── models/ # Pydantic data models
│ ├── email_models.py # Email-related models
│ ├── research_models.py # Research data models
│ └── agent_models.py # Generic agent models
├── tools/ # Tool functions
│ ├── brave_search.py # Brave Search API integration
│ └── gmail_tools.py # Gmail OAuth2 and draft creation
├── tests/ # Test suite
│ ├── test_research_agent.py
│ ├── test_email_agent.py
│ └── pytest.ini
├── gmail_setup.py # Gmail OAuth2 setup wizard
└── research_email_cli.py # Main CLI application
📋 Prerequisites
- Python 3.11+ with virtual environment capability
- API Keys:
- OpenAI API key (for LLM)
- Brave Search API key (for web search)
- Gmail OAuth2 Setup:
- Google Cloud Project with Gmail API enabled
- OAuth2 credentials downloaded from Google Cloud Console
🛠️ Installation
-
Clone and setup virtual environment:
git clone <repository-url> cd PydanticAI-Research-Agent python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate -
Install dependencies:
pip install 'pydantic-ai-slim[openai]' httpx rich python-dotenv pip install google-auth google-auth-oauthlib google-api-python-client pip install pytest pytest-asyncio # For testing -
Configure environment:
cp .env.example .env # Edit .env with your API keys -
Setup Gmail OAuth2:
python gmail_setup.py
⚙️ Configuration
Environment Variables (.env)
# 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
# Brave Search Configuration
BRAVE_API_KEY=your-brave-search-api-key-here
# Gmail OAuth2 Configuration
GMAIL_CREDENTIALS_PATH=credentials.json
GMAIL_TOKEN_PATH=token.pickle
# Application Configuration
APP_ENV=development
LOG_LEVEL=INFO
DEBUG=false
Gmail OAuth2 Setup
-
Create Google Cloud Project:
- Go to Google Cloud Console
- Create new project or select existing
-
Enable Gmail API:
- Go to APIs & Services > Library
- Search "Gmail API" and enable
-
Create OAuth2 Credentials:
- Go to APIs & Services > Credentials
- Create OAuth 2.0 Client ID for Desktop application
- Download as
credentials.json
-
Run Setup Wizard:
python gmail_setup.py
🎯 Usage
CLI Interface
source venv/bin/activate
python research_email_cli.py
Example Queries
- "Research AI safety trends and email summary to john@company.com"
- "Find latest developments in quantum computing"
- "Create email draft about market analysis for jane.doe@firm.com"
Programmatic Usage
from agents import research_agent, ResearchAgentDependencies
from config.settings import settings
# Create dependencies
deps = ResearchAgentDependencies(
brave_api_key=settings.brave_api_key,
gmail_credentials_path=settings.gmail_credentials_path,
gmail_token_path=settings.gmail_token_path
)
# Run research agent
result = await research_agent.run(
"Research machine learning trends",
deps=deps
)
🧪 Testing
Run the test suite with pytest:
source venv/bin/activate
python -m pytest tests/ -v
Test Environment
Tests use mock services and TestModel for validation without external API calls:
# Enable test mode
import os
os.environ['TESTING'] = 'true'
# Use TestModel for predictable responses
from pydantic_ai.models.test import TestModel
test_model = TestModel()
with research_agent.override(model=test_model):
result = research_agent.run_sync("Test query", deps=deps)
🔧 Development
Agent Tools
Research Agent:
search_web: Brave Search API integrationcreate_email_draft: Delegates to email agentsummarize_research: Creates structured summaries
Email Agent:
authenticate_gmail: OAuth2 authenticationcreate_gmail_draft: Draft creation in Gmailcompose_email_content: Professional email composition
Error Handling
The system includes comprehensive error handling:
- Gmail OAuth2: Detailed setup guidance and token refresh
- API Failures: Graceful degradation and retry mechanisms
- Network Issues: Timeout handling and connection recovery
- User Guidance: Actionable error messages with next steps
Security Features
- Environment Variables: No hardcoded secrets
- OAuth2 Flow: Secure Gmail authentication
- Input Validation: Pydantic model validation
- API Key Protection: Never logged or exposed in errors
📚 PydanticAI Patterns Used
This implementation demonstrates key PydanticAI patterns:
- Agent Composition: Multiple specialized agents working together
- Dependency Injection: Clean separation of concerns with
deps_type - Tool Integration:
@agent.tooldecorators with proper context - Model Override: TestModel for development and testing
- Streaming Output: Real-time CLI with
.iter()method - Usage Tracking: Token counting across agent delegations
- Error Recovery: Graceful handling of external service failures
🚨 Important Notes
- Never commit
credentials.jsonortoken.pickleto version control - Add to .gitignore: All sensitive files are properly excluded
- API Rate Limits: Brave Search has usage quotas - monitor consumption
- Token Expiry: Gmail tokens refresh automatically but may need re-authentication
- Mock Mode: Set
TESTING=truefor development without real API calls
🆘 Troubleshooting
Common Issues
-
"No module named 'pydantic_ai'":
pip install 'pydantic-ai-slim[openai]' -
Gmail authentication errors:
python gmail_setup.py # Re-run OAuth2 setup -
Import errors:
source venv/bin/activate # Ensure virtual environment is active -
Missing API keys:
- Check
.envfile exists and has valid keys - Verify environment variables are loaded
- Check
📖 Learn More
🤝 Contributing
This project follows PydanticAI best practices:
- Use environment-based configuration
- Implement comprehensive error handling
- Include TestModel validation for all agents
- Follow agent-to-agent delegation patterns
- Maintain security standards for API keys and OAuth2
Built with ❤️ using PydanticAI and following production-ready development practices.
Related Skills
mcp
Use the `mcp_perplexity-ask_perplexity_search` tools to answer questions. You should use this instead of the `web_search` tool because it is a lot more accurate.
practical-power-systems-synthesis
This skill enables synthesis in the domain of power-systems (engineering). It represents research-level-level expertise and is designed for production use in research, industry, and educational contexts. Use this skill when you need to perform synthesis operations related to power-systems.
semi-supervised-optogenetics-testing
This skill enables testing in the domain of optogenetics (neuroscience). It represents intermediate-level expertise and is designed for production use in research, industry, and educational contexts. Use this skill when you need to perform testing operations related to optogenetics.
data-mining-interpretation-fundamental
This skill enables interpretation in the domain of data-mining (data-science). It represents fundamental-level expertise and is designed for production use in research, industry, and educational contexts. Use this skill when you need to perform interpretation operations related to data-mining.
