h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2h2

27 minute read · February 1, 2025

Building AI Apps with Dremio, LangChain, Flask, and FastAPI

Alex Merced

Alex Merced · Senior Tech Evangelist, Dremio

AI-powered applications transform industries by enabling data-driven decision-making, automated workflows, and intelligent chat interfaces. However, one of the biggest challenges in developing these AI applications is efficiently accessing, integrating, and querying enterprise data from multiple sources.

Many AI models rely on structured data stored in databases, data lakes, and warehouses—but accessing this data in real time and ensuring high performance can be complex. This is where Dremio comes in. By providing a unified data access layer, Dremio allows AI applications to:

Seamlessly connect to structured and semi-structured data across multiple sources
Model data in a semantic layer to provide AI-ready datasets
Accelerate queries using Dremio Reflections for real-time AI responses
Integrate Apache Iceberg tables with other databases and data lakes

We will explore how to build AI-powered applications using Dremio, LangChain, Flask, and FastAPI. We’ll cover:

🔹 Why Dremio is an ideal data platform for AI workloads
🔹 How to build an AI application with Flask (for traditional request-response interactions)
🔹 How to build an AI application with FastAPI (for high-performance, asynchronous AI applications)
🔹 Different approaches to integrating LangChain tools and agents to enhance AI capabilities

Why Use Dremio for AI-Powered Applications?

Building AI applications requires more than just a powerful language model—you need structured, real-time access to enterprise data. Whether you’re building a chatbot for customer insights, an AI assistant for financial analysis, or an automated system for predictive maintenance, the ability to efficiently query and process data is critical.

Many traditional AI applications struggle with:
Fragmented data across databases, data lakes, and warehouses
Slow query performance when retrieving large datasets
Complex ETL pipelines that delay AI insights

This is where Dremio provides a game-changing approach.

Key Benefits of Using Dremio for AI Applications

1. Unified Data Access Across Sources

Dremio enables AI applications to access structured and semi-structured data across a variety of sources, including:

Cloud & On-Prem Storage (Amazon S3, Azure Data Lake, Google Cloud Storage)
Relational Databases (PostgreSQL, MySQL, SQL Server)
Data Warehouses (Snowflake, Redshift)
Lakehouse Table Formats and Catalogs (Apache Iceberg, Delta Lake, Glue, Polaris, Unity, etc.)
Semi-Structured Data (JSON, Parquet, CSV)

With Dremio as a unified data access layer, your AI models can query data from multiple sources without complex ETL workflows.

2. Semantic Layer for AI-Ready Datasets

AI models work best with structured, business-friendly datasets. Dremio allows you to:

🔹 Create virtual datasets that curate raw data into AI-ready tables
🔹 Enforce consistent business logic across AI applications
🔹 Apply data governance policies without duplicating data

By modeling data in Dremio’s semantic layer, AI applications can retrieve meaningful insights without worrying about data transformations.

3. Query Acceleration with Dremio Reflections

One of the biggest challenges in AI applications is query latency—especially when dealing with large datasets. Dremio Reflections solve this by precomputing and caching query results, which means:

Faster AI-powered responses with near-instant query performance
Lower compute costs by avoiding redundant queries
Optimized performance for federated data sources

With Dremio-powered query acceleration, AI applications can interact with structured data in real-time, making them more responsive and scalable.

4. Iceberg Lakehouse Integration for AI Workloads

For AI applications that rely on transactional, time-travel, or historical data, Apache Iceberg provides:

ACID transactions for reliable data consistency
Schema evolution without breaking existing queries
Partition pruning and metadata indexing for faster AI model execution

Dremio allows AI applications to query Apache Iceberg tables seamlessly—enabling AI-driven predictive analytics, anomaly detection, and automated decision-making.

Why This Matters for AI Applications

By using Dremio as the data backbone for AI applications, developers can:

Access enterprise data in real-time without ETL overhead
Accelerate AI queries with Reflections for instant performance
Enable AI-driven analytics on Apache Iceberg lakehouses
Simplify AI workflows with Python-native data retrieval

Building an AI-Powered Application with Flask

Now that we understand why Dremio is a powerful data platform for AI applications, let’s build our first AI-powered application using Flask. Flask is a lightweight web framework that allows us to create a simple, interactive AI assistant that retrieves structured data from Dremio using LangChain and responds to user queries.

In this section, we will:
Set up the project environment
Connect Flask to Dremio using dremio-simple-query
Define LangChain tools for AI-driven data retrieval
Build a Flask-based AI assistant that interacts with users in real-time

1. Setting Up the Project

Install Dependencies

First, let’s install the required Python packages:

pip install flask langchain langchain_openai dremio-simple-query pandas polars duckdb dotenv

