Adrian Bourke | Data & AI Engineering

AI Game Category Backend

This project is a backend service for generating creative MASH game categories and options using a local LLM (Ollama).

Personal Motivation & Background

Why I Built This Backend

I wanted to deepen my understanding of modern Python web frameworks, especially FastAPI and Pydantic. My focus was on designing robust API endpoints and learning how to structure backend logic for AI-driven applications. I worked on the backend only, integrating with a frontend I did not build.

The main challenge was orchestrating a local Ollama LLM (like llama3) with FastAPI and Pydantic to generate and validate creative game outputs. I had to ensure the backend could reliably call the LLM, parse its responses, and use Pydantic models to enforce structure and handle errors—especially since the frontend was out of my control.

Note: I did not create the frontend for this project. My main goal was to learn how to use FastAPI and Pydantic to control and validate backend outputs, and to orchestrate a local LLM (Ollama) for reliable, creative responses.

Project Overview

The Problem

The problem was learning how to validate unpredictable AI outputs for a game system.

Needed to design and validate API outputs for a frontend I did not control.

Wanted to learn how to use Pydantic and FastAPI to host and validate AI outputs.

Dealing with unpredictable AI outputs that needed to conform to specific structures.

The Solution

A backend service using modern Python tools:

  • Used FastAPI to quickly build and document RESTful endpoints
  • Leveraged Pydantic for strict data validation and output schemas
  • Created endpoints where users can input a theme and receive creative game categories
  • Implemented robust parsing of AI outputs to ensure consistent game options

How It Works

The backend exposes a POST /categories endpoint. When a request is received, FastAPI calls the Ollama LLM (via HTTP) to generate creative categories and options for the MASH game, based on the user-supplied theme. The raw LLM output is parsed and validated using Pydantic models before being returned to the client.

POST /categories { "theme": "space", "num_categories": 4, "num_options": 4 } # Response { "theme": "space", "categories": [ { "title": "Planet", "options": [ {"title": "Mars", "state": "waiting"}, {"title": "Venus", "state": "waiting"}, ... ] }, ... ] }

.env configuration is required to point the backend to the correct Ollama model and URL. Docker Compose is recommended for seamless local development.

Get Started

Explore the code and documentation on GitHub to learn more about the AI Game Category Backend.

Tech Stack

Modern Python backend technologies:

API Framework

FastAPI

Data Validation

Pydantic

Server

Uvicorn

Language

Python 3.11

LLM Host

Ollama

Lessons Learned

Handling AI Unpredictability: I learned effective strategies for constraining and validating unpredictable LLM outputs using Pydantic models, making the system more reliable and robust.

API Structure for AI Systems: Creating well-defined schemas and validation rules for both inputs and outputs proved essential when working with AI components that have inherent variability.

Local LLM Integration: Orchestrating Ollama as a local LLM provider taught me about the trade-offs between cloud-based and local AI solutions, and how to create resilient connections between services.

Future Enhancements

Automated Testing

Add more automated tests for endpoints and data validation to catch edge cases and regressions.

Deployment Automation

Set up CI/CD pipelines for automated deployment and integration testing.

API Documentation

Expand and improve API documentation for easier integration with future frontends.