Skip to content

Commit 8b0d78f

Browse files
committed
feat: Document backend deployment fixes including Vertex AI configuration, port settings, and import resolution
1 parent e32433d commit 8b0d78f

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

docs/BACKEND_DEPLOYMENT_FIX.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Backend Deployment Fix Summary
2+
3+
## Issue Encountered
4+
1. **403 Error**: Backend was using public Gemini API instead of Vertex AI
5+
2. **Port Mismatch**: Dockerfile set PORT=8000, Cloud Run expects PORT=8080
6+
3. **Import Errors**: Old incompatible files conflicting with new code
7+
8+
## Fixes Applied
9+
10+
### 1. Vertex AI Configuration ✅
11+
**Problem**: Backend defaulted to public Gemini API (`generativelanguage.googleapis.com`)
12+
**Root**: `use_vertex_ai: bool = False` in settings.py
13+
14+
**Changes**:
15+
- [backend/config/settings.py](../backend/config/settings.py#L21): Changed default `use_vertex_ai` from `False``True`
16+
- [backend/config/settings.py](../backend/config/settings.py#L78): Changed env var default from `"false"``"true"`
17+
- [Dockerfile](../Dockerfile): Added `ENV USE_VERTEX_AI=true`
18+
- [backend/.env.example](../backend/.env.example): Documented `USE_VERTEX_AI=true` setting
19+
20+
**Result**: Backend now uses Vertex AI (`aiplatform.googleapis.com`) by default
21+
22+
### 2. Port Configuration ✅
23+
**Problem**: Dockerfile set `ENV PORT=8000`, overriding Cloud Run's `PORT=8080`
24+
**Error**: "Container failed to start and listen on the port defined by PORT=8080"
25+
26+
**Changes**:
27+
- [Dockerfile](../Dockerfile): Removed `ENV PORT=8000` line
28+
- Added comment explaining Cloud Run sets PORT=8080 automatically
29+
30+
**Result**: Backend now listens on Cloud Run's PORT (8080)
31+
32+
### 3. File Cleanup & Import Resolution ✅
33+
**Problem**: Multiple versions of files causing import conflicts
34+
35+
**Old incompatible files** (renamed to .old.py):
36+
- `backend/main.py``backend/main.old.py`
37+
- `backend/api/app.py``backend/api/app.old.py`
38+
- `backend/api/endpoints.py``backend/api/endpoints.old.py`
39+
- `backend/agents/agent_definitions.py``backend/agents/agent_definitions.old.py`
40+
- `backend/agents/agent_strategies.py``backend/agents/agent_strategies.old.py`
41+
- `backend/managers/chatbot_manager.py``backend/managers/chatbot_manager.old.py`
42+
43+
**New files activated** (renamed from *_new.py → *.py):
44+
- `backend/main_new.py``backend/main.py`
45+
- `backend/api/app_new.py``backend/api/app.py`
46+
- `backend/api/endpoints_new.py``backend/api/endpoints.py`
47+
- `backend/agents/agent_definitions_new.py``backend/agents/agent_definitions.py`
48+
- `backend/agents/agent_strategies_new.py``backend/agents/agent_strategies.py`
49+
- `backend/managers/chatbot_manager_new.py``backend/managers/chatbot_manager.py`
50+
51+
**Import updates** in [backend/api/app.py](../backend/api/app.py):
52+
-FROM:
53+
```python
54+
from managers.chatbot_manager_new import get_chatbot_manager
55+
from api.endpoints_new import router
56+
from agents.agent_strategies_new import get_workflow_template
57+
from agents.agent_definitions_new import get_agent_config
58+
```
59+
60+
TO:
61+
```python
62+
from managers.chatbot_manager import get_chatbot_manager
63+
from api.endpoints import router
64+
from agents.agent_strategies import get_workflow_template
65+
from agents.agent_definitions import get_agent_config
66+
```
67+
68+
**Import updates** in [backend/main.py](../backend/main.py):
69+
```python
70+
uvicorn.run("api.app:app", ...) # Changed from "api.app_new:app"
71+
```
72+
73+
**Dockerfile CMD** updated:
74+
```dockerfile
75+
CMD ["python", "main.py"] # Changed from "main_new.py"
76+
```
77+
78+
**Result**: All imports resolved, no more conflicting file versions
79+
80+
---
81+
82+
## Deployment Command
83+
84+
```bash
85+
gcloud run deploy legalmind-backend \
86+
--source=backend \
87+
--project=legalmind-486106 \
88+
--region=us-central1 \
89+
--allow-unauthenticated \
90+
--set-env-vars="USE_VERTEX_AI=true" \
91+
--timeout=300
92+
```
93+
94+
---
95+
96+
## Expected Outcome
97+
98+
After deployment completes:
99+
100+
1. ✅ Backend starts successfully on PORT=8080
101+
2. ✅ Health endpoint responds with HTTP 200
102+
3. ✅ Vertex AI configured correctly (no 403 errors)
103+
4. ✅ Contract analysis works without authentication errors
104+
105+
### Test After Deployment
106+
107+
```bash
108+
# Health check
109+
curl https://legalmind-backend-677928716377.us-central1.run.app/health
110+
111+
# Expected response:
112+
{
113+
"status": "healthy",
114+
"service": "LegalMind API",
115+
"timestamp": "..."
116+
}
117+
```
118+
119+
---
120+
121+
## Files Modified
122+
123+
1. `Dockerfile` - Removed PORT override, added USE_VERTEX_AI
124+
2. `backend/config/settings.py` - Changed defaults to use Vertex AI
125+
3. `backend/.env.example` - Documented USE_VERTEX_AI setting
126+
4. `backend/main.py` - Updated imports (renamed from main_new.py)
127+
5. `backend/api/app.py` - Updated imports (renamed from app_new.py)
128+
6. Multiple backend modules - Renamed from *_new.py to *.py
129+
130+
---
131+
132+
## Summary
133+
134+
**Root Cause**: Configuration defaulted to public Gemini API + old incompatible files
135+
**Solution**: Enable Vertex AI by default + clean up file conflicts + fix port configuration
136+
**Status**: Deployment in progress
137+
**Deployment Time**: ~3-5 minutes
138+
139+
Once deployment completes, the backend will:
140+
- Use Vertex AI exclusively (`aiplatform.googleapis.com`)
141+
- Listen on the correct port (8080)
142+
- Have no import conflicts
143+
- Work correctly with Cloud Run service accounts

0 commit comments

Comments
 (0)