Project Structure

Organize your project as follows:

ai_flask_app/
├── app.py                 # Flask application
├── templates/
│   ├── index.html         # UI for chatbot
├── static/
│   ├── styles.css         # (Optional) Styles for the UI
├── .env                   # Store API keys & credentials
├── requirements.txt       # Dependencies
├── .gitignore			   # To git ignore .env 
└── README.md              # Project documentation

2. Connecting Flask to Dremio

Since our AI assistant will need to query structured data from Dremio, we first need to establish a secure connection.

Step 1: Store Dremio Credentials in .env

Create a .env file to store authentication details:

DREMIO_TOKEN=your_dremio_token_here
DREMIO_ARROW_ENDPOINT=grpc://your_dremio_instance:32010
OPENAI_API_KEY=your_openai_api_key_here
SECRET_KEY=your_flask_secret_key

Step 2: Establish the Connection in app.py

from dremio_simple_query.connect import DremioConnection
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Retrieve Dremio credentials
TOKEN = os.getenv("DREMIO_TOKEN")
ARROW_ENDPOINT = os.getenv("DREMIO_ARROW_ENDPOINT")

# Establish connection to Dremio
dremio = DremioConnection(TOKEN, ARROW_ENDPOINT)

Now, our Flask application can retrieve structured data from Dremio.

3. Defining LangChain Tools for AI-Powered Queries

LangChain tools allow our AI agent to interact with structured data sources dynamically.

Tool 1: Retrieve Customer Information

This tool fetches customer details (e.g., name, purchase history, total spend) from Dremio.

from langchain.agents import Tool

def get_customer_data(customer_id: str):
    query = f"SELECT * FROM customers WHERE id = '{customer_id}';"
    df = dremio.toPandas(query)
    return df.to_string(index=False) if not df.empty else "No data found."

get_customer_tool = Tool(
    name="get_customer_data",
    func=get_customer_data,
    description="Fetches customer details from Dremio."
)

Tool 2: Retrieve Recent Transactions

This tool fetches the last 5 transactions for a given customer.

def get_recent_transactions(customer_id: str):
    query = f"""
    SELECT transaction_id, amount, date FROM transactions
    WHERE customer_id = '{customer_id}'
    ORDER BY date DESC
    LIMIT 5;
    """
    df = dremio.toPandas(query)
    return df.to_string(index=False) if not df.empty else "No transactions found."

get_transactions_tool = Tool(
    name="get_recent_transactions",
    func=get_recent_transactions,
    description="Fetches recent transactions for a given customer ID."
)

4. Creating the Flask AI Chatbot

Now, we integrate LangChain, OpenAI, and Flask to create an interactive AI assistant.

Step 1: Initialize LangChain Memory & AI Model

We’ll use LangChain’s memory to keep track of past conversations.

from flask import Flask, request, render_template, session
from langchain.memory import ConversationBufferMemory
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent

# Initialize Flask app
app = Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY")

# Initialize AI memory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

# Initialize AI model (GPT-4)
chat_model = ChatOpenAI(model_name="gpt-4o", openai_api_key=os.getenv("OPENAI_API_KEY"))

# Define AI agent with tools
tools = [get_customer_tool, get_transactions_tool]
agent = initialize_agent(tools, chat_model, agent="chat-conversational-react-description", memory=memory)

Step 2: Create Flask Routes

We’ll define a Flask route to handle user interactions.

@app.route("/", methods=["GET", "POST"])
def index():
    response = None

    # Reset chat history on refresh
    if request.method == "GET":
        session.clear()

    if "chat_history" not in session:
        session["chat_history"] = []

    if request.method == "POST":
        user_question = request.form["question"]

        # Store conversation history
        past_chat = "\n".join([f"You: {msg['question']}\nAI: {msg['answer']}" for msg in session["chat_history"]])
        full_prompt = f"""
        You are an AI assistant that helps users retrieve customer insights.
        User's Question: {user_question}
        Previous Conversations: {past_chat}
        """

        # Run AI agent
        response = agent.run({"input": full_prompt})

        # Save conversation history
        session["chat_history"].append({"question": user_question, "answer": response})
        session.modified = True  

    return render_template("index.html", chat_history=session["chat_history"], response=response)

if __name__ == "__main__":
    app.run(debug=True)

5. Creating the Chat UI (index.html)

Create a simple web interface for interacting with the AI assistant.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>AI Customer Assistant</title>
</head>
<body>
    <h2>AI Customer Assistant</h2>
    <form method="POST">
        <input type="text" name="question" placeholder="Ask me a question..." required>
        <button type="submit">Submit</button>
    </form>

    {% if response %}
        <p><strong>AI:</strong> {{ response }}</p>
    {% endif %}

    <h3>Chat History:</h3>
    {% for chat in chat_history %}
        <p><strong>You:</strong> {{ chat.question }}</p>
        <p><strong>AI:</strong> {{ chat.answer }}</p>
    {% endfor %}
