High-performance Python SDK for fetching prompts from PromptCanvas
Project description
PromptCanvas Python SDK
High-performance Python SDK for fetching prompts from PromptCanvas with built-in caching, prefetching, and variable resolution.
Features
- Sync & Async - Full support for both synchronous and asynchronous code
- LRU Caching - Thread-safe cache with TTL and stale-while-revalidate
- Prefetching - Warm up cache on initialization
- Variable Resolution - Template interpolation with
{{ variable }}syntax - Autocomplete - Slug suggestions for IDE integration
- Batch Fetching - Single request for multiple prompts
- Type Hints - Full type annotations with dataclasses
Installation
pip install promptcanvas
Or install from source:
cd packages/python-sdk
pip install -e .
Quick Start
from promptcanvas import PromptCanvas
# Initialize
pc = PromptCanvas(api_key="pc_live_xxx")
# Fetch a prompt
prompt = pc.get("system-prompt")
print(prompt.content)
# With variables
prompt = pc.get("welcome-message", variables={
"user_name": "John",
"company": "Acme"
})
# Template: "Hello {{ user_name }}, welcome to {{ company }}!"
# Result: "Hello John, welcome to Acme!"
Async Usage
import asyncio
from promptcanvas import PromptCanvas
async def main():
pc = PromptCanvas(api_key="pc_live_xxx")
# Async fetch
prompt = await pc.aget("system-prompt")
print(prompt.content)
# Batch fetch
result = await pc.aget_many(["prompt-1", "prompt-2"])
for p in result.prompts:
print(p.slug, p.content)
await pc.aclose()
asyncio.run(main())
Configuration
pc = PromptCanvas(
# Required
api_key="pc_live_xxx",
# Optional
base_url="https://your-instance.supabase.co/functions/v1/sdk",
timeout=10.0, # Request timeout in seconds
cache_ttl=300.0, # Cache TTL in seconds (5 minutes)
cache_max_size=1000, # Max cached prompts
stale_while_revalidate=True, # Return stale while refreshing
max_retries=3, # Retry attempts
prefetch=["critical-prompt"], # Prefetch on init
debug=False, # Enable debug logging
)
API Reference
Sync Methods
| Method | Description |
|---|---|
get(slug, variables?, skip_cache?) |
Fetch single prompt |
get_many(slugs, variables?, skip_cache?) |
Batch fetch |
suggest(query, limit?) |
Autocomplete suggestions |
list(category?, limit?, offset?) |
List all prompts |
warmup(slugs) |
Pre-populate cache |
invalidate(slug) |
Remove from cache |
clear_cache() |
Clear all cache |
get_cache_stats() |
Get cache statistics |
Async Methods
| Method | Description |
|---|---|
aget(slug, variables?, skip_cache?) |
Fetch single prompt |
aget_many(slugs, variables?, skip_cache?) |
Batch fetch |
asuggest(query, limit?) |
Autocomplete suggestions |
alist(category?, limit?, offset?) |
List all prompts |
awarmup(slugs) |
Pre-populate cache |
ainvalidate(slug) |
Remove from cache |
aclear_cache() |
Clear all cache |
Error Handling
from promptcanvas import (
PromptCanvas,
PromptNotFoundError,
NoProductionVersionError,
RateLimitError,
AuthenticationError,
)
pc = PromptCanvas(api_key="pc_live_xxx")
try:
prompt = pc.get("my-prompt")
except PromptNotFoundError as e:
print(f"Prompt not found: {e.slug}")
except NoProductionVersionError as e:
print(f"No production version: {e.slug}")
except RateLimitError as e:
print(f"Rate limited, retry after {e.retry_after_ms}ms")
except AuthenticationError:
print("Invalid API key")
Context Manager
# Sync
with PromptCanvas(api_key="xxx") as pc:
prompt = pc.get("my-prompt")
# Async
async with PromptCanvas(api_key="xxx") as pc:
prompt = await pc.aget("my-prompt")
Variable Resolution
from promptcanvas import VariableResolver
# Extract variables from template
template = "Hello {{ name }}, your order {{ order_id }} is ready."
variables = VariableResolver.extract(template)
# ['name', 'order_id']
# Validate variables
valid, missing = VariableResolver.validate(template, {"name": "John"})
# (False, ['order_id'])
# Create resolver with globals
resolver = VariableResolver.create_with_globals({"app_name": "MyApp"})
result = resolver("Welcome to {{ app_name }}, {{ user }}!", {"user": "John"})
# "Welcome to MyApp, John!"
License
MIT
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 Distribution
promptcanvas-1.0.0.tar.gz
(10.7 kB
view details)
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 promptcanvas-1.0.0.tar.gz.
File metadata
- Download URL: promptcanvas-1.0.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdc2588002dc815e989298d0ed4418c7c674effc794d1955ae1119fb098d6c42
|
|
| MD5 |
5a81f182598a79c07e054956732b18a5
|
|
| BLAKE2b-256 |
f29c6d2397fd5312b44b4ac373ddea4a6c35a9aac29047dc0f26b7a379413ca0
|
File details
Details for the file promptcanvas-1.0.0-py3-none-any.whl.
File metadata
- Download URL: promptcanvas-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78f54842e465485850ce650c0a9f88c268c64a9f8d936b3ac14416a848bdb402
|
|
| MD5 |
82be6c73eba039947900c9087ac27788
|
|
| BLAKE2b-256 |
a5ccd4dcd13e2dbf812f07dac0a9172c68e5fc99444ff8de1d3c6e3a4937b344
|