|
| 1 | +--- |
| 2 | +name: backend-debugging-dependency-resolution |
| 3 | +description: Systematic approach to diagnose and resolve backend service issues including dependency conflicts, database schema mismatches, import errors, and middleware configuration problems in FastAPI applications. |
| 4 | +--- |
| 5 | + |
| 6 | +# Backend Debugging and Dependency Resolution Skill |
| 7 | + |
| 8 | +## Purpose |
| 9 | + |
| 10 | +This skill provides a systematic methodology for diagnosing and resolving backend service issues in FastAPI applications, with particular focus on dependency conflicts, database schema mismatches, import errors, and middleware configuration problems. |
| 11 | + |
| 12 | +## When to Use This Skill |
| 13 | + |
| 14 | +This skill should be used when: |
| 15 | +- FastAPI applications fail to start or respond with internal server errors |
| 16 | +- Dependency version conflicts arise between FastAPI, Starlette, and related packages |
| 17 | +- Database foreign key constraint violations occur |
| 18 | +- Import-related errors prevent module loading |
| 19 | +- Middleware configuration causes request processing failures |
| 20 | +- Authentication or session management systems malfunction |
| 21 | +- RAG agent or vector store integration fails |
| 22 | + |
| 23 | +## Implementation Process |
| 24 | + |
| 25 | +### Phase 1: Dependency Conflict Diagnosis |
| 26 | + |
| 27 | +1. **Check version compatibility matrix** |
| 28 | + - Identify FastAPI version (e.g., 0.104.1) |
| 29 | + - Identify Starlette version and verify compatibility range |
| 30 | + - Identify anyio and other dependency versions |
| 31 | + - Consult official compatibility documentation |
| 32 | + |
| 33 | +2. **Resolve dependency conflicts** |
| 34 | + - Install compatible versions (e.g., Starlette <0.28.0 for FastAPI 0.104.1) |
| 35 | + - Pin dependency versions in requirements.txt |
| 36 | + - Test with minimal dependency set first |
| 37 | + |
| 38 | +3. **Verify resolution** |
| 39 | + - Test basic FastAPI functionality with simple endpoint |
| 40 | + - Confirm middleware stack processes requests without errors |
| 41 | + - Validate dependency compatibility with test client |
| 42 | + |
| 43 | +### Phase 2: Database Schema Consistency |
| 44 | + |
| 45 | +1. **Identify foreign key mismatches** |
| 46 | + - Check error messages for "ForeignKeyViolation" or "constraint violation" |
| 47 | + - Compare model definitions with actual database schema |
| 48 | + - Identify which table references which in foreign key relationships |
| 49 | + |
| 50 | +2. **Fix relationship inconsistencies** |
| 51 | + - Update ForeignKey references to point to correct tables |
| 52 | + - Ensure relationship definitions match foreign key constraints |
| 53 | + - Update related model relationships accordingly |
| 54 | + |
| 55 | +3. **Apply schema changes** |
| 56 | + - Drop and recreate tables if safe to do so (development) |
| 57 | + - Use Alembic migrations for production environments |
| 58 | + - Verify new relationships work correctly |
| 59 | + |
| 60 | +### Phase 3: Import and Module Structure Resolution |
| 61 | + |
| 62 | +1. **Diagnose import errors** |
| 63 | + - Identify missing module errors (e.g., "No module named 'embeddings'") |
| 64 | + - Trace import chains to find incorrect paths |
| 65 | + - Check for circular imports or namespace pollution |
| 66 | + |
| 67 | +2. **Fix import paths** |
| 68 | + - Update import statements to reflect actual module structure |
| 69 | + - Use explicit imports instead of wildcard imports |
| 70 | + - Ensure all `__init__.py` files properly export required modules |
| 71 | + |
| 72 | +3. **Validate module structure** |
| 73 | + - Test individual module imports in isolation |
| 74 | + - Verify `__init__.py` files contain proper exports |
| 75 | + - Confirm package structure matches import expectations |
| 76 | + |
| 77 | +### Phase 4: Middleware and Configuration Fixes |
| 78 | + |
| 79 | +1. **Diagnose middleware errors** |
| 80 | + - Look for "too many values to unpack" errors in middleware context |
| 81 | + - Check middleware stack construction and configuration |
| 82 | + - Verify middleware compatibility with FastAPI version |
| 83 | + |
| 84 | +2. **Resolve configuration issues** |
| 85 | + - Remove conflicting middleware configurations |
| 86 | + - Ensure middleware is added in correct order |
| 87 | + - Validate middleware parameters and options |
| 88 | + |
| 89 | +### Phase 5: System Integration Verification |
| 90 | + |
| 91 | +1. **Test core components** |
| 92 | + - Verify RAG agent functionality with sample queries |
| 93 | + - Test authentication endpoints (registration, login, profile) |
| 94 | + - Confirm database connectivity and session management |
| 95 | + - Validate vector store integration |
| 96 | + |
| 97 | +2. **Validate end-to-end flows** |
| 98 | + - Test complete user journey from authentication to content retrieval |
| 99 | + - Verify session persistence across requests |
| 100 | + - Confirm error handling and fallback mechanisms |
| 101 | + |
| 102 | +## Validation Checklist |
| 103 | + |
| 104 | +Before considering the debugging complete, verify: |
| 105 | + |
| 106 | +- [ ] All dependency version conflicts resolved |
| 107 | +- [ ] Database schema consistency achieved |
| 108 | +- [ ] Import errors eliminated |
| 109 | +- [ ] Middleware configuration fixed |
| 110 | +- [ ] Core functionality tested (RAG, auth, sessions) |
| 111 | +- [ ] Error handling working properly |
| 112 | +- [ ] End-to-end flows functional |
| 113 | +- [ ] No regressions introduced |
| 114 | + |
| 115 | +## Critical Indicators |
| 116 | + |
| 117 | +- **Dependency conflicts**: Look for "ValueError: too many values to unpack" in middleware context |
| 118 | +- **Schema mismatches**: Check for "ForeignKeyViolation" or "constraint violation" errors |
| 119 | +- **Import issues**: Watch for "No module named 'X'" errors |
| 120 | +- **Middleware problems**: Monitor for errors in `build_middleware_stack` calls |
| 121 | + |
| 122 | +## Expected Outcomes |
| 123 | + |
| 124 | +Upon successful application of this skill: |
| 125 | +- Backend services start without errors |
| 126 | +- API endpoints respond with appropriate status codes |
| 127 | +- Database operations complete successfully |
| 128 | +- Authentication and session management work properly |
| 129 | +- RAG agent processes queries and returns responses |
| 130 | +- Frontend integration functions correctly |
| 131 | +- Error handling provides meaningful feedback |
0 commit comments