A robust zero-dependency library for creating prompt templates
Project description
Prompt Template
A lightweight, zero-dependency Python library for managing LLM prompt templates. Built on the design principles of
string.Template but with enhanced features specifically designed for LLM prompt engineering.
Features
- Strong template validation with detailed error messages
- Support for nested braces and complex JSON structures
- Automatic value serialization for common Python types
- Default value support with deep copying for mutable types
- Incremental template population with value inheritance
- Type hints for enhanced IDE support
- Zero dependencies - just pure Python
- 100% test coverage
Installation
pip install prompt-template
Usage
The library is intentionally very simple to use. The idea is to keep it simple, have validation and serialization builtin, and make debugging simple.
Simple Templates
from prompt_template import PromptTemplate
# Create a template
template = PromptTemplate("Hello ${name}! Welcome to ${location}.")
# Render with values
result = template.to_string(name="Alice", location="Wonderland")
print(result) # Hello Alice! Welcome to Wonderland.
Default Values
# Set default values that can be overridden later
template = PromptTemplate("Hello ${name}! Your settings are: ${settings}")
# Set default values - they're safely deep copied
template.set_default(
name="Guest",
settings={"theme": "light", "language": "en"}
)
# Use with defaults
print(template.to_string())
# Hello Guest! Your settings are: {"theme": "light", "language": "en"}
# Override specific values
print(template.to_string(name="Alice"))
# Hello Alice! Your settings are: {"theme": "light", "language": "en"}
# Override everything
print(template.to_string(
name="Bob",
settings={"theme": "dark", "language": "fr"}
))
# Hello Bob! Your settings are: {"theme": "dark", "language": "fr"}
Named Templates
Adding a name to your template enhances error messages with context:
template = PromptTemplate(
name="user_greeting",
template="Hello ${name}! Welcome to ${location}."
)
Complex JSON Templates
The library handles nested structures elegantly:
template = PromptTemplate("""
{
"user": {
"name": "${username}",
"role": "${role}"
},
"settings": {
"theme": "${theme}",
"notifications": ${notifications},
"preferences": ${preferences}
}
}
""")
# Values are automatically serialized
result = template.to_string(
username="john_doe",
role="admin",
theme="dark",
notifications={"email": True, "push": False},
preferences=["daily_digest", "weekly_report"]
)
Incremental Template Population
You can build templates incrementally, preserving defaults along the way:
# Start with a base template
base = PromptTemplate("""
Query parameters:
Model: ${model}
Temperature: ${temperature}
User: ${user}
Prompt: ${prompt}
""")
# Set some defaults
base.set_default(
model="gpt-4",
temperature=0.7
)
# Create a partially populated template
user_template = base.substitute(user="alice")
# Complete the template later
final = user_template.to_string(prompt="Tell me a story")
Type Handling and Automatic Serialization
The library automatically handles various Python types:
from uuid import UUID
from decimal import Decimal
from datetime import datetime
template = PromptTemplate("""
{
"id": "${id}",
"amount": "${amount}",
"binary": "${binary_data}",
"metadata": ${metadata}
}
""")
result = template.to_string(
id=UUID("550e8400-e29b-41d4-a716-446655440000"),
amount=Decimal("45.67"),
binary_data=b"Hello World", # Automatically base64 encoded if needed
metadata={
"timestamp": datetime.now(), # Serialized via JSON
"values": [1, 2, 3]
}
)
Custom Serialization
Extend the base class to customize value serialization:
from typing import Any
from datetime import datetime
from prompt_template import PromptTemplate as BasePromptTemplate
import orjson
class PromptTemplate(BasePromptTemplate):
@staticmethod
def serializer(value: Any) -> str:
return orjson.dumps(value) # use orjson for faster json serialization etc.
Error Handling
The library provides specific extensive errors:
Missing Values
from prompt_template import MissingTemplateValuesError
template = PromptTemplate("Hello ${name}!")
try:
template.to_string() # No values provided
except MissingTemplateValuesError as e:
print(f"Missing values: {e.missing_values}") # {'name'}
Invalid Keys
from prompt_template import InvalidTemplateKeysError
template = PromptTemplate("Hello ${name}!")
try:
template.to_string(name="World", invalid_key="value")
except InvalidTemplateKeysError as e:
print(f"Invalid keys: {e.invalid_keys}") # ['invalid_key']
print(f"Valid keys: {e.valid_keys}") # {'name'}
Serialization Errors
from prompt_template import TemplateSerializationError
template = PromptTemplate("Value: ${value}")
try:
template.to_string(value=object()) # Non-serializable object
except TemplateSerializationError as e:
print(f"Failed to serialize key '{e.key}': {e.original_error}")
License
MIT License
If you find the library useful, please star it on GitHub.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prompt_template-1.1.0.tar.gz.
File metadata
- Download URL: prompt_template-1.1.0.tar.gz
- Upload date:
- Size: 36.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77ac23f1668f1dda0d4f44d7152b5a85a9bf918f4484f19272af67945feb13f6
|
|
| MD5 |
26071ef4579d85993bec34de87280eaa
|
|
| BLAKE2b-256 |
4887cc0a525b8a004dba14ba05cd37b48cb3c62e55916766664f07c26b1a5bb4
|
Provenance
The following attestation bundles were made for prompt_template-1.1.0.tar.gz:
Publisher:
release.yaml on Goldziher/prompt-template
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
prompt_template-1.1.0.tar.gz -
Subject digest:
77ac23f1668f1dda0d4f44d7152b5a85a9bf918f4484f19272af67945feb13f6 - Sigstore transparency entry: 166222749
- Sigstore integration time:
-
Permalink:
Goldziher/prompt-template@6e8ceff870636b24c08042859d80892aaef7909a -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/Goldziher
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@6e8ceff870636b24c08042859d80892aaef7909a -
Trigger Event:
release
-
Statement type:
File details
Details for the file prompt_template-1.1.0-py3-none-any.whl.
File metadata
- Download URL: prompt_template-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1520dc57d2aced224bbf232a806fd2df86066a03b4ab54e30970cb7c2fbbe46c
|
|
| MD5 |
b808e5ef81e0c5dc4277a69646dd1870
|
|
| BLAKE2b-256 |
9eb88b1e083cedaada9e8d3b4dbb4296bff8a676ec9ea4cbb3e426d322a01803
|
Provenance
The following attestation bundles were made for prompt_template-1.1.0-py3-none-any.whl:
Publisher:
release.yaml on Goldziher/prompt-template
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
prompt_template-1.1.0-py3-none-any.whl -
Subject digest:
1520dc57d2aced224bbf232a806fd2df86066a03b4ab54e30970cb7c2fbbe46c - Sigstore transparency entry: 166222750
- Sigstore integration time:
-
Permalink:
Goldziher/prompt-template@6e8ceff870636b24c08042859d80892aaef7909a -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/Goldziher
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@6e8ceff870636b24c08042859d80892aaef7909a -
Trigger Event:
release
-
Statement type: