Skip to main content

QuerySmith

QuerySmith is a Python 3.11+ package that turns Persian and English natural-language questions into Microsoft SQL Server T-SQL SELECT queries. It reads schema metadata, prompts an LLM with that context, validates the generated SQL, and can execute approved queries through SQL Server.

Early release: QuerySmith is being prepared for its first public PyPI release.

Features

  • SQL Server schema introspection for tables, columns, primary keys, and foreign keys.
  • Persian and English question support in the SQL-generation prompt.
  • T-SQL query generation through an OpenAI-compatible client, with AvalAI defaults.
  • Environment-based AvalAI and OpenAI-compatible configuration.
  • A conservative guard that allows one SELECT or WITH ... SELECT query and rejects comments, multiple statements, DDL/DML, procedure access, SELECT INTO, external data access, and linked-server-style object names.
  • Python helpers for configuration, connection creation, query generation, validation, and guarded execution.

Installation

PyPI

After the first PyPI release, install QuerySmith with:

python -m pip install querysmith

From source

git clone https://github.com/SinaQP/QuerySmith.git
cd QuerySmith
python -m venv .venv

Activate the environment:

# Linux/macOS
source .venv/bin/activate

# Windows PowerShell
.venv\Scripts\Activate.ps1

Install QuerySmith (and, for contributors, its configured development dependencies):

python -m pip install -e .
python -m pip install -e ".[dev]"

Supported Python versions

QuerySmith requires Python 3.11 or newer.

SQL Server prerequisites

QuerySmith requires access to a Microsoft SQL Server instance and credentials for the target database. Configure a server or host, optional port, database, username, password, and the installed ODBC driver name. Windows authentication is supported by setting DB_TRUSTED_CONNECTION=true.

ODBC driver required: Installing pyodbc does not necessarily install the Microsoft SQL Server ODBC driver. pyodbc is the Python adapter; Microsoft ODBC Driver 18 for SQL Server is a separate system-level driver that may also be required. Driver installation differs across Windows, Linux, and macOS. Follow Microsoft's official ODBC-driver installation documentation for your platform, and make sure DB_DRIVER exactly matches an installed driver.

To view available driver names:

import pyodbc

print(pyodbc.drivers())

ODBC Driver 18 for SQL Server is a common choice. QuerySmith's code defaults to version 17 when DB_DRIVER is not set, so set this variable explicitly when using version 18.

Environment configuration

Create a local configuration file and keep it out of version control:

cp .env.example .env
Copy-Item .env.example .env

.env can contain database passwords and API keys; never commit it.

SQL Server settings

Variable Required Purpose
DB_SERVER or DB_HOST Yes SQL Server host or server name.
DB_PORT No Port appended to a host as host,port; omit for a named instance.
DB_DATABASE or DB_NAME Yes Target database name.
DB_USERNAME or DB_USER Yes* SQL Server login name.
DB_PASSWORD Yes* SQL Server login password.
DB_DRIVER No Installed driver name, such as ODBC Driver 18 for SQL Server.
DB_TRUSTED_CONNECTION No Set to true, 1, or yes to use Windows trusted authentication.

*These values are currently required by load_config() even when trusted authentication is enabled.

LLM settings

OpenAICompatibleClient selects the first non-empty API key from QUERYSMITH_LLM_API_KEY, AVALAI_API_KEY, and OPENAI_API_KEY. Its base URL comes from QUERYSMITH_LLM_BASE_URL or AVALAI_BASE_URL, otherwise it defaults to https://api.avalai.ir/v1. QUERYSMITH_LLM_MODEL selects the model and defaults to gpt-4o-mini.

There is no LLM_PROVIDER setting: select a provider by supplying its API key and, where needed, its compatible base URL.

AvalAI

AvalAI is used through its OpenAI-compatible API by default. A minimal configuration is:

AVALAI_API_KEY=your-avalai-api-key
AVALAI_BASE_URL=https://api.avalai.ir/v1
QUERYSMITH_LLM_MODEL=gpt-4o-mini

You can instead use the provider-neutral QUERYSMITH_LLM_API_KEY and QUERYSMITH_LLM_BASE_URL names.

OpenAI

For the OpenAI API, configure its key and explicitly set the OpenAI-compatible endpoint:

OPENAI_API_KEY=your-openai-api-key
QUERYSMITH_LLM_BASE_URL=https://api.openai.com/v1
QUERYSMITH_LLM_MODEL=gpt-4o-mini

Do not place actual credentials in source files, documentation, or commits.

Minimal working example

from querysmith import load_config, make_engine
from querysmith.llm import OpenAICompatibleClient
from querysmith.pipeline import generate_query

config = load_config()
engine = make_engine(config)
client = OpenAICompatibleClient()

sql = generate_query(
    question="Show the top 10 customers by total order value.",
    engine=engine,
    client=client,
    schema="dbo",
)
print(sql)

The same flow accepts Persian questions, for example: ده مشتری با بیشترین مبلغ سفارش را نمایش بده.

To execute a generated query, pass it to querysmith.pipeline.execute_select(engine, sql). That function validates the SQL again and returns at most 100 rows by default (up to 1,000 when requested).

Security model

QuerySmith treats generated SQL as untrusted input. Before generation, it supplies the LLM with the introspected schema. Before execution, its application-level guard permits only a single conservative SELECT or WITH ... SELECT query and rejects comments, multiple statements, DDL/DML keywords, procedure access, SELECT INTO, OUTPUT INTO, risky external data access, and linked-server-style names.

These checks reduce risk; they are not a complete security boundary. Use a read-only SQL Server account that can access only the necessary databases, schemas, tables, and views. In sensitive environments, review generated SQL and add authentication, authorization, rate limiting, logging, and query-cost controls before exposing QuerySmith to untrusted users.

Limitations

  • Supports Microsoft SQL Server and T-SQL only.
  • Generates and executes SELECT queries only.
  • Requires access to an external OpenAI-compatible LLM service.
  • SQL quality depends on the schema metadata, question clarity, and selected model.
  • Complex or ambiguous questions can produce incorrect or inefficient queries.
  • Database permissions and query-resource limits must be managed independently.

Development and testing

python -m pip install -e ".[dev]"
python -m pytest

License

QuerySmith is released under the MIT License. See LICENSE for details.

Metadata

Release files for querysmith 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for querysmith 0.1.0
File Size Uploaded Provenance Actions
querysmith-0.1.0.tar.gz 72.9 kB With provenance Details for querysmith-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for querysmith 0.1.0
File Interpreter ABI Platform Actions
querysmith-0.1.0-py3-none-any.whl Python 3 none any Details for querysmith-0.1.0-py3-none-any.whl

Total release size: 86.4 kB

Release files / querysmith-0.1.0.tar.gz

Download URL querysmith-0.1.0.tar.gz
Size 72.9 kB
Tags Source
SHA-256 checksum
How to use checksums
78cdf5e10c5eca576dafcdd419682548643c89b34d65a1902c1e700276746b07
BLAKE2b-256 checksum
How to use checksums
6505e471b51186ae3e94b14f2c167cb68e4ff0b2bae03512ed0d10091142e3e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 5, 2026.

Transparency log

Release files / querysmith-0.1.0-py3-none-any.whl

Download URL querysmith-0.1.0-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f73d1b6136bb93a8ec0a9032d054b147109ae390c23fc2d430f8e1e7fa5810b
BLAKE2b-256 checksum
How to use checksums
c5e2fa244b415ef2a9f4ba964ef0781b2c863b9be3615caecd102583bd7b20f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 5, 2026.

Transparency log

Security for querysmith 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for querysmith-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page