AI Agent Troubleshooting Guide¶
This guide helps resolve common issues with the PyFIA AI Agent.
Quick Diagnostics¶
Run this command to check your setup:
This will verify: - ✓ Python version (3.11+) - ✓ Required packages installed - ✓ OpenAI API key configured - ✓ Database connection - ✓ Memory/cache directories
Common Issues¶
1. OpenAI API Key Issues¶
Problem: "OpenAI API key not found"¶
Solution:
# Set the API key
export OPENAI_API_KEY="sk-..."
# Or add to your shell profile
echo 'export OPENAI_API_KEY="sk-..."' >> ~/.bashrc
source ~/.bashrc
Problem: "Invalid API key"¶
Check: - Key starts with sk-
- No extra spaces or quotes - Key hasn't been revoked
Test:
2. Database Connection Issues¶
Problem: "Database not found"¶
Solutions:
-
Check file path:
-
Check permissions:
Problem: "Cannot open database"¶
Possible causes: - Database is corrupted - Wrong DuckDB version - File is locked by another process
Fix:
import duckdb
# Test connection
conn = duckdb.connect("database.duckdb", read_only=True)
conn.execute("SELECT 1").fetchall()
3. Import and Dependency Issues¶
Problem: "ModuleNotFoundError: langchain"¶
Solution:
Problem: "ImportError: cannot import name 'create_react_agent'"¶
Fix - Update LangGraph:
4. Query Processing Issues¶
Problem: "No results found"¶
Common causes and fixes:
-
Wrong EVALID:
-
Too restrictive filters:
-
Species name issues:
Problem: "Query timeout"¶
Solutions:
-
Simplify query:
-
Add limits:
-
Use specific filters:
5. Memory and Performance Issues¶
Problem: "Conversation not persisting"¶
Check checkpoint directory:
# Default location
ls ~/.pyfia/checkpoints/
# Set custom location
pyfia-ai database.duckdb --checkpoint-dir ./my_checkpoints
Fix permissions:
Problem: "Slow responses"¶
Optimizations:
-
Enable caching:
-
Reduce token usage:
-
Use faster model:
6. Formatting and Display Issues¶
Problem: "No colors or formatting in terminal"¶
Solutions:
-
Check terminal support:
-
Force color output:
-
Disable Rich if needed:
Problem: "Tables not displaying correctly"¶
Fix terminal width:
Error Messages Reference¶
API Errors¶
Error | Meaning | Solution |
---|---|---|
RateLimitError | Too many requests | Wait and retry, or upgrade API plan |
InvalidRequestError | Bad request format | Check query syntax |
AuthenticationError | Invalid API key | Verify key configuration |
APIConnectionError | Network issue | Check internet connection |
Database Errors¶
Error | Meaning | Solution |
---|---|---|
CatalogException | Table not found | Verify table names with schema |
BinderException | Column not found | Check column names |
ParserException | SQL syntax error | Review query syntax |
IOException | Can't read file | Check file path and permissions |
Agent Errors¶
Error | Meaning | Solution |
---|---|---|
ToolExecutionError | Tool failed | Check tool parameters |
ValidationError | Invalid input | Review input format |
MemoryError | Out of memory | Reduce result size |
TimeoutError | Query too slow | Simplify query |
Debugging Techniques¶
1. Enable Verbose Mode¶
# See detailed processing
pyfia-ai database.duckdb --verbose
# Or in Python
agent = FIAAgent("db.duckdb", verbose=True)
2. Check Logs¶
# View agent logs
tail -f ~/.pyfia/logs/agent.log
# Enable debug logging
export PYFIA_LOG_LEVEL=DEBUG
3. Test Individual Components¶
# Test database connection
from pyfia.database.query_interface import DuckDBQueryInterface
qi = DuckDBQueryInterface("database.duckdb")
qi.test_connection()
# Test tools directly
from pyfia.ai.agent import find_species_codes
result = find_species_codes("oak")
print(result)
4. Trace LangChain Execution¶
# Enable LangChain tracing
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY="your-langsmith-key"
Performance Optimization¶
Query Optimization¶
-
Use EVALIDs:
-
Filter early:
Resource Management¶
-
Limit memory usage:
-
Clear cache periodically:
-
Close unused connections:
Getting Help¶
Self-Help Resources¶
-
Built-in help:
-
Examples:
-
Documentation:
- User Guide
- Examples
- Tools Reference
Community Support¶
- GitHub Issues: Report bugs and request features
- Discussions: Ask questions and share tips
- Wiki: Community-contributed guides
Debug Information¶
When reporting issues, include:
# System info
python --version
pip show pyfia
# Error details
pyfia-ai --diagnose > debug_info.txt
# Sample query that fails
echo "Your problematic query"
Recovery Procedures¶
Reset Agent State¶
# Clear all caches and checkpoints
rm -rf ~/.pyfia/checkpoints/*
rm -rf ~/.pyfia/cache/*
# Restart fresh
pyfia-ai database.duckdb
Reinstall Package¶
Database Repair¶
import duckdb
# Verify database integrity
conn = duckdb.connect("database.duckdb")
conn.execute("PRAGMA integrity_check").fetchall()
# Export and reimport if needed
conn.execute("EXPORT DATABASE 'backup' (FORMAT PARQUET)")
Preventive Measures¶
-
Regular Updates:
-
Monitor Resources:
-
Backup Conversations:
-
Test After Updates:
Remember: Most issues can be resolved by checking the basics - API key, database path, and dependencies. When in doubt, start with pyfia-ai --diagnose
.