Skip to main content

A modular text-to-SQL toolkit.

Project description

🐷 piglets

A modular library of text-to-SQL tools.

Status

piglets is currently an alpha-stage package. The API is expected to evolve before 1.0.

Get started

Install

venv

pip install piglets

uv

uv add piglets

Install the optional dependency for the model provider you use. For OpenAI:

venv

pip install "piglets[openai]"

uv

uv add "piglets[openai]"

Other provider extras include anthropic, google_genai, google_vertexai, bedrock, cohere, mistralai, groq, ollama, and openrouter.

Install the optional dependency for the database backend you use. For BigQuery:

venv

pip install "piglets[bigquery]"

uv

uv add "piglets[bigquery]"

Logical planning

Use gpt-5.2 to generate 3 logical plans from a natural language query.

from piglets import LogicalPlanner

# initialise a logical planner
logical_planner = LogicalPlanner('gpt-5.2')

# generate 3 logical plan samples and aggregate them
logical_plan = logical_planner.plan(
    natural_language_query="What was the average number of piglets per week for Q4 2025?",
    num_samples=3,
)

# print the aggregated logical plan
for i, step in enumerate(logical_plan.logical_steps):
    print(f"Step {i + 1}: ")
    print(step)

# inspect the candidate plans used to create the aggregate
print(f"Aggregated from {len(logical_plan.sample_plans)} sample plans.")
>>> Step 1:
>>> 1. Identify all piglet birth (or piglet addition) events with their event dates and piglet counts.
>>> Step 2:
>>> 2. Filter the events to the Q4 2025 date range (Oct 1, 2025 through Dec 31, 2025).
>>> Step 3:
>>> 3. Assign each event to a calendar week within that quarter using a consistent week definition (e.g., week starting Monday or Sunday).
>>> Aggregated from 3 sample plans.
...

Database connector

Use DatabaseConnector to inspect a supported database and return a typed schema.

from piglets import DatabaseConnector

database_connector = DatabaseConnector(
    database_type="bigquery",
    bq_dataset="my_bigquery_dataset",
)

database = database_connector.get_database_schema()

print(database.name)
for table in database.tables:
    print(table.name)
    for column in table.columns:
        print(f"- {column.name} ({column.data_type})")

BigQuery connections use the GOOGLE_CLOUD_PROJECT_ID environment variable by default. You can also pass gcp_project_id directly:

database_connector = DatabaseConnector(
    database_type="bigquery",
    database_name="my_bigquery_dataset",
    gcp_project_id="my-gcp-project",
)

Supported Databases

Database type database_type value Install requirement Notes
SQLite sqlite Included by default Uses SQLAlchemy's built-in SQLite dialect.
MySQL mysql SQLAlchemy dialect included by default Requires a compatible MySQL DBAPI driver.
PostgreSQL postgresql SQLAlchemy dialect included by default Requires a compatible PostgreSQL DBAPI driver.
Oracle oracle SQLAlchemy dialect included by default Requires a compatible Oracle DBAPI driver.
Microsoft SQL Server mssql SQLAlchemy dialect included by default Requires a compatible SQL Server DBAPI driver.
BigQuery bigquery piglets[bigquery] Uses GOOGLE_CLOUD_PROJECT_ID or gcp_project_id for the GCP project.
Snowflake snowflake piglets[snowflake] Uses SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER and SNOWFLAKE_PASSWORD environment variables.

Dual-pathway pruning

Use Pruner to reduce a database schema with both preservation and deletion signals. The preservation pathway selects tables and columns that look useful for the query. The deletion pathway removes tables and columns that look irrelevant. dual_pathway_pruning() combines both paths into a final Database schema.

from piglets import DatabaseConnector, LogicalPlanner, Pruner

question = "Which tags saw the largest increase in average answer score from 2022 to 2023, considering only questions with at least 5 answers?"

logical_planner = LogicalPlanner("gpt-5.2")
logical_plan = logical_planner.plan(
    natural_language_query=question,
    num_samples=3,
)

database_connector = DatabaseConnector(
    database_type="bigquery",
    bq_dataset="stack_overflow",
)
database = database_connector.get_database_schema()

pruner = Pruner(model_name="gpt-5.2")
pruned_database = pruner.dual_pathway_pruning(
    natural_language_query=question,
    database=database,
    logical_plan=logical_plan,
)

print(pruned_database.export_as_string())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

piglets-0.1.12.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

piglets-0.1.12-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file piglets-0.1.12.tar.gz.

File metadata

  • Download URL: piglets-0.1.12.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for piglets-0.1.12.tar.gz
Algorithm Hash digest
SHA256 ab725d3ea53142923f0362529ad39dbed3db6ae5d8af69792edabc95c357905d
MD5 2bd708c718043b071e385aa5f05a23f6
BLAKE2b-256 913c5ea62a93e1b42d1f70f4b82af6932702c70457c5d14b48aa186d4951d495

See more details on using hashes here.

Provenance

The following attestation bundles were made for piglets-0.1.12.tar.gz:

Publisher: publish.yml on mportdata/piglets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file piglets-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: piglets-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for piglets-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 63b26f727cae2a5093a8df5e1f7c0d69c58b130e160f118d4b4be5638e74cce3
MD5 7b3436ce98ec0b450c3fa6ac2242b61a
BLAKE2b-256 859f93dce64dc7809073696cf1de8b49336483922515a659ce2ea6007e99d878

See more details on using hashes here.

Provenance

The following attestation bundles were made for piglets-0.1.12-py3-none-any.whl:

Publisher: publish.yml on mportdata/piglets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page