Skip to main content

Type-safe Jinja prompt trees with runtime validation and generated Python stubs.

Project description

promptree

promptree turns a directory of Jinja templates into a type-safe Python prompt tree, with runtime validation and generated .pyi stubs for IDE autocomplete.

It gives you:

  • Dot-notation access to prompt folders and files
  • Runtime validation through Jinja StrictUndefined
  • Generated .pyi stubs for IDE autocomplete
  • Full Jinja support, including include, extends, and macros
  • Raw strings as the only integration format, so there is no adapter layer

Install

pip install promptree

Optional extras:

pip install promptree[watch]
pip install promptree[pydantic-ai]

Quick Start

Directory layout:

prompts/
├── system.md
└── user/
    ├── greeting.md
    └── farewell.txt

Example templates:

{# prompts/system.md #}
System prompt for {{ name }}.
{# prompts/user/greeting.md #}
Hello {{ name }}!
{# prompts/user/farewell.txt #}
Goodbye {{ name }}.

Use them from Python:

from promptree import Promptree

prompts = Promptree("./prompts")

print(prompts.system(name="Claude"))
print(prompts.user.greeting(name="Tim"))
print(prompts.user.farewell(name="Tim"))

The CLI can generate an importable package and matching type stubs inside the prompt directory:

promptree generate ./prompts
promptree check ./prompts

You can also run the CLI as a module:

python -m promptree generate ./prompts

Pre-commit And CI

promptree check regenerates the stubs in memory and exits non-zero if the files on disk are stale. That makes it useful for both local pre-commit enforcement and CI.

If you keep a hand-written __init__.py in a prompt directory, promptree generate will leave it alone and promptree check will ignore that file.

The repository includes a hook definition in .pre-commit-hooks.yaml, and a consumer can wire it up like this:

- repo: https://github.com/your-org/promptree
  rev: v0.1.0
  hooks:
    - id: promptree-check
      files: ^prompts/

For CI, run the same command directly:

promptree check ./prompts

Why No Adapter Layer

Rendered prompt text is the common format across libraries like pydantic_ai, LangChain, and the OpenAI Python client. promptree focuses on generating and validating that text well, rather than wrapping every downstream API.

pydantic_ai

Full runnable example:

from dataclasses import dataclass

from pydantic_ai import Agent, RunContext
from promptree import Promptree

prompts = Promptree("./prompts")


@dataclass
class MyDeps:
    user_name: str
    language: str


agent = Agent("openai:gpt-4o", deps_type=MyDeps)

# Static: render once at agent creation
agent_static = Agent(
    "openai:gpt-4o",
    instructions=prompts.system(name="Claude"),
)


# Dynamic: re-evaluated every run with access to ctx.deps
@agent.instructions
def dynamic_instructions(ctx: RunContext[MyDeps]) -> str:
    return prompts.system(
        name=ctx.deps.user_name,
        language=ctx.deps.language,
    )


result = agent.run_sync(
    prompts.user.greeting(name="Tim"),
    deps=MyDeps(user_name="Tim", language="en"),
)
print(result.output)

LangChain

from langchain_core.messages import HumanMessage, SystemMessage

from promptree import Promptree

prompts = Promptree("./prompts")

messages = [
    SystemMessage(content=prompts.system(name="Claude")),
    HumanMessage(content=prompts.user.greeting(name="Tim")),
]

Raw OpenAI Client

from openai import OpenAI

from promptree import Promptree

client = OpenAI()
prompts = Promptree("./prompts")

client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": prompts.system(name="Claude")},
        {"role": "user", "content": prompts.user.greeting(name="Tim")},
    ],
)

Generated Package

When you run promptree generate ./prompts, promptree writes two files into the prompt directory:

  • __init__.py with a tree object backed by Promptree
  • __init__.pyi with nested classes and typed call signatures for autocomplete

That means you can import the generated package and get both runtime access and IDE support from the same directory.

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

promptree-0.1.1.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

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

promptree-0.1.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file promptree-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for promptree-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bcb065bf2c4903008c2d1f87d601824dff58ec7c42429f5f90615ee860f5b6a6
MD5 f35952718ea99abf4bb31292949bb941
BLAKE2b-256 952219c5506cd6039421f65cc44470467b2d16b30262e26750c8e814abbbf8e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptree-0.1.1.tar.gz:

Publisher: python-package.yml on TKaluza/promptree

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

File details

Details for the file promptree-0.1.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for promptree-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df5287cbe1b84ae0c0eb1ba636719e6ccd84bbc231780fa547f1924ee6ad5209
MD5 d259a3e08a24ea1c51074691189042a6
BLAKE2b-256 1fb8dd0e60e6178aecbd2ff0665515f32932016ea5df146da55c518e56d5f48a

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptree-0.1.1-py3-none-any.whl:

Publisher: python-package.yml on TKaluza/promptree

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