Skip to main content

Parse free-form human prompts into a canonical Prompt AST (structured JSON/YAML).

Project description

📦 prompt-ast

Parse human prompts into a canonical, structured Prompt AST.

prompt-ast is a lightweight Python library that converts free-form human prompts into a Prompt Abstract Syntax Tree (AST) — a clean, machine-friendly representation that captures intent, constraints, and expected output.

It treats prompts as first-class artifacts, not just strings.


✨ Why prompt-ast?

Modern LLM workflows rely heavily on prompts — but prompts today are:

  • unstructured text
  • hard to analyze or validate
  • difficult to reuse or compare
  • tightly coupled to specific tools or models

prompt-ast introduces a foundational layer that sits before agents, chains, or LLM calls:

Human Prompt
     ↓
Prompt AST (this library)
     ↓
LLM / Agent / Tooling

By normalizing prompts into a canonical structure, you unlock better tooling, governance, and reuse — without forcing opinions on how prompts are stored or executed.


🧠 What is a Prompt AST?

Inspired by compiler ASTs, a Prompt AST is an abstract, structured representation of a prompt.

Example:

Input

Act as a CTO. Be concise. Explain risks of migrating MySQL to RDS.

Output (JSON)

{
  "version": "0.1",
  "raw": "Act as a CTO. Be concise. Explain risks of migrating MySQL to RDS.",
  "role": "CTO",
  "context": null,
  "task": "Explain risks of migrating MySQL to RDS",
  "constraints": [
    "Be concise"
  ],
  "assumptions": [],
  "ambiguities": [],
  "output_spec": {
    "format": null,
    "structure": [],
    "language": null
  },
  "metadata": {
    "extracted_by": "heuristic",
    "confidence": 0.55
  }
}

🚀 Features

  • ✅ Parse free-form prompts into a canonical AST
  • ✅ Heuristic (offline) parsing — no API key required
  • ✅ Optional LLM-assisted parsing (pluggable)
  • ✅ JSON and YAML export
  • ✅ CLI and Python API
  • ✅ Minimal, extensible schema
  • ✅ Stateless by design (you own storage, versioning, governance)

📦 Installation

pip install prompt-ast

Optional extras:

pip install prompt-ast[yaml]     # YAML export
pip install prompt-ast[openai]   # OpenAI-compatible LLM parsing
pip install prompt-ast[all]

🧪 Quick Start (Python)

from prompt_ast import parse_prompt

ast = parse_prompt(
    "Act as a senior backend architect. Be concise. Explain system design trade-offs.",
    mode="heuristic"
)

print(ast.to_json())

🖥 CLI Usage

prompt-ast normalize \
  "Act as a CTO. Be concise." \
  --mode heuristic \
  --format json

LLM-assisted mode (optional):

export OPENAI_API_KEY=...
prompt-ast normalize \
  "Design a scalable data pipeline." \
  --mode hybrid \
  --use-openai

🧩 Design Principles

prompt-ast is intentionally lean:

  • ❌ No storage
  • ❌ No prompt registry
  • ❌ No agent framework
  • ❌ No vendor lock-in

Instead, it provides a clean abstraction that other systems can build on.

This makes it ideal for:

  • internal tooling
  • prompt governance pipelines
  • evaluation frameworks
  • AI platform teams
  • research & experimentation

🛣 Roadmap / Future Scope

The current version focuses on core normalization. Planned and possible future directions include:

Near-term

  • Improved heuristic extraction accuracy
  • Better ambiguity detection
  • Schema refinements based on real-world usage
  • More LLM adapters (Anthropic, local models)

Mid-term (opt-in tooling)

  • Prompt linting (missing task, conflicting constraints)
  • Prompt diffs (semantic-ish comparison)
  • Prompt fingerprints / hashes
  • Validation rules

Long-term (out of scope for core)

  • Prompt registries
  • Governance workflows
  • Evaluation frameworks
  • UI tooling

These are intentionally not bundled into the core library.


🤝 Contributing

Contributions are very welcome — especially in these areas:

  • Improving heuristic parsing
  • Adding test fixtures (real-world prompts)
  • Refining the schema
  • Documentation improvements
  • CLI UX enhancements

Getting started

git clone https://github.com/<your-username>/prompt-ast.git
cd prompt-ast
poetry install
poetry run pytest

Please:

  • keep changes small and focused
  • add tests for behavior changes
  • avoid introducing heavy dependencies

Open an issue before large changes — discussion is encouraged.

If you’re new to the project, check issues labeled good first issue — they are small, well-scoped tasks ideal for first-time contributors.


📜 License

MIT License

This project is released under the MIT License, which means:

  • ✅ Free for personal and commercial use
  • ✅ Permissive and business-friendly
  • ✅ Attribution required
  • ❌ No warranty provided

Perfect for individual contributors and early open-source traction.

(See LICENSE file for full text.)


🌱 Why this project exists

Prompts are becoming one of the most important interfaces in modern software — yet they lack the basic tooling we take for granted in other domains.

prompt-ast is an attempt to provide a small, composable foundation that the ecosystem can build upon.

If that resonates with you, you’re in the right place.


⭐ Support the project

If you find this useful:

  • ⭐ Star the repo
  • 🐛 Report issues
  • 💡 Share ideas
  • 🧑‍💻 Contribute code

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

prompt_ast-0.2.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

prompt_ast-0.2.1-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file prompt_ast-0.2.1.tar.gz.

File metadata

  • Download URL: prompt_ast-0.2.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.9 Darwin/24.6.0

File hashes

Hashes for prompt_ast-0.2.1.tar.gz
Algorithm Hash digest
SHA256 319698f09674ff528b88920db0311f5712a8c9d600f59d2aeea4264b435c6ec9
MD5 eda6f82430fcc3394420498472f2a628
BLAKE2b-256 b798518dd2e7e508dd009cd6411eab8d74ffe14b5c8028d7c01e660cde85bfdd

See more details on using hashes here.

File details

Details for the file prompt_ast-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: prompt_ast-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.9 Darwin/24.6.0

File hashes

Hashes for prompt_ast-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe7c3d0098e653bd226be7c68d7f0bad16b5de8a5ae0c557997c5b3ea8c13706
MD5 408b90e83495429961581d21394dba07
BLAKE2b-256 376de7dab0cbfafd4604a10ded07a710279f3a59ff1977fabaa3523a917d0371

See more details on using hashes here.

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