text2ql
Natural-language to query-language toolkit for GraphQL and SQL.
text2ql is built for practical usage: deterministic generation, LLM-assisted generation, schema/mapping normalization, evaluation utilities, and benchmark adapters (Spider/BIRD).
Current first-class targets are GraphQL and SQL; planned future targets include JSONata, jq-style queries, Cypher, and dialect-focused SQL paths (PostgreSQL, MySQL, and more).
Why teams use it
- Fast path from text -> query for GraphQL and SQL.
- Three generation modes:
deterministic,llm,function_calling. - Schema + mapping aware generation with validation and confidence scoring.
- Built-in dataset ingestion, synthetic rewrites, and evaluation hooks.
- Built-in Spider/BIRD benchmark loaders and runner.
Install
Python: >=3.10
pip install text2ql
Optional extras:
# Streamlit playground
pip install "text2ql[app]"
# SQL execution backend support (SQLAlchemy)
pip install "text2ql[sql]"
# Local development tools
pip install -e ".[dev]"
Quick Start (CLI)
- Deterministic GraphQL:
text2ql "show top 5 client records with mail state enabled" \
--target graphql \
--schema '{"entities":["customers"],"fields":{"customers":["id","email","status"]}}' \
--mapping '{"entities":{"client":"customers"},"fields":{"mail":"email"},"filters":{"state":"status"},"filter_values":{"status":{"enabled":"active"}}}'
- Deterministic SQL:
text2ql "show customers highest total first 5 offset 10" \
--target sql \
--schema '{"entities":["customers"],"fields":{"customers":["id","total","status"]}}'
- LLM mode:
export OPENAI_API_KEY=...
text2ql "show latest 5 orders with status active" \
--target sql \
--mode llm \
--llm-model gpt-4o-mini \
--schema '{"entities":["orders"],"fields":{"orders":["id","status","createdAt"]}}'
- Function-calling / structured output mode:
export OPENAI_API_KEY=...
text2ql "show latest 5 orders with status active" \
--target graphql \
--mode function_calling \
--llm-model gpt-4o-mini \
--schema '{"entities":["orders"],"fields":{"orders":["id","status","createdAt"]},"args":{"orders":["status","limit","orderBy","orderDirection"]}}'
Quick Start (Python API)
from text2ql import Text2QL
service = Text2QL()
result = service.generate(
text="list active customers",
target="graphql",
schema={"entities": ["customers"], "fields": {"customers": ["id", "status", "email"]}},
mapping={"filters": {"state": "status"}, "filter_values": {"status": {"active": "active"}}},
)
print(result.query)
print(result.confidence)
LLM provider wiring:
from text2ql import Text2QL
from text2ql.providers.openai_compatible import OpenAICompatibleProvider
provider = OpenAICompatibleProvider(
api_key="...", # or use OPENAI_API_KEY / TEXT2QL_API_KEY
model="gpt-4o-mini",
use_structured_output=True,
)
service = Text2QL(provider=provider)
Streamlit Playground
Use hosted app: https://text2ql.streamlit.app/
For local/private data: pip install -e ".[app]".
Run locally: python -m streamlit run examples/streamlit_app.py.
Modes at a glance
| Mode | What it does | Best for |
|---|---|---|
deterministic |
Rule/schema-driven generation | Fast, predictable production defaults |
llm |
Direct LLM-assisted generation | Broad language coverage |
function_calling |
Schema-constrained structured intent path | Better output shape control |
Production Setup (brief)
Recommended project files:
project/
schema.json
mapping.json
data.json # optional; used for payload execution checks
expected_query.sql # optional
expected_rows.json # optional
Production checklist:
- Keep
schema.jsonandmapping.jsonin source control. - Start with
--mode deterministicfor baseline reliability. - Enable
--mode llmor--mode function_callingafter baseline tests pass. - Use retry settings for provider calls (
--llm-max-retries,--llm-retry-backoff). - Run evaluation/benchmark checks in CI before releases.
- Use
-vduring rollout/debugging for detailed logs.
Useful CLI operations:
# Generate hybrid mapping (auto baseline + optional overrides)
text2ql --generate-hybrid-mapping \
--schema-file ./schema.json \
--data-file ./data.json \
--mapping-output-file ./mapping.generated.json
# Execute generated query against payload JSON and compare expected output
text2ql "how many qqq do i own" \
--target graphql \
--schema-file ./schema.json \
--mapping-file ./mapping.json \
--data-file ./data.json \
--expected-execution-file ./expected_rows.json
Benchmarking (Spider & BIRD)
text2ql ships with benchmark adapters and runner APIs:
load_spider(...)load_bird(...)run_benchmark(...),arun_benchmark(...)format_report(...)
CLI examples:
# Spider
text2ql --benchmark spider --benchmark-path /path/to/spider --benchmark-mode structural
# BIRD
text2ql --benchmark bird --benchmark-path /path/to/bird-minidev --benchmark-mode execution
Latest LLM benchmark snapshot (synthetic harness)
Using run_llm_benchmark.py (50 Spider-style + 50 BIRD-style synthetic examples, mode=llm, gpt-4o-mini):
Raw summary:
| Benchmark | Exact | Structural | Execution | Errors |
|---|---|---|---|---|
| Spider (50) | 62.0% | 64.0% | 84.0% | 0/50 |
| BIRD (50) | 70.0% | 78.0% | 90.0% | 0/50 |
Reproduce:
OPENAI_API_KEY=... ./venv/bin/python run_llm_benchmark.py
# or
OPENAI_API_KEY=... python3 run_llm_benchmark.py
Latest deterministic benchmark snapshot (synthetic harness)
Using run_deterministic_benchmark.py (50 Spider-style + 50 BIRD-style synthetic examples, mode=deterministic):
Raw summary:
| Benchmark | Exact | Structural | Execution | Errors |
|---|---|---|---|---|
| Spider (50) | 0.0% | 40.0% | 100.0% | 0/50 |
| BIRD (50) | 0.0% | 52.0% | 100.0% | 0/50 |
Reproduce:
./venv/bin/python run_deterministic_benchmark.py
# or
python3 run_deterministic_benchmark.py
Evaluation + Synthetic Data Utilities
Public helpers:
ingest_dataset(...)generate_synthetic_examples(...)evaluate_examples(...)aevaluate_examples(...)rewrite_user_utterance(...)
Built-in rewrite plugins include:
generic,portfolio,banking,crm,healthcare,ecommerce
Repo layout (high level)
src/text2ql/
core.py # Text2QL facade
cli.py # CLI entrypoint
engines/ # GraphQL/SQL engines + stage modules
renderers.py # GraphQLIRRenderer / SQLIRRenderer
evaluate.py # exact/structural/backend evaluation
benchmarks/ # Spider/BIRD loaders + runner
mapping.py # hybrid mapping generation
dataset.py # ingestion + synthetic variants
sql_executor.py # SQLAlchemy-backed execution backend
Testing
python3 -m pytest -m unit
python3 -m pytest -m e2e
python3 -m pytest
License
Apache-2.0
Metadata
Release files for text2ql 0.2.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| text2ql-0.2.7.tar.gz | 126.5 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| text2ql-0.2.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 266.6 kB
Release files / text2ql-0.2.7.tar.gz
| Download URL | text2ql-0.2.7.tar.gz |
|---|---|
| Size | 126.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
226774d9ba8c89c15432849a4ff3647805a59aece6b733a8778aafd27ca473ae
|
|
BLAKE2b-256 checksum How to use checksums |
d3a761b786706fa7bf8ab3c50b168128027d5017428e62a9ae80b9c81c5646d2
|
| 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 27, 2026.
Transparency logRelease files / text2ql-0.2.7-py3-none-any.whl
| Download URL | text2ql-0.2.7-py3-none-any.whl |
|---|---|
| Size | 140.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
edf0b5f4938b77268687c477dc1c9bd41e8d34d43414f1533536cf3e2b51592d
|
|
BLAKE2b-256 checksum How to use checksums |
c6c356ffa31152ab58e10fe41166e0d8e90bcb7b230bd4ace8633ebc29564d28
|
| 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 27, 2026.
Transparency logSecurity for text2ql 0.2.7
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.
Attestation for text2ql-0.2.7.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 27, 2026.
Transparency logA 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.