A production-minded FastAPI demo for validating, classifying, and processing customer support requests.
The project demonstrates practical backend patterns for AI-powered applications:
- Clean REST API endpoints
- Pydantic request and response validation
- Async request handling
- Optional OpenAI integration
- Mock fallback without API credentials
- Background job submission and status polling
- Structured provider error handling
- Automatic OpenAPI / Swagger documentation
- Automated API tests
- Dockerized local deployment
A customer support application can submit a customer message and receive:
- A support category
- A priority level
- A suggested customer-facing reply
- The processing mode used (
mockoropenai)
Long-running requests can also be submitted as background jobs.
Client
|
v
FastAPI endpoints
|
+--> Pydantic validation
|
+--> Support classification
|
+--> OpenAI service or mock fallback
|
+--> Structured API response
Background workflow:
POST /support/jobs
|
v
202 Accepted + job_id
|
v
Background processing
|
v
GET /support/jobs/{job_id}
|
v
completed / failed result
GET /healthExample response:
{
"status": "ok",
"ai_mode": "mock"
}POST /support/analyzeExample request:
{
"message": "My order has not arrived and I need an update.",
"priority": "high"
}Example response:
{
"category": "shipping",
"priority": "high",
"suggested_reply": "Thank you for contacting our support team. We have received your request and will review it shortly.",
"processing_mode": "mock"
}POST /support/jobsThe endpoint returns 202 Accepted with a job identifier:
{
"job_id": "example-job-id",
"status": "queued",
"status_url": "/support/jobs/example-job-id"
}GET /support/jobs/{job_id}Example completed response:
{
"job_id": "example-job-id",
"status": "completed",
"result": {
"category": "shipping",
"priority": "high",
"suggested_reply": "Thank you for contacting our support team. We have received your request and will review it shortly.",
"processing_mode": "mock"
},
"error": null
}Windows PowerShell:
python -m venv .venv
.\.venv\Scripts\Activate.ps1pip install -r requirements.txtCopy the example environment file:
Copy-Item .env.example .envThe application works in mock mode when no API key is provided:
OPENAI_API_KEY=
OPENAI_MODEL=your-available-model-nameuvicorn app.main:app --reload --port 8010Open:
- API documentation:
http://127.0.0.1:8010/docs - Health endpoint:
http://127.0.0.1:8010/health
Run the complete test suite:
pytest -vThe tests cover:
- Health endpoint behavior
- Valid support request processing
- Pydantic validation failures
- Background job processing
- Unknown job handling
Build the image:
docker build -t ai-support-api .Run it with an isolated host port:
docker run --rm --name ai-support-api-demo -p 18010:8010 ai-support-apiOpen:
http://127.0.0.1:18010/docshttp://127.0.0.1:18010/health
External AI requests are network-bound operations, so the provider integration uses an asynchronous client rather than blocking the server thread.
The application remains fully testable without paid API credentials. This makes local development and automated testing predictable and inexpensive.
FastAPI BackgroundTasks and an in-memory job store are used to demonstrate the request pattern clearly.
For a production deployment, the in-memory implementation should be replaced with durable infrastructure such as Redis, a task queue, or a managed cloud queue.
Pydantic models enforce message length, allowed priority values, and consistent response structures. Invalid input receives a standard 422 response.
Timeouts, connection failures, and upstream API errors are converted into clear HTTP responses instead of exposing raw exceptions.
- Background job state is stored in memory and is lost when the application restarts.
- The mock classifier uses simple keyword matching.
- Authentication and rate limiting are not yet implemented.
- No persistent database is currently used.
- Real AI responses require valid provider credentials and API billing.
Potential next steps include:
- Redis-backed job storage
- Celery, RQ, or a managed cloud queue
- Authentication and rate limiting
- Persistent conversation history
- Structured model outputs
- Logging and observability
- Database integration
- CI/CD deployment
- Retrieval-augmented support answers
The current version includes:
- Working FastAPI endpoints
- Pydantic validation
- Async request handling
- Mock and optional AI processing modes
- Background job submission and polling
- Automated tests
- Docker support
- Swagger documentation