LangGraph Data Analysis Assistant Agent: Step‑by‑Step Project Guide (Part 5)

This tutorial walks you through building a LangGraph-powered data analysis assistant that converts natural language into SQL, executes queries via NL2SQL and NL2Python tools, visualizes results with a Python interpreter, and deploys the agent using LangGraph CLI and Agent Chat UI for end‑to‑end interaction.

Fun with Large Models
Fun with Large Models
Fun with Large Models
LangGraph Data Analysis Assistant Agent: Step‑by‑Step Project Guide (Part 5)

The article continues a series on LangGraph AI Agent development, focusing on a practical project that creates a data analysis assistant with front‑end visualization. The assistant uses the DeepSeek‑3.1 model and the create_react_agent high‑level pre‑built graph API.

Architecture

The assistant’s core consists of two tools: NL2Python and NL2SQL, which translate natural language into executable Python code or SQL statements. The workflow runs the generated SQL via a Python interpreter connected to a MySQL database.

Project Setup

A project directory langgraph_dataanalysis is created with graph.py, requirements.txt, .env, and langgraph.json. The .env file stores the DeepSeek API key, LangSmith tracing flag, database credentials, and other environment variables.

Dependency Installation

langgraph
langchain-core
langchain-deepseek
langchain-tavily
python-dotenv
langsmith
pydantic
matplotlib
seaborn
pandas
IPython
langchain_mcp_adapters
uv
pymysql

These packages are installed in an Anaconda virtual environment langgraphenv via pip install -r requirements.txt.

Tool Functions

SQL Query Tool ( sql_inter ) – Connects to MySQL with pymysql, executes the model‑generated SQL, and returns results as JSON.

# Create SQL query tool
class SQLQuerySchema(BaseModel):
    sql_query: str = Field(description=description)

@tool(args_schema=SQLQuerySchema)
def sql_inter(sql_query: str) -> str:
    load_dotenv(override=True)
    host = os.getenv('HOST')
    user = os.getenv('USER')
    mysql_pw = os.getenv('MYSQL_PW')
    db = os.getenv('DB_NAME')
    port = os.getenv('PORT')
    connection = pymysql.connect(host=host, user=user, passwd=mysql_pw, db=db, port=int(port), charset='utf8')
    try:
        with connection.cursor() as cursor:
            cursor.execute(sql_query)
            results = cursor.fetchall()
    finally:
        connection.close()
    return json.dumps(results, ensure_ascii=False)

Data Extraction Tool ( extract_data ) – Runs a SELECT query, loads the result into a pandas DataFrame, and stores it in a global variable named by the user.

# Create data extraction tool
class ExtractQuerySchema(BaseModel):
    sql_query: str = Field(description="SQL query to extract data from MySQL.")
    df_name: str = Field(description="Name of the pandas variable to store the result.")

@tool(args_schema=ExtractQuerySchema)
def extract_data(sql_query: str, df_name: str) -> str:
    load_dotenv(override=True)
    # connection setup omitted for brevity
    df = pd.read_sql(sql_query, connection)
    globals()[df_name] = df
    return f"Successfully created pandas object `{df_name}` with extracted data."

Python Execution Tool ( python_inter ) – Executes arbitrary non‑visual Python code, handling expression evaluation and variable creation.

# Create Python code execution tool
class PythonCodeInput(BaseModel):
    py_code: str = Field(description="A valid Python code string, e.g., '2 + 2' or multi‑line statements.")

@tool(args_schema=PythonCodeInput)
def python_inter(py_code):
    g = globals()
    try:
        return str(eval(py_code, g))
    except Exception:
        try:
            exec(py_code, g)
        except Exception as e:
            return f"Code execution error: {e}"
        new_vars = set(g.keys()) - set()
        if new_vars:
            result = {var: g[var] for var in new_vars}
            return str(result)
        return "Code executed successfully"

Figure Generation Tool ( fig_inter ) – Executes matplotlib/seaborn code that creates a figure object (named by the user) and saves the image to the public/images folder of the Agent Chat UI.

# Create plotting tool
class FigCodeInput(BaseModel):
    py_code: str = Field(description="Python plotting code using matplotlib/seaborn that creates a figure.")
    fname: str = Field(description="Variable name of the figure object, e.g., 'fig'.")

@tool(args_schema=FigCodeInput)
def fig_inter(py_code: str, fname: str) -> str:
    current_backend = matplotlib.get_backend()
    matplotlib.use('Agg')
    local_vars = {"plt": plt, "pd": pd, "sns": sns}
    base_dir = r"D:\Learning\Learning\大模型\LangChain\Python\langgraph_dataanalysis\agent-chat-ui-main\public"
    images_dir = os.path.join(base_dir, "images")
    os.makedirs(images_dir, exist_ok=True)
    try:
        g = globals()
        exec(py_code, g, local_vars)
        fig = local_vars.get(fname)
        if fig:
            rel_path = os.path.join("images", f"{fname}.png")
            fig.savefig(os.path.join(images_dir, f"{fname}.png"), bbox_inches='tight')
            return f"Image saved at: {rel_path}"
        return "Figure object not found."
    except Exception as e:
        return f"Execution failed: {e}"
    finally:
        plt.close('all')
        matplotlib.use(current_backend)

Prompt Engineering

A detailed system prompt defines the assistant’s role, lists the available tools with usage priorities, and enforces Chinese output. The prompt guides the model to generate correct SQL, call the appropriate tool, and handle data extraction, Python execution, and visualization.

Deployment

The agent graph is instantiated with create_react_agent, the DeepSeek chat model, the tool list, and the prompt. The project is launched locally using langgraph dev, which starts the backend service and provides URLs for the API, LangGraph Studio, and API docs.

Agent Chat UI is cloned from the official repository, installed via npm install, and run with npm run dev. The UI is configured to point to the deployed agent name data_agent and to use the public folder for image assets.

Testing and Demonstration

Examples show the assistant handling queries such as “How many tables are in the database?” (using sql_inter) and retrieving data from a newly created movies table. A more complex request—querying movie ratings, plotting a bar chart, and reporting the highest‑rated movie—demonstrates the full pipeline: NL2SQL → data extraction → Python plotting → image saving → UI display.

Conclusion

The guide demonstrates end‑to‑end development of a LangGraph AI agent that translates natural language into SQL, performs data extraction, runs Python analysis, generates visualizations, and serves the result through a web UI. Readers are encouraged to extend the project with additional tools and prompts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData AnalysisAI AgentNL2SQLLangGraphAgent Chat UI
Fun with Large Models
Written by

Fun with Large Models

Master's graduate from Beijing Institute of Technology, published four top‑journal papers, previously worked as a developer at ByteDance and Alibaba. Currently researching large models at a major state‑owned enterprise. Committed to sharing concise, practical AI large‑model development experience, believing that AI large models will become as essential as PCs in the future. Let's start experimenting now!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.