{"record":{"id":"b108817dc09a049b","repo":"datawhalechina/hello-agents","slug":"user-id-b10881","errorCode":null,"errorMessage":"user_id 不能为空","messagePattern":"user_id 不能为空","errorType":"validation","errorClass":"ValueError","httpStatus":422,"severity":"warning","filePath":"Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/api/routes/health.py","lineNumber":25,"sourceCode":"from pydantic import BaseModel, Field, field_validator\n\nfrom memory.store import get_report_run, list_report_runs_for_user\nfrom service.observability_views import build_report_observability\nfrom service.health_analysis import HealthAnalysisService\n\nrouter = APIRouter()\n\n\nclass HealthRequest(BaseModel):\n    report_text: str\n    user_id: str = Field(..., min_length=1, max_length=256)\n\n    @field_validator(\"user_id\")\n    @classmethod\n    def normalize_user_id(cls, v: str) -> str:\n        v = v.strip()\n        if not v:\n            raise ValueError(\"user_id 不能为空\")\n        return v\n\n\n@router.post(\"/health/analysis\")\nasync def analysis_health(request: HealthRequest):\n    task_id = str(uuid4())\n\n    service = HealthAnalysisService(task_id=task_id, user_id=request.user_id)\n    asyncio.create_task(service.run(request.report_text, request.user_id))\n\n    return {\"task_id\": task_id, \"user_id\": request.user_id}\n\n\n@router.post(\"/health/analysis/pdf\")\nasync def analysis_health_pdf(\n    file: UploadFile = File(...),\n    user_id: str = Form(...),\n):","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/api/routes/health.py#L7-L43","documentation":"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.","triggerScenarios":"POST /health/analysis with user_id=\" \" or an empty/whitespace string in the JSON body.","commonSituations":"Frontend sending untrimmed input fields; default placeholder values in forms; test payloads with blank ids.","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."],"exampleFix":"# before\nrequests.post(f'{base}/health/analysis', json={'report_text': txt, 'user_id': '  '})  # 422\n\n# after\nrequests.post(f'{base}/health/analysis', json={'report_text': txt, 'user_id': 'u123'})","handlingStrategy":"validation","validationCode":"uid = user_id.strip()\nif not uid:\n    raise ValueError(\"user_id required\")\nrequests.post(f\"{base}/health/analysis\", json={\"report_text\": txt, \"user_id\": uid})","typeGuard":"def is_valid_user_id(v) -> bool:\n    return isinstance(v, str) and 1 <= len(v.strip()) <= 256","tryCatchPattern":null,"preventionTips":["Make user_id a required, trimmed field in client forms.","Share validation rules between Health and Diet request models to keep behavior uniform."],"tags":["pydantic","fastapi","validation","http-422"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}