Structured Generative AI Workout Architect
By (author)BUSHRA Khalid
Tech stack:
PY
A robust Python module that uses Google's Gemini to generate highly personalized workout plans while strictly adhering to a predefined exercise librar...
read more
read more
AI Contributions:0
Published At:August 13, 2026
Files Included:1 file(s)
Updated At:October 22, 2026
Read Me
# Structured Generative AI Workout Architect
This module bridges the gap between the creative power of Large Language Models (like Google Gemini) and the strict safety requirements of a fitness application.
## Features
1. **Context-Aware Prompting**: The AI is fed not only the user's goals and equipment, but also their *recent performance history* and *recently prescribed plans*. This ensures progressive overload and workout variety, bypassing the common issue of an LLM generating the exact same response for a given user profile.
2. **Structured JSON Output**: Enforces strict JSON schemas on the Gemini API response so the client app always receives a predictable, strongly-typed data structure (blocks, exercises, reps, sets, warmups, cooldowns).
3. **Anti-Hallucination Sanitization**: The most critical feature. Even when explicitly instructed not to, LLMs occasionally hallucinate exercises or ignore physical equipment constraints. The `_sanitize_plan` function acts as a firewall, intercepting the AI's output and cross-referencing every prescribed movement against the definitive database library. Any exercise that the user lacks equipment for, or that doesn't exist in the library, is safely stripped out.
4. **Fallback Safety**: If Gemini times out, returns malformed JSON, or if the sanitization pass strips out too many invalid exercises (dropping below a minimum threshold), the module gracefully delegates to a local, deterministic fallback engine.
This Code Gig is an excellent blueprint for developers looking to build safe, reliable, and strictly structured generative AI features into production applications.
```