Chat with your business data, without letting AI write SQL
Business users waited for reports to get one number. Bluemesh's lead architect designed a chat assistant that answers in plain words, where the model only extracts parameters and every query runs with the user's own access.
At a glance
- Service
- AI Solutions Architecture
- Stack
- Python, Microsoft Entra ID, SQL
- When
- Between 2022 and 2026
- Who
- Bluemesh's lead architect, in an earlier role. Client not named.
The story
The situation
Business users clicked through business intelligence (BI) reports, or waited for the BI team, to get one number. They wanted answers in plain words, in seconds, by chat or voice.
What was really holding it back
Trust. Letting a model write SQL makes the fastest demo and the riskiest design: it allows SQL injection (a question crafted to run a harmful query), and it can show people rows they should never see. The answer had to be right, and access had to stay exactly as it was.
What was decided, and why
- The model never writes SQL. It only extracts parameters, such as a location, an asset, a date range and a measure.
- A typed contract. A Pydantic schema in Python defines which fields are required. If a required field is missing, the assistant asks the user for it. If an optional field is missing, the assistant uses a stated default and says which default it used.
- A controlled query. An API runs a parameterized query against a fixed view, and the result comes back as a table in the chat.
- The database decides access. The user signs in with Microsoft Entra ID and receives a sign-in token. The API exchanges that token for a database token with the on-behalf-of flow (MSAL), so the query runs as that user. Row-level security hides rows, and column permissions or per-role views protect sensitive columns.
- A narrow first slice. One stable data mart, one view and a fixed set of parameters. The scope widens only after the golden set passes.
The trade-off
Parameters instead of free SQL means the assistant cannot answer everything. That is the point: a polite "that's out of scope" is better than a confident wrong number.
How it was tested
A golden set of business questions, each with its expected parameters and results. Ordinary code checked each answer field by field, so no AI judge was needed. The hard cases were an unclear question, where the assistant had to ask, and an out-of-scope question, where it had to decline. The architect owned the end-to-end design and defined the release gates: code tests, parameter evaluation, an access test for each role, then human approval. The client's DevOps team built the pipeline and deployed it.
The result
Answers in plain words, without waiting for the BI team. Users said answers came much faster. Time saved was not measured, so no number is quoted.
What Bluemesh would do differently today
Agree on the baseline first: the time to get an answer today, then the time with the assistant.
How the parts fit
The parts used in this project, in the order a request moves through them.
- Userasks in plain words
- Entra IDsigns the user in
- Modelextracts parameters only
- Pydantic schemaask, default or decline
- Controlled APIparameterized query
- On-behalf-of tokenthe query runs as the user
- SQL viewrow-level security
- Chatthe answer as a table
Technology used in this project
- Python
- Pydantic
- LLM parameter extraction
- Microsoft Entra ID
- MSAL on-behalf-of flow
- SQL row-level security
- Column permissions or per-role views
- Parameterized queries on a fixed view
- Golden evaluation set
- Access tests per role
How Bluemesh builds it today
A reference architecture for a new project, based on current Azure and AWS guidance. It describes what Bluemesh would build now, not what was built in the project above.
Identity
- Entra ID sign-in and the on-behalf-of flow to the data source. No shared service account.
Data
- Azure SQL or a Microsoft Fabric warehouse with row-level and column-level security, and one view per use case.
Model
- Structured outputs, validated against the schema in code.
- Models reached through an AI gateway with model token limits and metrics.
Hosting and audit
- Azure Container Apps or App Service, private endpoints and Key Vault.
- Every question, the extracted parameters and the query that ran are logged, without personal information.
Testing
- The golden set and the per-role access tests as a release gate. New failures become test cases.
Start small
- One data mart, one view, a handful of parameters. Voice later, with Azure AI Speech in a Canadian region.
Questions
Is text-to-SQL safe on production databases?
It carries real risk: a model that writes SQL can be pushed into queries nobody intended. Extracting parameters and running a fixed, parameterized query removes SQL injection through the model. A manipulated question can still only return data that user is already allowed to see.
How do I give an AI assistant access to a database without exposing other users' data?
Run every query as the signed-in user, through the on-behalf-of flow, and let row-level security in the database decide what comes back. The model never decides who sees what.
How do you test a chat-with-your-data assistant?
With a golden set of real questions and their expected parameters, checked by ordinary code, plus an access test for every role. A wrong parameter or a failed access test stops the release.
Facing the same problem?
Send a few lines about your system. You get a reply with the first questions to answer and a way to start small.
Talk to BluemeshUpdated:
More work
- AI agents for Jira, Confluence and GitHub, inside Microsoft Teams through MCP
- A document assistant designed so each person sees only what they are allowed to read
- A production generative AI platform with six model providers and more than 40% fewer prompt tokens
- From ASP.NET Web Forms to .NET microservices on Azure, with no big-bang rewrite
- Enterprise architecture aimed at sales: a new biotechnology product line in the public supply chain
- Java EE point-of-sale platforms rebuilt as microservices on AWS
- SAP Business One integrations that keep each system independent
- Moving a large group of servers to the cloud, riskiest dependencies first
- An AI assistant that puts the caller's history on the representative's screen