fix: resolve API key config mismatch and Pydantic model row collection#55
fix: resolve API key config mismatch and Pydantic model row collection#55
Conversation
Two runtime bugs found during live server testing: 1. config.py: field `assembly_api_key` with `env_prefix="ASSEMBLY_"` caused pydantic-settings to look for `ASSEMBLY_ASSEMBLY_API_KEY` instead of the documented `ASSEMBLY_API_KEY`. Rename field to `api_key` so the resolved env var matches the documented name. 2. services.py `_collect_rows`: `assembly_client` now returns Pydantic model instances instead of raw dicts. Add `model_dump()` handling so all service methods (MemberService, BillService, CommitteeService, etc.) correctly extract row data instead of silently returning empty lists. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request renames the assembly_api_key configuration field to api_key and updates its usage throughout the server. Additionally, it adds support for Pydantic models in the _collect_rows utility. A critical feedback point was raised regarding the Pydantic model handling: the current implementation might incorrectly treat container models as single data rows, potentially leading to parsing errors. It is recommended to convert the model to a dictionary and continue traversing its internal structure to locate the actual data rows.
| # Pydantic model → convert to dict and treat as a row | ||
| if hasattr(node, "model_dump"): | ||
| rows.append(node.model_dump()) | ||
| return | ||
| if isinstance(node, dict): |
There was a problem hiding this comment.
Pydantic 모델을 처리할 때 model_dump()를 호출하고 즉시 rows에 추가한 뒤 return하는 방식은, 해당 모델이 개별 데이터 행(row)이 아닌 전체 응답을 감싸는 컨테이너(Wrapper) 모델일 경우 문제를 일으킬 수 있습니다. 국회 API의 특성상 응답 객체는 여러 계층으로 중첩되어 있으며, 실제 데이터 행은 특정 키("row") 아래의 리스트에 존재합니다.
만약 assembly_client가 반환하는 모델이 전체 응답 구조를 유지하고 있다면, 이 로직은 개별 데이터 행이 아닌 전체 응답 객체 하나를 rows에 담게 되어 이후의 데이터 파싱(예: _build_bill) 과정에서 필드 누락으로 인한 오류가 발생할 수 있습니다. 모델을 dict로 변환한 후 내부 구조를 계속 탐색(walk)하여 실제 데이터 행을 찾도록 하거나, 모델이 컨테이너인지 개별 행인지에 따라 분기 처리하는 것이 더 안전합니다.
| # Pydantic model → convert to dict and treat as a row | |
| if hasattr(node, "model_dump"): | |
| rows.append(node.model_dump()) | |
| return | |
| if isinstance(node, dict): | |
| # Pydantic 모델인 경우 dict로 변환하여 내부 구조(특히 "row" 키)를 탐색하도록 합니다. | |
| if hasattr(node, "model_dump"): | |
| node = node.model_dump() | |
| if isinstance(node, dict): |
발견된 버그 (실서버 테스트 중)
1. API 키 환경변수 불일치 (
config.py)env_prefix="ASSEMBLY_"+ 필드명assembly_api_key→ pydantic-settings가ASSEMBLY_ASSEMBLY_API_KEY를 찾음.실제
.env와 문서는ASSEMBLY_API_KEY를 사용하므로 서버 기동 시 API 키가 항상None으로 인식됨.수정: 필드명을
api_key로 변경 →ASSEMBLY_API_KEY정상 인식.2. Pydantic 모델 인스턴스 미처리 (
services.py)assembly_client가 이전엔 dict를 반환했으나, 현재 버전은 Pydantic 모델 인스턴스 (Model_XXXX)를 반환._collect_rows가 이를 처리 못해 모든 서비스(의원·법안·위원회 등)가 빈 리스트 반환.수정:
_collect_rows내model_dump()핸들링 추가.영향 범위
버그 수정 전:
get_member_info,search_bills,get_committee_info등 실제 API 데이터를 반환하는 모든 툴 무응답버그 수정 후: 전 툴 정상 동작 확인 (MCP HTTP 엔드포인트 실사용 검증 완료)
Test plan
pytest tests/— 67/67 통과ruff check— 이상 없음get_member_info,search_bills,get_committee_info실호출 확인🤖 Generated with Claude Code