Skip to main content

Dynamic prompt management and configuration library for LLM applications. Powerful, lazy-loading, and supports Jinja2 templates and Pydantic schemas.

Project description

dynaprompt logo

dynaprompt - Dynamic prompt management and configuration library for LLM applications. Powerful, lazy-loading, and supports Jinja2 templates and Pydantic schemas.

MIT License PyPI Code Style Black Coverage

DynaPrompt is a powerful, lazy-loading prompt configuration manager inspired by Dynaconf. It offers a structured way to manage, version, and render LLM prompts while keeping your templates separate from your application logic.


FeaturesWhy DynaPrompt?UsageInspection


🚀 30-Second Quickstart

# Using pip
pip install dynaprompt

# Using uv (recommended)
uv add dynaprompt
  1. Create a prompts.toml (or a directory of .md files):
[default.greeting]
template = "Hello {{ name }}! You are a helpful assistant."
model = "gpt-3.5-turbo"

[production.greeting]
template = "Hello {{ name }}! You are a professional consultant."
model = "gpt-4o"
  1. Use it in Python:
from dynaprompt import DynaPrompt

# 1. Initialize (zero I/O happens here)
prompts = DynaPrompt(settings_files=["prompts.toml"])

# 2. Render default environment
rendered = prompts.greeting.render(name="Emam")
print(rendered.text)
# -> "Hello Emam! You are a helpful assistant."

# 3. Switch environments seamlessly
with prompts.using_env("production"):
    prod_rendered = prompts.greeting.render(name="Emam")
    print(prod_rendered.text)
    # -> "Hello Emam! You are a professional consultant."

💡 Why DynaPrompt?

1. Lazy Loading is the Core

Most libraries load prompts at import time. This makes environment swapping hard and slows down startup.

The "Old" Way (Hardcoded/Manual):

# Loaded once, forever. Hard to swap for tests/production.
SYSTEM_PROMPT = open("prompts/system.txt").read()

The DynaPrompt Way:

from dynaprompt import prompts
# File is only read NOW. Respects ENV_FOR_DYNAPROMPT=production automatically.
print(prompts.system.render())

2. Why not just use Dynaconf?

Since Dynaconf handles strings, why a new library? DynaPrompt adds prompt-specific logic:

  • Jinja2 First-Class: Automatic variable injection, recursive flattening, and secret resolution.
  • Schema Auto-loading: Automatically registers Pydantic models from .py files as response schemas.
  • Prompt Inheritance: Use extends to share model config (temperature, max_tokens) between templates.
  • Render State: Remembers previous variables for precise partial updates via .rerender().

3. Comparison with others

Feature DynaPrompt Prompt-Poet / Promptix f-strings
Boilerplate Zero (just a folder) Medium (manual registration) High
Lazy Loading ✅ Yes ❌ No ❌ No
Env Layers ✅ Native ⚠️ Manual ❌ No
Inheritance ✅ Native ❌ No ❌ No
Schemas ✅ Auto-discovery ⚠️ Manual ❌ No

✨ Features

  • 📂 File-based Management: Write templates in clean Markdown (.md) with YAML frontmatter or group multiple prompts in a prompts.toml.
  • 🏗️ Recursive Auto-Discovery: Pass a directory like settings_files=["prompts/"] and DynaPrompt builds a nested namespace reflecting your folder structure.
  • ⚡ Lazy Loading: Zero I/O at import. Files are only read when a prompt is actually accessed.
  • 🌍 Environment Layering: Native support for development, production, etc. Override metadata per environment without touching the template.
  • 🔧 Schema Auto-discovery: Automatically registers Pydantic models, TypedDicts, and JSON schemas from your settings directories.
  • 🧩 Jinja2 First-Class: Supports recursive variable flattening, auto-rendering, and complex logic inside templates.
  • 📤 Auto-Export: Mirror your entire prompt structure to a central TOML file for easy external overrides.
  • 🛡️ Validation & Hooks: Enforce constraints on rendered output and intercept rendering with a powerful hook system.

🛠 Usage

Loading from a Directory & Namespaces

DynaPrompt excels at organizing templates as files. When you load files from a nested directory structure (e.g., examples/google/gemini.md), it automatically builds a nested namespace.

from dynaprompt import DynaPrompt

prompts = DynaPrompt(
    settings_files=["examples/"], # Scans for .md, .toml, .py schemas recursively
    environments=True
)

# Accessing a nested prompt using intuitive dot notation:
rendered = prompts.google.gemini.render(user_name="Emam", text="DynaPrompt is great!")

print(rendered.text)
print(rendered.config["model"]) # "gemini-1.5-pro" (from frontmatter or .toml)

# Partial update: keeps "user_name" but changes "text"
updated = prompts.google.gemini.rerender(text="It's really fast.")

Auto-Exporting Prompts to TOML

You can automatically export your entire loaded prompt structure into a central pyprompts.toml file. This acts as an interface for users to easily view or override prompt templates and settings.

# Pass auto_export=True, or auto_export="custom_path.toml"
prompts = DynaPrompt(settings_files=["examples/"], auto_export=True)

# Access a prompt to trigger the lazy load and export the file
_ = prompts.google.gemini

File-Based Templates and Variables

Instead of writing long strings in your configuration files, you can reference external files directly in your TOML config. DynaPrompt will automatically resolve and load their contents!

[default.my_prompt]
# Read the prompt text directly from a file
template = "path/to/external_template.md"

# Load default variables from a JSON or YAML file
variables = "path/to/default_vars.json"

You can also define prompt-specific variables directly inline:

[default.my_prompt]
template = "Hello {{ username }}! Your tier is {{ tier }}."
[default.my_prompt.variables]
tier = "Premium"

Schema Integration

DynaPrompt automatically registers Pydantic models found in your settings_files.

# If examples/schemas.py defines class UserProfile(BaseModel):
prompts = DynaPrompt(settings_files=["examples/"])

# Model is available as an attribute
user_schema = prompts.UserProfile

# Use it in rendering (automatically injects JSON schema if referenced in template)
rendered = prompts.fetch_user.render(username="em2m")

🔍 Inspection & Tab-Completion

DynaPrompt is designed for developer productivity.

  • Tab-Completion: Use dir(prompts) or hit Tab in your IDE to see all available prompts and schemas.
  • History Tracking: Inspect exactly where a prompt was loaded from and how it was merged across layers.
# See loading history for a prompt
print(prompts.inspect("customer_support"))

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

dynaprompt-0.2.0.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

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

dynaprompt-0.2.0-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dynaprompt-0.2.0.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dynaprompt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 43b3e953d75bc964eb90e0b865e7aca48bf659da6a2bde553647f1b0c9c051d9
MD5 55510ad34518adf62eb229267791f721
BLAKE2b-256 101f1b2d87ceaaa5e124a5bb9d16ba30a7ea30a8ada1f4a182331944e04e21a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dynaprompt-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dynaprompt-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb6111f01d6593c6f4bbdd019243a2ab2ce7287783ccd60e265eaaafd993a49d
MD5 9dddb63e715c8d780d4eabfad0c9223a
BLAKE2b-256 c12868e8694208e93636fc3a1ba8c63394ccde7849656afc90c4cfb347337915

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