PromptStream Python SDK
1. Introduction
PromptStream’s Python SDK lets you run prompts and capture user events from your Python applications. Key concepts:- Prompt – a versioned template you create in the dashboard
- Prompt Run – a single execution of a prompt with inputs
- Experiment – groups runs for A/B testing
- Variant – the specific prompt version assigned to a run
- Events – clicks, conversions, feedback, ratings, or engagement metrics
2. Installation
Requirements
- Python \u2265 3.x
promptstreamand its dependencies
Install via pip
Importing
3. Authentication & Client Initialization
Generate an API key from your PromptStream dashboard. Pass it directly or via thePROMPTSTREAM_API_KEY environment variable.
4. Core API
4.1 run_prompt
| Name | Type | Description |
|---|---|---|
prompt_id | str | UUID of your PromptStream prompt |
inputs | dict | Mapping of input names to values |
external_user_id | str? | Optional stable end-user ID |
RunResponse with:
run_idexperiment_idvariant_assignedoutput
Errors
Authentication failures, invalid prompt IDs, and network issues raise exceptions.4.2 Event-Tracking Methods
All event methods acceptprompt_run_id, experiment_id, and optional external_user_id.
4.2.1 track_click
4.2.2 track_conversion
4.2.3 track_feedback
4.2.4 track_rating
4.2.5 track_engagement
5. Response Types & Data Models
RunResponse fields:
| Field | Type | Description |
|---|---|---|
run_id | str | Unique identifier for this run |
experiment_id | str | Experiment the run belongs to |
variant_assigned | str | Version used |
output | str | Prompt output text |
AuthenticationError, PromptNotFoundError, and RateLimitError.
6. Usage Examples
Basic Prompt Run
Batch Runs & Bulk Tracking
Loop over your data to run prompts and track outcomes in bulk.7. Best Practices
- Use idempotent identifiers for runs when possible
- Standardize event labels and metadata
- Handle retries for transient failures
- Avoid storing PII or send hashes when required for GDPR compliance
