Parallel icon
Request icon
ChatCompletion icon
Script icon
If icon
ForEach icon
Queries icon
MailSend icon
Schedule icon

AI-Powered Inventory Demand Forecasting, Auto-Generate Purchase Orders with OpenAI

Automate inventory management with Kestra and OpenAI GPT-4o. Forecast 7-day demand, auto-generate purchase orders for stockout-risk SKUs, and log them to PostgreSQL.

Categories
AIBusinessData

Automate supply chain replenishment end to end. This blueprint pulls live warehouse inventory and sales velocity every 6 hours, sends both to OpenAI GPT-4o for a structured 7-day demand forecast, then automatically generates purchase orders for SKUs at risk of stockout, submits them to your supplier, logs them to PostgreSQL, and emails your team a digest. It replaces manual stockout monitoring and static reorder points with AI-driven demand prediction, helping retail, e-commerce, and manufacturing teams cut both stockouts and excess inventory.

How it works

  1. A io.kestra.plugin.core.trigger.Schedule trigger fires on cron 0 */6 * * *, running the flow every 6 hours.
  2. fetch_data, a io.kestra.plugin.core.flow.Parallel task, runs two io.kestra.plugin.core.http.Request calls at once: fetch_inventory and fetch_sales_velocity, both authenticated with a bearer token.
  3. ai_demand_forecast (io.kestra.plugin.openai.ChatCompletion, model gpt-4o) receives the combined data and returns a JSON-Schema-constrained forecast with predicted_demand_7d, reorder_needed, recommended_quantity, confidence, and reasoning per SKU.
  4. filter_and_create_orders, a io.kestra.plugin.scripts.python.Script, keeps only items where reorder_needed is true and builds purchase orders with generated PO numbers, emitting them via Kestra.outputs.
  5. process_orders, a io.kestra.plugin.core.flow.If, runs only when has_orders is true. Inside, a io.kestra.plugin.core.flow.ForEach (concurrencyLimit: 3) POSTs each order to the supplier API, log_to_postgres upserts them with io.kestra.plugin.jdbc.postgresql.Queries, and notify_team sends a summary via io.kestra.plugin.email.MailSend.

What you get

  • A hands-off reorder loop running every 6 hours
  • GPT-4o forecasts returned as validated JSON via jsonResponseSchema
  • Parallel supplier submission with bounded concurrency
  • An idempotent PostgreSQL audit log (ON CONFLICT DO NOTHING)
  • An HTML email digest with quantities and AI confidence scores

Who it's for

  • Retail and e-commerce teams automating replenishment
  • Manufacturing operations balancing raw material stock against production
  • Supply chain and procurement teams replacing rule-based reorder points with AI

Why orchestrate this with Kestra

OpenAI has no scheduler, no retry logic, and no way to gate downstream supplier writes on a forecast result. Kestra supplies the event trigger, parallel and conditional control flow, per-task retries, and full execution lineage across HTTP, AI, Python, JDBC, and email steps. The entire pipeline is declarative YAML you can version, review, and replay, with every input, output, and forecast captured for audit.

Prerequisites

  • A Kestra instance (0.18.0+ recommended)
  • Reachable inventory, sales, supplier, and ERP API endpoints
  • A PostgreSQL database with a purchase_orders table
  • An OpenAI API key with gpt-4o access

Secrets

  • WAREHOUSE_API_TOKEN: bearer token for the inventory and sales APIs
  • OPENAI_API_KEY: OpenAI API key for GPT-4o
  • SUPPLIER_API_TOKEN: bearer token for the supplier purchase order API
  • POSTGRES_URL, POSTGRES_USER, POSTGRES_PASSWORD: PostgreSQL connection
  • ALERT_EMAIL_FROM, ALERT_EMAIL_TO: email sender and recipient
  • SMTP_HOST, SMTP_USERNAME, SMTP_PASSWORD: SMTP server credentials

Quick start

  1. Add the blueprint to your namespace and define the secrets above.
  2. Set the four input URLs (inventory, sales, supplier, ERP) to your endpoints.
  3. Create the purchase_orders table with a unique constraint on po_number.
  4. Run once manually to confirm the forecast and order logic, then let the schedule take over.

How to extend

  • Add a human-in-the-loop Pause task to approve orders before submission.
  • Push confirmed orders into your ERP (SAP, Oracle, NetSuite) via its REST API.
  • Add a Slack alert for high-confidence recommendations.
  • Swap or tune the model, or branch on confidence to auto-approve only above a threshold.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.