datawhalechina/hello-agents · warning · ValueError
user_id 不能为空
Error message
user_id 不能为空
What it means
Pydantic field_validator error on HealthRequest: user_id must be 1-256 chars and non-empty after stripping. A whitespace-only value passes min_length but fails this check, and FastAPI returns 422 for POST /health/analysis.
Source
Thrown at Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/api/routes/health.py:25
from pydantic import BaseModel, Field, field_validator
from memory.store import get_report_run, list_report_runs_for_user
from service.observability_views import build_report_observability
from service.health_analysis import HealthAnalysisService
router = APIRouter()
class HealthRequest(BaseModel):
report_text: str
user_id: str = Field(..., min_length=1, max_length=256)
@field_validator("user_id")
@classmethod
def normalize_user_id(cls, v: str) -> str:
v = v.strip()
if not v:
raise ValueError("user_id 不能为空")
return v
@router.post("/health/analysis")
async def analysis_health(request: HealthRequest):
task_id = str(uuid4())
service = HealthAnalysisService(task_id=task_id, user_id=request.user_id)
asyncio.create_task(service.run(request.report_text, request.user_id))
return {"task_id": task_id, "user_id": request.user_id}
@router.post("/health/analysis/pdf")
async def analysis_health_pdf(
file: UploadFile = File(...),
user_id: str = Form(...),
):View on GitHub (pinned to 606a07d341)
Solutions
- Send a trimmed, real user identifier in the request body.
- Add client-side required-field validation for user_id before submit.
- Read the 422 detail to confirm which field failed.
Example fix
# before
requests.post(f'{base}/health/analysis', json={'report_text': txt, 'user_id': ' '}) # 422
# after
requests.post(f'{base}/health/analysis', json={'report_text': txt, 'user_id': 'u123'}) Defensive patterns
Strategy: validation
Validate before calling
uid = user_id.strip()
if not uid:
raise ValueError("user_id required")
requests.post(f"{base}/health/analysis", json={"report_text": txt, "user_id": uid}) Type guard
def is_valid_user_id(v) -> bool:
return isinstance(v, str) and 1 <= len(v.strip()) <= 256 Prevention
- Make user_id a required, trimmed field in client forms.
- Share validation rules between Health and Diet request models to keep behavior uniform.
When it happens
Trigger: POST /health/analysis with user_id=" " or an empty/whitespace string in the JSON body.
Common situations: Frontend sending untrimmed input fields; default placeholder values in forms; test payloads with blank ids.
Related errors
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/b108817dc09a049b.
Report an issue: GitHub.