Official Python SDK for interacting with the PromptOS.dev API prompts endpoints.
pip install promptos
from promptos import PromptOSClient
# Initialize the client with your API key
client = PromptOSClient(api_key="mpa_live_your_api_key_here")
# List all prompts
prompts = client.prompts.list()
for prompt in prompts.data:
print(f"{prompt.title} - {prompt.id}")
# Render a prompt
result = client.prompts.render(
prompt_id="your_prompt_id",
inputs={"name": "John", "topic": "Python"}
)
print(result.prompt)
The SDK requires an API key for authentication. You can obtain an API key from your PromptOS dashboard.
API keys come in two types:
- Live keys (
mpa_live_...
): For production use - Test keys (
mpa_test_...
): For development and testing
# Production client
client = PromptOSClient(api_key="mpa_live_...")
# Development client
client = PromptOSClient(api_key="mpa_test_...")
You can customize the client behavior:
client = PromptOSClient(
api_key="mpa_live_...",
base_url="http://localhost:3001", # Custom API endpoint
timeout=60, # Request timeout in seconds
max_retries=5 # Number of retries for failed requests
)
List all prompts in your organization with pagination support.
# Basic usage
prompts = client.prompts.list()
# With pagination
prompts = client.prompts.list(page=2, limit=50)
# Access results
for prompt in prompts.data:
print(f"ID: {prompt.id}")
print(f"Title: {prompt.title}")
print(f"Tags: {prompt.tags}")
if prompt.latest_version:
print(f"Latest version: v{prompt.latest_version.version}")
Retrieve detailed information about a specific prompt, including all versions.
prompt = client.prompts.get("prompt_id_here")
print(f"Prompt: {prompt.title}")
print(f"Description: {prompt.description}")
print(f"Alias: {prompt.alias}")
print(f"Number of versions: {len(prompt.versions)}")
# Access version history
for version in prompt.versions:
print(f"Version {version.version}: {version.status}")
print(f"Content: {version.content}")
if version.variables:
print(f"Variables: {version.variables}")
Render a prompt with the latest published version.
# Render by ID
result = client.prompts.render(
prompt_id="prompt_id_here",
inputs={
"name": "Alice",
"product": "PromptOS",
"feature": "SDK"
}
)
print(result.prompt) # The rendered prompt text
print(f"Version used: {result.metadata['version']}")
print(f"Variables used: {result.metadata['variables']}")
# Render by alias
result = client.prompts.render_by_alias(
alias="welcome-email",
inputs={"name": "Bob"}
)
The SDK provides specific exception types for different error scenarios:
from promptos import (
PromptOSError,
AuthenticationError,
ValidationError,
NotFoundError,
RateLimitError,
ServerError
)
try:
result = client.prompts.render("invalid_id")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
print(f"Status code: {e.status_code}")
except NotFoundError as e:
print(f"Prompt not found: {e}")
except ValidationError as e:
print(f"Invalid request: {e}")
print(f"Details: {e.response}")
except RateLimitError as e:
print(f"Rate limit exceeded: {e}")
except ServerError as e:
print(f"Server error: {e}")
except PromptOSError as e:
print(f"General error: {e}")
The SDK uses Pydantic models for type safety and validation:
Prompt
: Basic prompt information (used in list responses)PromptDetail
: Full prompt details including all versionsPromptVersion
: Individual version informationPromptListResponse
: Response from list endpoint with paginationPromptRenderResponse
: Response from render endpoint
See the examples/
directory for more detailed examples:
basic_usage.py
: Basic operationsprompt_rendering.py
: Advanced rendering exampleserror_handling.py
: Comprehensive error handling
# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
python -m pytest tests/
# Run with coverage
python -m pytest tests/ --cov=promptos
mypy promptos
MIT License - see LICENSE file for details.
- Documentation: https://docs.promptos.dev
- API Reference: https://api.promptos.dev
- Issues: https://github.com/promptos/python-sdk/issues