</body>
</html>

6. Running the Flask AI Application

Run the Flask server:

python app.py

Open a browser and go to localhost:5000 to interact with the AI assistant.

We’ve successfully built an AI-powered customer assistant that retrieves structured data from Dremio and interacts with users in real-time.

Building an AI-Powered Application with FastAPI

While Flask provides a simple way to build AI-powered applications, it is synchronous, meaning it can handle only one request at a time (while has some newer asynchronous features, still not originally designed for asynchronous request processing). FastAPI is a better alternative for AI applications that need to handle multiple concurrent requests efficiently.

In this section, we will:
Set up the FastAPI project
Connect FastAPI to Dremio using dremio-simple-query
Define LangChain tools for AI-powered data retrieval
Build an AI-powered API using FastAPI

1. Setting Up the FastAPI Project

Install Dependencies

First, install the required Python packages:

pip install fastapi uvicorn langchain langchain_openai dremio-simple-query pandas polars duckdb dotenv

Project Structure

Organize your project as follows:

ai_fastapi_app/
├── main.py                 # FastAPI application
├── .env                    # Store API keys & credentials
├── requirements.txt        # Dependencies
└── README.md               # Project documentation

2. Connecting FastAPI to Dremio

Just like in our Flask application, we need to establish a secure connection to Dremio.

Step 1: Store Dremio Credentials in .env

Create a .env file to store authentication details:

DREMIO_TOKEN=your_dremio_token_here
DREMIO_ARROW_ENDPOINT=grpc://your_dremio_instance:32010
OPENAI_API_KEY=your_openai_api_key_here

Step 2: Establish the Connection in main.py

from dremio_simple_query.connect import DremioConnection
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Retrieve Dremio credentials
TOKEN = os.getenv("DREMIO_TOKEN")
ARROW_ENDPOINT = os.getenv("DREMIO_ARROW_ENDPOINT")

# Establish connection to Dremio
dremio = DremioConnection(TOKEN, ARROW_ENDPOINT)

3. Defining LangChain Tools for AI-Powered Queries

LangChain tools will allow our AI-powered API to interact dynamically with structured data sources.

Tool 1: Retrieve Product Inventory

This tool fetches product stock availability from Dremio.

from langchain.agents import Tool

def get_product_inventory(product_id: str):
    query = f"SELECT product_name, stock_quantity FROM inventory WHERE product_id = '{product_id}';"
    df = dremio.toPandas(query)
    return df.to_string(index=False) if not df.empty else "No data found."

get_inventory_tool = Tool(
    name="get_product_inventory",
    func=get_product_inventory,
    description="Fetches product inventory details from Dremio."
)

Tool 2: Retrieve Order Status

This tool fetches customer order status.

def get_order_status(order_id: str):
    query = f"""
    SELECT order_id, status, estimated_delivery 
    FROM orders WHERE order_id = '{order_id}';
    """
    df = dremio.toPandas(query)
    return df.to_string(index=False) if not df.empty else "No order found."

get_order_status_tool = Tool(
    name="get_order_status",
    func=get_order_status,
    description="Fetches order status from Dremio."
)

4. Creating the FastAPI AI Chatbot

Now, let’s integrate LangChain, OpenAI, and FastAPI to build a REST API that handles AI-powered queries.

Step 1: Initialize LangChain Memory & AI Model

We use LangChain’s memory to track past conversations.

from fastapi import FastAPI, Query
from langchain.memory import ConversationBufferMemory
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent

# Initialize FastAPI app
app = FastAPI()

# Initialize AI memory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

# Define AI model (GPT-4)
chat_model = ChatOpenAI(model_name="gpt-4o", openai_api_key=os.getenv("OPENAI_API_KEY"))

# Define AI agent with tools
tools = [get_inventory_tool, get_order_status_tool]
agent = initialize_agent(tools, chat_model, agent="chat-conversational-react-description", memory=memory)

Step 2: Create FastAPI Routes

Define API endpoints for AI interactions.

@app.get("/")
def home():
    return {"message": "Welcome to the AI-powered FastAPI Assistant!"}

@app.get("/ask/")
async def ask(question: str = Query(..., description="Enter your AI query")):
    """
    Asks the AI assistant a question and returns a response.
    """
    response = agent.run({"input": question})
    return {"response": response}

5. Running the FastAPI AI Application

Run the FastAPI server using Uvicorn, an ASGI web server optimized for async requests:

uvicorn main:app --reload

