Back to Articles

WrenAI: The Semantic Context Layer That Keeps LLMs From Wrecking Your Data Governance

[ View on GitHub ]

WrenAI: The Semantic Context Layer That Keeps LLMs From Wrecking Your Data Governance

Hook

Your LLM just generated perfectly valid SQL that calculated Q4 revenue using the wrong currency conversion logic, costing your company $2M in misreported earnings. WrenAI exists because giving AI direct schema access is a governance nightmare waiting to happen.

Context

The text-to-SQL revolution promised developers could skip learning complex schemas and just ask questions in English. But production reality revealed a fundamental problem: LLMs generate syntactically correct SQL that's semantically wrong. They don't know that 'revenue' should exclude refunds, that 'active users' means a login within 30 days, or that the customer table joins to orders through a specific foreign key relationship that respects data residency rules.

Traditional solutions fell into two camps. BI tools added semantic layers for human analysts but weren't designed for programmatic AI access. Meanwhile, RAG-based text-to-SQL systems tried stuffing schema documentation into context windows, leading to inconsistent interpretations and ungoverned data access. WrenAI emerged from Canner's experience building data platforms to solve this with a fundamentally different approach: a standalone semantic context layer that compiles business logic into a format AI agents can reliably consume, backed by a high-performance Rust engine that executes governed queries across 20+ heterogeneous data sources.

Technical Insight

WrenAI's architecture revolves around three core components that work together to bridge the semantic gap between business logic and raw databases. At the foundation sits wren-core, a Rust engine built on Apache DataFusion that compiles MDL (Modeling Definition Language) into optimized SQL. This isn't just a query translator—it's a semantic compiler that understands business entity relationships, metric calculations, and access patterns.

The MDL itself is where WrenAI's governance power lives. Instead of exposing raw database schemas to LLMs, you define business-level abstractions that encode your organization's data semantics. Here's a practical example modeling an e-commerce system:

models:
  - name: customers
    tableReference:
      schema: public
      table: users
    columns:
      - name: customer_id
        expression: user_id
        type: integer
      - name: customer_name
        expression: CONCAT(first_name, ' ', last_name)
        type: varchar
      - name: lifetime_value
        expression: total_spend
        type: decimal

  - name: orders
    tableReference:
      schema: public
      table: transactions
    columns:
      - name: order_id
        type: integer
      - name: customer_id
        type: integer
      - name: net_revenue
        expression: gross_amount - refund_amount - tax_amount
        type: decimal
    relationships:
      - name: customer
        model: customers
        joinType: many_to_one
        condition: orders.customer_id = customers.customer_id

metrics:
  - name: total_revenue
    baseObject: orders
    measure:
      expression: SUM(net_revenue)
      type: decimal
    dimension:
      timeGrain: day

This MDL creates a governed layer where 'revenue' always means the same calculation, 'customers' abstracts the underlying users table structure, and relationships are explicitly defined. When an AI agent asks "What's our revenue by customer?", WrenAI translates this against the MDL—not the raw schema—ensuring consistent semantics.

The multi-compilation strategy is architecturally clever. The same Rust codebase compiles to three targets: native binaries for server-side execution, Python extensions via PyO3 for SDK integration, and WebAssembly for in-browser analytics. This means you can run WrenAI as a standalone service, embed it in Python data pipelines, or ship semantic query capabilities directly to frontend applications without maintaining separate codebases.

For AI agent integration, WrenAI provides framework-specific "skills" that expose MDL-aware capabilities. The LangChain integration (currently the only shipped SDK) looks like this:

from langchain_wrenai import WrenAI
from langchain.agents import create_sql_agent

# Initialize with your MDL and data source
wren = WrenAI(
    mdl_path="./my_business_model.yaml",
    datasource="postgresql://localhost/analytics"
)

# Create an agent that uses WrenAI for semantic context
agent = create_sql_agent(
    llm=ChatOpenAI(model="gpt-4"),
    toolkit=wren.get_toolkit(),
    verbose=True
)

# The agent now queries against business entities, not raw tables
result = agent.run(
    "Show me revenue trends by customer segment for Q4"
)

Under the hood, WrenAI intercepts the LLM's query planning, provides MDL-based context about available business entities and metrics, then validates and executes the generated SQL against the governed model. The LLM never sees your raw database schema—it only interacts with the business-level abstractions you've defined.

The Apache DataFusion foundation gives WrenAI serious query execution chops. DataFusion's columnar processing and query optimization mean WrenAI can handle complex analytical queries across federated sources without becoming a bottleneck. The governance layer sits atop this performance foundation, adding semantic validation without sacrificing speed.

Gotcha

WrenAI's biggest limitation is the upfront modeling investment required. You can't just point it at a database and start querying—you need to define your business semantics in MDL first. For a non-trivial data model with dozens of entities, hundreds of metrics, and complex relationships, this initial modeling effort can take weeks. Teams expecting plug-and-play text-to-SQL will hit this barrier hard. The value proposition only materializes after you've invested in comprehensive MDL definitions, creating a chicken-and-egg problem for evaluation.

The SDK ecosystem is currently incomplete in ways that limit production adoption. Only the LangChain integration is shipped; CrewAI, Pydantic-AI, LlamaIndex, and other popular agent frameworks are marked "coming soon." If your AI stack isn't built on LangChain, you're either waiting for integrations or building custom bindings yourself. Additionally, the project underwent a major architecture consolidation in May 2025—merging the separate Wren Engine repo and archiving the legacy GenBI application. While this simplifies the codebase long-term, it suggests ecosystem fragmentation and potential migration pain for early adopters. The rapid architectural changes indicate a project still finding its stable form, which raises concerns about production-readiness for risk-averse teams.

Verdict

Use if: You're building AI agents or analytics tools that need governed, semantically consistent data access across multiple sources in an enterprise context where metric definitions and compliance matter. WrenAI excels when you have complex business logic that shouldn't be reinterpreted differently by every LLM interaction, when you need to abstract legacy schema complexity from AI systems, or when data governance requirements prohibit direct schema exposure to language models. It's particularly valuable for teams already investing in semantic modeling who want to extend those definitions to AI agent workflows. Skip if: You have simple, single-database use cases where direct text-to-SQL works acceptably, if you need production-ready integrations beyond LangChain today, if you can't dedicate engineering time to upfront MDL modeling, or if your AI stack requires framework support that's still in the "coming soon" category. Also skip if you need a stable, mature project without recent major architectural changes—WrenAI's consolidation suggests it's still evolving rapidly.

// ADD TO YOUR README
[![Featured on Starlog](https://starlog.is/api/badge/data-knowledge/canner-wrenai.svg)](https://starlog.is/api/badge-click/data-knowledge/canner-wrenai)