Overview
ThegenerateSubInterests server action uses Google’s Gemini 2.5 Flash model to generate 3-5 highly relevant sub-interests or related topics for a given interest. It includes automatic fallback to mock data if the API key is missing or if an error occurs.
Function Signature
Parameters
The main interest or topic to generate sub-interests for. This will be sent to the Gemini API as context for generation.
Returns
An array of 3-5 strings representing related sub-interests or topics. The array is limited to a maximum of 5 items even if the API returns more.
Behavior
API Key Check
The function first checks for theGEMINI_API_KEY environment variable:
- If missing, logs a warning and returns mock data
- If present, proceeds with the API call
Response Processing
The function expects a raw JSON array from Gemini (without markdown code blocks):- Removes any
\“jsonor```` markdown formatting - Parses the cleaned response as JSON
- Validates that the result is an array
- Returns up to 5 items from the array
Error Handling
If any error occurs during the API call or response parsing:- Logs the error to console
- Returns fallback mock data based on the input interest
Usage Example
Mock Data Fallback
When the API key is missing or an error occurs, the function returns:Environment Variables
Google Gemini API key. Required for AI-generated responses. Without this key, the function falls back to mock data.
Error Cases
| Error Type | Behavior |
|---|---|
| Missing API key | Returns mock data, logs warning |
| Empty response | Throws error, caught by try-catch, returns mock data |
| Invalid JSON | Parse error caught, returns mock data |
| Non-array response | Throws error, returns mock data |
| Network error | Caught by try-catch, returns mock data |
Source Code Location
app/actions.ts:7-47