Server Skills for LlamaFarm
Framework-specific patterns and code review checklists for the LlamaFarm Server component.
Overview
| Property |
Value |
| Path |
server/ |
| Python |
3.12+ |
| Framework |
FastAPI 0.116+ |
| Task Queue |
Celery 5.5+ |
| Validation |
Pydantic 2.x, pydantic-settings |
| Logging |
structlog with FastAPIStructLogger |
Links to Shared Skills
This skill extends the shared Python skills. See:
- Python Patterns - Dataclasses, comprehensions, imports
- Async Patterns - async/await, asyncio, concurrency
- Typing Patterns - Type hints, generics, Pydantic
- Testing Patterns - Pytest, fixtures, mocking
- Error Handling - Exceptions, logging, context managers
- Security Patterns - Path traversal, injection, secrets
Server-Specific Checklists
| Topic |
File |
Key Points |
| FastAPI |
fastapi.md |
Routes, dependencies, middleware, exception handlers |
| Celery |
celery.md |
Task patterns, error handling, retries, signatures |
| Pydantic |
pydantic.md |
Pydantic v2 models, validation, serialization |
| Performance |
performance.md |
Async patterns, caching, connection pooling |
Architecture Overview
server/
├── main.py # Uvicorn entry point, MCP mount
├── api/
│ ├── main.py # FastAPI app factory, middleware setup
│ ├── errors.py # Custom exceptions + exception handlers
│ ├── middleware/ # ASGI middleware (structlog, errors)
│ └── routers/ # API route modules
│ ├── projects/ # Project CRUD endpoints
│ ├── datasets/ # Dataset management
│ ├── rag/ # RAG query endpoints
│ └── ...
├── core/
│ ├── settings.py # pydantic-settings configuration
│ ├── logging.py # structlog setup, FastAPIStructLogger
│ └── celery/ # Celery app configuration
│ ├── celery.py # Celery app instance
│ └── rag_client.py # RAG task signatures and helpers
├── services/ # Business logic layer
│ ├── project_service.py # Project CRUD operations
│ ├── dataset_service.py # Dataset management
│ └── ...
├── agents/ # AI agent implementations
└── tests/ # Pytest test suite
Quick Reference
Settings Pattern (pydantic-settings)
from pydantic_settings import BaseSettings
class Settings(BaseSettings, env_file=".env"):
HOST: str = "0.0.0.0"
PORT: int = 14345
LOG_LEVEL: str = "INFO"
settings = Settings() # Module-level singleton
Structured Logging
from core.logging import FastAPIStructLogger
logger = FastAPIStructLogger(__name__)
logger.info("Operation completed", extra={"count": 10, "duration_ms": 150})
logger.bind(namespace=namespace, project=project_id) # Add context
Custom Exceptions
# Define exception hierarchy
class NotFoundError(Exception): ...
class ProjectNotFoundError(NotFoundError):
def __init__(self, namespace: str, project_id: str):
self.namespace = namespace
self.project_id = project_id
super().__init__(f"Project {namespace}/{project_id} not found")
# Register handler in api/errors.py
async def _handle_project_not_found(request: Request, exc: Exception) -> Response:
payload = ErrorResponse(error="ProjectNotFound", message=str(exc))
return JSONResponse(status_code=404, content=payload.model_dump())
def register_exception_handlers(app: FastAPI) -> None:
app.add_exception_handler(ProjectNotFoundError, _handle_project_not_found)
Service Layer Pattern
class ProjectService:
@classmethod
def get_project(cls, namespace: str, project_id: str) -> Project:
project_dir = cls.get_project_dir(namespace, project_id)
if not os.path.isdir(project_dir):
raise ProjectNotFoundError(namespace, project_id)
# ... load and validate
Review Checklist Summary
FastAPI Routes (High priority)
- Proper async/sync function choice
- Response model defined with
response_model=
- OpenAPI metadata (operation_id, tags, summary)
- HTTPException with proper status codes
Celery Tasks (High priority)
- Use signatures for cross-service calls
- Implement proper timeout and polling
- Handle task failures gracefully
- Store group metadata for parallel tasks
Pydantic Models (Medium priority)
- Use Pydantic v2 patterns (model_config, Field)
- Proper validation with field constraints
- Serialization with model_dump()
Performance (Medium priority)
- Avoid blocking calls in async functions
- Use proper connection pooling for external services
- Implement caching where appropriate
See individual topic files for detailed checklists with grep patterns.
1---2name: server-skills3description: Server-specific best practices for FastAPI, Celery, and Pydantic. Extends python-skills with framework-specific patterns.4---5
6# Server Skills for LlamaFarm
7
8Framework-specific patterns and code review checklists for the LlamaFarm Server component.
9
10## Overview
11
12| Property | Value |
13|----------|-------|
14| Path | `server/` |
15| Python | 3.12+ |
16| Framework | FastAPI 0.116+ |
17| Task Queue | Celery 5.5+ |
18| Validation | Pydantic 2.x, pydantic-settings |
19| Logging | structlog with FastAPIStructLogger |
20
21## Links to Shared Skills
22
23This skill extends the shared Python skills. See:
24
25- [Python Patterns](../python-skills/patterns.md) - Dataclasses, comprehensions, imports
26- [Async Patterns](../python-skills/async.md) - async/await, asyncio, concurrency
27- [Typing Patterns](../python-skills/typing.md) - Type hints, generics, Pydantic
28- [Testing Patterns](../python-skills/testing.md) - Pytest, fixtures, mocking
29- [Error Handling](../python-skills/error-handling.md) - Exceptions, logging, context managers
30- [Security Patterns](../python-skills/security.md) - Path traversal, injection, secrets
31
32## Server-Specific Checklists
33
34| Topic | File | Key Points |
35|-------|------|------------|
36| FastAPI | [fastapi.md](fastapi.md) | Routes, dependencies, middleware, exception handlers |
37| Celery | [celery.md](celery.md) | Task patterns, error handling, retries, signatures |
38| Pydantic | [pydantic.md](pydantic.md) | Pydantic v2 models, validation, serialization |
39| Performance | [performance.md](performance.md) | Async patterns, caching, connection pooling |
40
41## Architecture Overview
42
43```
44server/
45├── main.py # Uvicorn entry point, MCP mount
46├── api/
47│ ├── main.py # FastAPI app factory, middleware setup
48│ ├── errors.py # Custom exceptions + exception handlers
49│ ├── middleware/ # ASGI middleware (structlog, errors)
50│ └── routers/ # API route modules
51│ ├── projects/ # Project CRUD endpoints
52│ ├── datasets/ # Dataset management
53│ ├── rag/ # RAG query endpoints
54│ └── ...
55├── core/
56│ ├── settings.py # pydantic-settings configuration
57│ ├── logging.py # structlog setup, FastAPIStructLogger
58│ └── celery/ # Celery app configuration
59│ ├── celery.py # Celery app instance
60│ └── rag_client.py # RAG task signatures and helpers
61├── services/ # Business logic layer
62│ ├── project_service.py # Project CRUD operations
63│ ├── dataset_service.py # Dataset management
64│ └── ...
65├── agents/ # AI agent implementations
66└── tests/ # Pytest test suite
67```
68
69## Quick Reference
70
71### Settings Pattern (pydantic-settings)
72
73```python
74from pydantic_settings import BaseSettings
75
76class Settings(BaseSettings, env_file=".env"):
77 HOST: str = "0.0.0.0"
78 PORT: int = 14345
79 LOG_LEVEL: str = "INFO"
80
81settings = Settings() # Module-level singleton
82```
83
84### Structured Logging
85
86```python
87from core.logging import FastAPIStructLogger
88
89logger = FastAPIStructLogger(__name__)
90logger.info("Operation completed", extra={"count": 10, "duration_ms": 150})
91logger.bind(namespace=namespace, project=project_id) # Add context
92```
93
94### Custom Exceptions
95
96```python
97# Define exception hierarchy
98class NotFoundError(Exception): ...
99class ProjectNotFoundError(NotFoundError):
100 def __init__(self, namespace: str, project_id: str):
101 self.namespace = namespace
102 self.project_id = project_id
103 super().__init__(f"Project {namespace}/{project_id} not found")
104
105# Register handler in api/errors.py
106async def _handle_project_not_found(request: Request, exc: Exception) -> Response:
107 payload = ErrorResponse(error="ProjectNotFound", message=str(exc))
108 return JSONResponse(status_code=404, content=payload.model_dump())
109
110def register_exception_handlers(app: FastAPI) -> None:
111 app.add_exception_handler(ProjectNotFoundError, _handle_project_not_found)
112```
113
114### Service Layer Pattern
115
116```python
117class ProjectService:
118 @classmethod
119 def get_project(cls, namespace: str, project_id: str) -> Project:
120 project_dir = cls.get_project_dir(namespace, project_id)
121 if not os.path.isdir(project_dir):
122 raise ProjectNotFoundError(namespace, project_id)
123 # ... load and validate
124```
125
126## Review Checklist Summary
127
1281. **FastAPI Routes** (High priority)
129 - Proper async/sync function choice
130 - Response model defined with `response_model=`
131 - OpenAPI metadata (operation_id, tags, summary)
132 - HTTPException with proper status codes
133
1342. **Celery Tasks** (High priority)
135 - Use signatures for cross-service calls
136 - Implement proper timeout and polling
137 - Handle task failures gracefully
138 - Store group metadata for parallel tasks
139
1403. **Pydantic Models** (Medium priority)
141 - Use Pydantic v2 patterns (model_config, Field)
142 - Proper validation with field constraints
143 - Serialization with model_dump()
144
1454. **Performance** (Medium priority)
146 - Avoid blocking calls in async functions
147 - Use proper connection pooling for external services
148 - Implement caching where appropriate
149
150See individual topic files for detailed checklists with grep patterns.