Once the server is running, open your browser and visit:
📌 http://localhost:8000/ – Home Page
📌 http://localhost:8000/docs – Interactive API Documentation

6. Testing AI API Endpoints

Querying the AI Assistant

Send a GET request to the /ask/ endpoint with a sample question:

GET http://127.0.0.1:8000/ask/?question=What%20is%20the%20order%20status%20for%20order%20ID%2056789?

Example API Response

{
    "response": "Order ID: 56789, Status: Shipped, Estimated Delivery: 2024-02-10"
}

This AI-powered API retrieves real-time structured data from Dremio and returns natural language insights to the user.

7. Comparing Flask vs. FastAPI for AI Applications

FeatureFlaskFastAPI
Request HandlingOne request at a timeHandles multiple concurrent requests
API DocumentationManual setupBuilt-in interactive docs (/docs)
Use CaseSimple AI chatbotsHigh-performance AI APIs

🔹 Use Flask for basic AI chat applications where synchronous processing is sufficient.
🔹 Use FastAPI for scalable AI APIs that handle high-volume concurrent requests.

Expanding AI Capabilities with LangChain

Now that we’ve built two AI-powered applications using Dremio, LangChain, Flask, and FastAPI, here are some next steps you can take:

🚀 Enhance AI responses by integrating retrieval-augmented generation (RAG).
🚀 Deploy the AI API to cloud platforms like AWS Lambda, GCP, or Azure Functions.
🚀 Incorporate streaming AI queries with WebSockets and FastAPI’s async support.
🚀 Experiment with multi-agent AI workflows to handle complex queries across multiple datasets.

You can build scalable AI applications that deliver meaningful business insights by leveraging Dremio’s unified data access, LangChain’s intelligent AI framework, and FastAPI’s high-performance capabilities.

Conclusion: Unlocking AI-Powered Data Applications with Dremio, LangChain, Flask, and FastAPI

Building AI-powered applications requires more than a powerful language model—it requires seamless, real-time access to structured data from multiple sources. Dremio, in combination with LangChain, Flask, and FastAPI, enables developers to create scalable, intelligent AI applications that can query and analyze data dynamically.

Key Takeaways

Dremio provides a unified data access layer, allowing AI applications to query structured and semi-structured data from databases, data lakes, and warehouses without complex ETL processes.

LangChain’s AI agents empower applications with intelligent data retrieval, memory, and decision-making capabilities, making it easier to build conversational AI assistants.

Flask is ideal for simple AI-powered web applications, offering an easy way to build interactive chatbots and AI assistants.

FastAPI is better suited for high-performance, scalable AI APIs, supporting asynchronous AI requests and real-time data processing.

Dremio Reflections enable fast AI-driven analytics, ensuring low-latency queries and cost-efficient data processing for AI workloads.

Flask vs. FastAPI: When to Use Each for AI Applications

FeatureFlaskFastAPI
Best forSimple AI chatbotsHigh-performance AI APIs
Concurrent RequestsOne at a timeHandles multiple concurrently
API DocumentationManual setupBuilt-in interactive docs (/docs)
Use CaseWeb-based AI assistantsScalable, production-ready AI services

🚀 Flask is excellent for basic AI applications, such as an interactive chatbot UI.
⚡ FastAPI is the best choice for building AI-driven APIs that require high performance, real-time analytics, and concurrent request handling.

Expanding AI Capabilities

🔹 Enhance AI-driven responses using Retrieval-Augmented Generation (RAG) with Dremio’s structured datasets.
🔹 Deploy your AI applications to cloud platforms like AWS, GCP, or Azure for scalability and production use.
🔹 Incorporate AI-powered analytics dashboards with Streamlit, Dash, or React.
🔹 Use WebSockets with FastAPI to enable real-time AI streaming interactions.
🔹 Experiment with LangChain multi-agent workflows to build intelligent AI systems that handle complex queries dynamically.

Why Dremio is the Ideal Data Platform for AI

Dremio is a game-changer for AI applications because it allows real-time, federated data access without requiring expensive ETL pipelines. By leveraging Dremio’s query acceleration, Iceberg support, and Python-native integration, AI developers can:

Eliminate data silos by unifying structured and unstructured data.
Optimize AI query performance using Dremio’s Reflections and caching mechanisms.
Enable self-service analytics with a semantic layer that simplifies AI-ready datasets.
Scale AI workloads efficiently, reducing compute costs and response times.

With Dremio + LangChain + Flask/FastAPI, you can build the next generation of AI-driven applications that provide real-time, data-powered insights.

Learn more about evaluating Dremio and scheduling a free architectural workshop.

Ready to Get Started?

Enable the business to create and consume data products powered by Apache Iceberg, accelerating AI and analytics initiatives and dramatically reducing costs.