This n8n workflow automates the generation of video scripts for agricultural products using AI (Google Gemini), with built-in quality control and regeneration capabilities.
The workflow creates farmer-friendly video scripts for agricultural products by:
- Fetching product data from a database
- Generating scripts using Google Gemini AI
- Performing quality control checks
- Automatically regenerating scripts if they don't meet standards
- Updating script status in the database
- Triggering video generation workflows
- Cron_Hourly: Scheduled trigger that runs every hour
- Script-Generation: Webhook trigger for manual script generation
- DB_SelectNextProduct1: Selects the next product without a script from the database
- Func_BuildProductJSON: Formats product data into a structured JSON format
- Nano_Banana_Node: Calls Google Gemini API to generate the initial script
- Edit Fields: Extracts the generated script text from the API response
- DB_InsertScriptPending: Saves the script to database with "pending_approval" status
- Set_CarryIds: Carries forward script and product IDs
- Func_QC_Evaluate: Performs comprehensive quality checks on the generated script
The QC system validates:
- Scene Structure: Exactly 6 scenes with Voice/Visual format
- Content Requirements:
- Composition information
- Dosage and mixing instructions
- Crop suitability and application stage
- Safety precautions
- Advantages and call-to-action
- Switch_QC_Result: Routes the script based on quality assessment:
auto_approve: Script meets all requirementsneeds_edit: Script has minor issues, kept for manual reviewbad_regen: Script has major issues, triggers regeneration
- DB_UpdateScriptApproved: Updates script status to "approved"
- HTTP Request2: Triggers video generation workflow
- DB_KeepPendingApproval: Keeps script in pending status for human review
- HTTP Request: Notifies video generation system
When a script fails quality control, the workflow enters a sophisticated regeneration loop:
- DB_UpdateRegenerating: Marks script as being regenerated
- Set_RegenPayload: Prepares regeneration parameters
- Set_AttemptArray: Creates array for retry attempts (up to 10 attempts)
- Loop Over Items: Iterates through regeneration attempts
- Gemini regeneration HTTP node: Calls Gemini with specific improvement instructions
- Set_RegenScript: Extracts the regenerated script
- Func_QC_Regen: Re-evaluates the regenerated script
- Switch_QC_Regen: Routes based on regeneration results
The regeneration system uses targeted prompts that specifically address the issues found in the original script, making the AI focus on fixing only the problematic areas.
flowchart TD
A[Cron_Hourly] --> B[DB_SelectNextProduct1]
B --> C[Func_BuildProductJSON]
C --> D[Nano_Banana_Node<br/>Gemini API]
D --> E[Edit Fields]
E --> F[DB_InsertScriptPending]
F --> G[Set_CarryIds]
G --> H[Func_QC_Evaluate]
H --> I{Switch_QC_Result}
I -->|auto_approve| J[DB_UpdateScriptApproved]
I -->|needs_edit| K[DB_KeepPendingApproval]
I -->|bad_regen| L[DB_UpdateRegenerating]
J --> M[HTTP Request2<br/>Video Generation]
K --> N[HTTP Request<br/>Manual Review]
L --> O[Set_RegenPayload]
O --> P[Set_AttemptArray]
P --> Q[Loop Over Items]
Q --> R[Gemini Regeneration<br/>HTTP Node]
R --> S[Set_RegenScript]
S --> T[Func_QC_Regen]
T --> U{Switch_QC_Regen}
U -->|auto_approve| V[Set_CarryIds1]
U -->|needs_edit| K
U -->|bad_regen| P
V --> J
W[Script-Generation<br/>Webhook] --> X[Script-callback]
style A fill:#e1f5fe
style W fill:#e1f5fe
style D fill:#fff3e0
style R fill:#fff3e0
style I fill:#f3e5f5
style U fill:#f3e5f5
style J fill:#e8f5e8
style K fill:#fff8e1
style L fill:#ffebee
- Multi-criteria validation: Checks scene structure, content completeness, and format compliance
- Graduated responses: Different actions based on severity of issues
- Automated decision making: Reduces manual intervention for high-quality scripts
- Targeted improvements: AI receives specific feedback about what needs fixing
- Retry mechanism: Up to 10 attempts to generate acceptable scripts
- Loop prevention: Prevents infinite regeneration cycles
- Product management: Selects products without scripts based on priority
- Script tracking: Maintains script status throughout the lifecycle
- Audit trail: Complete history of script generation and modifications
- Video generation: Automatically triggers video creation for approved scripts
- Webhook support: Allows external systems to trigger script generation
- API callbacks: Notifies external systems of script status changes
The workflow enforces strict quality standards for generated scripts:
- 6 scenes exactly: Each with Voice and Visual components
- Scene separation: Uses
---delimiter between scenes - Consistent format: Voice: and Visual: labels for each scene
- Introduction: Product name and basic overview
- Composition: Active ingredients and formulation details
- Dosage & Mixing: Application rates and preparation instructions
- Crops & Application Stage: Target crops and timing
- Safety: Precautions and safety measures
- Advantages & CTA: Benefits and call-to-action (Kissan Ghar)
- Auto-approve: All requirements met
- Needs edit: 1-2 minor issues, human review required
- Bad regeneration: 3+ major issues, automatic regeneration triggered
- Gemini API Key:
AIzaSyAenJhmCjfUqiQ0_iDqNFZbxTQdjUW612g - Database Connection: PostgreSQL connection for product and script data
- Webhook URLs: External system integration endpoints
The workflow expects these database tables:
- products: Product information (id, product_name, company, composition, etc.)
- scripts: Generated scripts (id, product_id, content, status, created_at)
- Script Generation:
/webhook/script-generate - Video Generation:
/webhook/video_gen - Script Callback:
/api/webhooks/n8n/script-callback
The workflow runs automatically every hour, processing products that don't have scripts yet, prioritized by the priority field in the products table.
Scripts can be generated on-demand using the webhook endpoint:
POST /webhook/script-generate
Content-Type: application/json
{
"video_id": "your_video_id"
}Monitor the workflow through:
- Database script status fields
- n8n execution logs
- External system callbacks
The workflow includes robust error handling:
- Retry mechanisms: Automatic regeneration for failed scripts
- Graceful degradation: Manual review fallback for edge cases
- Status tracking: Clear audit trail of all script states
- Loop prevention: Maximum attempt limits prevent infinite loops
This workflow integrates with:
- Product Database: Source of agricultural product information
- Google Gemini AI: Script generation engine
- Video Generation System: Downstream video creation workflow
- External APIs: Webhook-based integrations for system coordination
The workflow serves as a critical component in an automated content creation pipeline for agricultural marketing materials.

