api-conventions
REST and WebSocket endpoint patterns, error handling, and Pydantic schema conventions for the backend
소스 정보
- 저장소
- ag2ai/resource-hub
- 최근 소스 활동
- 2026년 3월 19일 06:20
- 감지된 SKILL.md 언어
- 영어
- 스타
- 4
- 포크
- 3
설치 방법
기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.
소스 파일 검토
설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.
SKILL.md 표시 중
SKILL.md
소스 지침 · 읽기 전용 미리보기- name
- api-conventions
- description
- REST and WebSocket endpoint patterns, error handling, and Pydantic schema conventions for the backend
- license
- Apache-2.0
# API Conventions
## Adding a REST endpoint
All REST endpoints go in `backend/app/main.py` under the `/api` prefix.
```python
from .schemas import MyRequest, MyResponse
@app.post("/api/my-endpoint", response_model=MyResponse)
async def my_endpoint(req: MyRequest) -> MyResponse:
# Business logic here
return MyResponse(...)
```
Rules:
- Always use a Pydantic `response_model` — never return raw dicts
- Define request/response models in `schemas.py`, not inline
- Use async endpoints — the agent team is async
- Prefix all REST routes with `/api/`
## Pydantic schema conventions
```python
from pydantic import BaseModel
class MyResponse(BaseModel):
"""Docstring explains what this response represents."""
field_name: str
optional_field: str | None = None
items: list[str] = []
```
Rules:
- Use Pydantic v2 API: `model_dump()`, `model_dump_json()`, `model_validate()`
- Provide defaults for optional fields
- Add docstrings — they appear in the auto-generated OpenAPI docs
## WebSocket message handling
The WebSocket handler in `main.py` follows this pattern:
```python
@app.websocket("/ws/my-feature")
async def my_ws(websocket: WebSocket) -> None:
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
payload = json.loads(data)
# Process and send responses
await websocket.send_text(response.model_dump_json())
except WebSocketDisconnect:
pass
```
Rules:
- Always wrap the receive loop in `try/except WebSocketDisconnect`
- Send structured JSON using Pydantic `model_dump_json()`, not hand-built strings
- Send a `{"type": "done"}` message to signal completion to the frontend
- Send `{"type": "error"}` for recoverable errors instead of closing the connection
## Error handling
- REST: let FastAPI's built-in validation handle 422s; raise `HTTPException` for business errors
- WebSocket: send an error message over the socket, don't close it — the frontend stays connected
- Agent errors: catch exceptions from `run_team()`, send as `{"type": "error"}` to the client
## CORS
CORS is configured in `main.py` for `http://localhost:5173` (Vite dev server). For production, replace with the actual frontend origin.
GitHub에서 보기