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.0.tar.gz (14.6 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.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prompt_ast-0.2.0.tar.gz
  • Upload date:
  • Size: 14.6 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.0.tar.gz
Algorithm Hash digest
SHA256 dce904d6df98f2020e71d48c5ee159eb3029226952f7ce7ebac4b996175dc1e9
MD5 9008cf96e0079b25f60767f4ed612f3f
BLAKE2b-256 b6fb293e8a5a43b0cd3f74c17bfe2b77bb1e7f6cc857025614794b07093cfa34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prompt_ast-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc9896e3ca97993dd75bb2e8177c2798155d38f5595b5fd2a0cd59fdce047318
MD5 6c6df979c6580f30076d091cdf23f018
BLAKE2b-256 f7e3983f15899cd03953f027c633fe86a703e45849cf2c69f4b0049fe16a5f05

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