Introduction
After extracting and normalizing code metadata, DocuGen AI leverages Google’s Gemini API to generate human-readable documentation. The AI acts as a Technical Writer, interpreting metadata and creating clear, professional documentation.The AI generation layer is implemented in
docugen/api/gemini_client.py using the google-genai Python SDK.The GeminiClient Class
TheGeminiClient class (gemini_client.py:23) encapsulates all AI interactions:
Configuration
- API Key: Required authentication for Gemini API
- Model: Defaults to
gemini-3.1-flash-lite-preview(gemini_client.py:11) - System Prompt: Defines the AI’s role and behavior
- Client: Auto-initialized
genai.Clientinstance
The System Prompt
The system prompt (gemini_client.py:12-20) is critical—it defines how the AI behaves:
What the Prompt Achieves
| Instruction | Purpose |
|---|---|
| ”Technical Writer Senior” | Sets expertise level and writing style |
| ”Genera descripción técnica” | Ensures high-level overview is included |
| ”Crea ejemplos de uso” | Forces the AI to generate usage examples |
| ”Tono profesional, conciso” | Maintains consistency and brevity |
| ”Infiere la utilidad” | Handles codebases without docstrings |
| ”Markdown limpio” | Ensures output is properly formatted |
| ”Sin introducciones ni despedidas” | Removes conversational fluff |
The system prompt is in Spanish in this implementation. You can customize it by passing a different
system_prompt parameter during initialization.Metadata Preparation
Before sending data to the AI, the_build_content() method (gemini_client.py:44-56) formats the metadata:
What Gets Sent to Gemini
-
JSON-formatted metadata from the normalization phase:
-
Optional user prompt for custom instructions:
The metadata is serialized with
ensure_ascii=False to preserve Unicode characters (useful for non-English codebases) and indent=2 for readability.Making the API Call
Thegenerate_markdown() method (gemini_client.py:77-89) orchestrates the entire generation process:
Request Configuration
- model: The Gemini model ID (e.g.,
gemini-3.1-flash-lite-preview) - contents: The formatted metadata + user prompt
- config.system_instruction: The Technical Writer system prompt
Response Extraction
Gemini API responses can vary in structure. The_extract_text() method (gemini_client.py:58-75) handles multiple response formats:
Why This Complexity?
- Direct access: Some models return
response.textdirectly - Structured responses: Others return
response.candidates[0].content.parts[0].text - Multi-part responses: Text may be split across multiple parts
The method uses
getattr() with fallbacks to avoid AttributeError exceptions when the response structure differs from expectations.Error Handling
API Call Failures
All exceptions during the API call are caught and wrapped with context (gemini_client.py:86-87):
- Invalid API key
- Network timeouts
- Rate limiting
- Model unavailability
Empty Responses
If the response doesn’t contain text, an error is raised (gemini_client.py:75):
Client Initialization
The_build_client() static method (gemini_client.py:38-42) handles SDK initialization:
Dependency Check
The module attempts to importgoogle.genai at the top (gemini_client.py:6-9):
google-genai isn’t installed (useful for testing or alternative backends).
Integration with Templates
After generation, the AI-produced Markdown is combined with project metadata using theTemplateEngine (templates/engine.py:27):
- Project name as the title
- AI-generated content as the main body
- Summary statistics (file count, class count, etc.)
Example Workflow
Here’s how the pieces fit together:Customization Options
Use a Different Model
Custom System Prompt
Provide User Instructions
User prompts are appended to the metadata, allowing you to guide the AI’s focus without changing the system prompt.
Best Practices
- API Key Security: Store API keys in environment variables, never in code
- Error Handling: Wrap API calls in try/except blocks to handle network issues gracefully
- Rate Limiting: Implement retry logic with exponential backoff for production use
- Prompt Tuning: Experiment with system prompts to match your documentation style
- Model Selection: Use Flash models for speed, Pro models for complex codebases
Next Steps
Architecture Overview
See how AI generation fits into the complete pipeline
CLI Reference
Learn how to use the CLI to generate documentation
