Template-based prompt assembly with ordered slot injectors
Project description
contexting
Template-based prompt assembly. Fill {{slot}} placeholders in a base prompt using ordered injectors and a runtime context.
Installation
pip install contexting
How it works
- Inline injection — if a slot exists in the template, the injector's output replaces it directly
- Appended injection — if the slot is absent, the output is collected and appended after the filled template, separated by a blank line
- Base fill — after all injectors run, remaining
{{slots}}are filled directly from context (strings and numbers only)
API
from contexting import assemble, create_injector
create_injector(slot, fn)
| param | type | description |
|---|---|---|
slot |
str |
name matching {{slot}} in the template |
fn |
Callable[[dict], str] |
receives the full context, returns the string to inject |
assemble(base_prompt, rules, context)
| param | type | description |
|---|---|---|
base_prompt |
str |
template string with {{slot}} placeholders |
rules |
list[Injector] |
ordered injectors applied before base fill |
context |
dict |
passed to every injector fn; also fills remaining slots |
Returns the assembled str.
Examples
Inline and appended injection
from contexting import assemble, create_injector
base_prompt = 'You are a {{role}} assistant for the language {{lang}}.'
# Inline — {{role}} exists in template, replaced directly
role_injector = create_injector('role', lambda ctx:
'senior software engineer' if ctx.get('senior') else 'helpful coding'
)
# Appended — {{constraints}} absent, output appended after a blank line
constraints_injector = create_injector('constraints', lambda ctx:
'Never suggest running untrusted code. Always explain risks.' if ctx.get('safe') else ''
)
lang_injector = create_injector('lang', lambda ctx:
'javascript, node and typescript' if ctx.get('lang') == 'javascript' else 'python and java'
)
prompt = assemble(base_prompt, [role_injector, constraints_injector, lang_injector], {
'senior': True,
'safe': True,
'lang': 'javascript',
})
print(prompt)
# You are a senior software engineer assistant for the language javascript, node and typescript.
#
# Never suggest running untrusted code. Always explain risks.
Base fill from context
Remaining slots not handled by any injector are filled directly from context:
prompt = assemble('Hello {{name}}, you are using {{lang}}.', [], {
'name': 'Alice',
'lang': 'python',
})
print(prompt)
# Hello Alice, you are using python.
Language-aware prompts
base_prompt = '{{lang_style}} Help the user with their question about {{topic}}.'
lang_style = create_injector('lang_style', lambda ctx: {
'es': 'Respond only in Spanish. Be warm and conversational.',
'fr': 'Respond only in French. Be formal and precise.',
}.get(ctx.get('lang'), 'Respond in English. Be clear and concise.'))
prompt = assemble(base_prompt, [lang_style], {'lang': 'es', 'topic': 'cooking'})
print(prompt)
# Respond only in Spanish. Be warm and conversational. Help the user with their question about cooking.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
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 contexting-0.1.1-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: contexting-0.1.1-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 598.6 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbb5129c10b9275be2c3d4dee66d63e63a1257eb628084f1d5f422880a3317d6
|
|
| MD5 |
0dcabba509ee77dcea4ffdebeceaee41
|
|
| BLAKE2b-256 |
50a38b17ba88ca38f66db6f77a7f3dd44fbe1f8e5868c692f10f72cb24e086fe
|
Provenance
The following attestation bundles were made for contexting-0.1.1-cp38-abi3-win_amd64.whl:
Publisher:
release.yml on ActuallyGoodClub/contexting
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
contexting-0.1.1-cp38-abi3-win_amd64.whl -
Subject digest:
fbb5129c10b9275be2c3d4dee66d63e63a1257eb628084f1d5f422880a3317d6 - Sigstore transparency entry: 1599962289
- Sigstore integration time:
-
Permalink:
ActuallyGoodClub/contexting@4781aced4ae96c4570bd2a392c4a27da745ca6c8 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/ActuallyGoodClub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4781aced4ae96c4570bd2a392c4a27da745ca6c8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 684.7 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c9452716c2546798ed6cde53e19ad34bdbe704589d328bb991c4799e054f92b
|
|
| MD5 |
42700f36249e17bb16d19631e7645ed2
|
|
| BLAKE2b-256 |
9d613fd4f33c56033bdde5d02a9f9102a0ec46d3db5d8debff4bee4cee887746
|
Provenance
The following attestation bundles were made for contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
release.yml on ActuallyGoodClub/contexting
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
7c9452716c2546798ed6cde53e19ad34bdbe704589d328bb991c4799e054f92b - Sigstore transparency entry: 1599962388
- Sigstore integration time:
-
Permalink:
ActuallyGoodClub/contexting@4781aced4ae96c4570bd2a392c4a27da745ca6c8 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/ActuallyGoodClub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4781aced4ae96c4570bd2a392c4a27da745ca6c8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 616.5 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4248c4878cc78ccb430dae59d94373ed53eefaffe186088a8426be0e1b202f9
|
|
| MD5 |
6167dbc615fcf9c46b4c54277b90b2c9
|
|
| BLAKE2b-256 |
6d3dcc1bc4cc0ebdab002aefa7d6cb80650e01edff825ba8d57a8140ca9f4ece
|
Provenance
The following attestation bundles were made for contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl:
Publisher:
release.yml on ActuallyGoodClub/contexting
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl -
Subject digest:
b4248c4878cc78ccb430dae59d94373ed53eefaffe186088a8426be0e1b202f9 - Sigstore transparency entry: 1599962507
- Sigstore integration time:
-
Permalink:
ActuallyGoodClub/contexting@4781aced4ae96c4570bd2a392c4a27da745ca6c8 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/ActuallyGoodClub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4781aced4ae96c4570bd2a392c4a27da745ca6c8 -
Trigger Event:
push
-
Statement type: