When building LLM applications, you sometimes need direct control over prompt compilation without routing through the AI Gateway. The SDK provides an alternative integration method that allows you to pull and compile prompts directly in your application.
SDK vs AI Gateway#
We provide SDKs for both TypeScript and Python that offer two ways to use Helicone prompts:
- AI Gateway Integration - Use prompts through the Helicone AI Gateway (recommended)
- Direct SDK Integration - Pull prompts directly via SDK (this page)
Prompts through the AI Gateway come with several benefits:
- Cleaner code: Automatically performs compilation and substitution in the router.
- Input traces: Traces inputs on each request for better observability in Helicone requests.
- Faster TTFT: The AI Gateway adds significantly less latency compared to the SDK.
The SDK is a great option for users that need direct interaction with compiled prompt bodies without using the AI Gateway.
Installation#
Note: The OpenAI Python SDK is required for prompt management features.
Types and Classes#
The SDK provides types for both integration methods when using the OpenAI SDK:
| Type | Description | Use Case |
|---|---|---|
HeliconeChatCreateParams | Standard chat completions with prompts | Non-streaming requests |
HeliconeChatCreateParamsStreaming | Streaming chat completions with prompts | Streaming requests |
Both types extend the OpenAI SDK's chat completion parameters and add:
prompt_id- Your saved prompt identifierenvironment- Optional environment to target (e.g., "production", "staging")version_id- Optional specific version (defaults to production version)inputs- Variable values
Important: These types make messages optional because Helicone prompts are expected to contain the required message structure. If your prompt template is empty or doesn't include messages, you'll need to provide them at runtime.
For direct SDK integration:
The SDK provides types that extend OpenAI's official types:
| Type | Description | Use Case |
|---|---|---|
HeliconeChatParams | Chat completion parameters with prompt support (includes environment) | All prompt requests |
PromptCompilationResult | Result with body and validation errors | Error handling |
The HeliconeChatParams type includes all OpenAI parameters plus:
prompt_id- Your saved prompt identifierenvironment- Optional environment to target (e.g., "production", "staging")version_id- Optional specific version (defaults to production version)inputs- Variable values for template substitution
Important: Similar to TypeScript, messages becomes optional when using prompts since your saved prompt template should contain the necessary message structure.
The main class for direct SDK integration:
Methods#
Both SDKs provide the HeliconePromptManager with these main methods:
| Method | Description | Returns |
|---|---|---|
pullPromptVersion() | Determine which prompt version to use | Prompt version object |
pullPromptBody() | Fetch raw prompt from storage | Raw prompt body |
pullPromptBodyByVersionId() | Fetch prompt by specific version ID | Raw prompt body |
mergePromptBody() | Merge prompt with inputs and validation | Compilation result |
getPromptBody() | Complete compile process with inputs | Compiled body + validation errors |
extractPromptPartials() | Extract prompt partial references from prompt body | Array of prompt partial objects |
getPromptPartialSubstitutionValue() | Get the content to substitute for a prompt partial | Substitution string |
Usage Examples#
Both approaches are fully compatible with all OpenAI SDK features including function calling, response formats, and advanced parameters. The HeliconePromptManager, while not providing input traces, will provide validation error handling.
Handling Prompt Partials#
Prompt partials allow you to reference messages from other prompts using the syntax {{hcp:prompt_id:index:environment}}. This enables code reuse across your prompt library.
AI Gateway vs SDK#
When using the AI Gateway, prompt partials are automatically resolved - you don't need to do anything special:
When using the SDK directly, you must manually resolve prompt partials by fetching and substituting the referenced prompts:
Understanding Prompt Partial Syntax#
Prompt partials use the format {{hcp:prompt_id:index:environment}}:
prompt_id- The 6-character alphanumeric identifier of the prompt to referenceindex- The message index (0-based) to extract from that promptenvironment- Optional environment identifier (defaults to production)
Examples:
If your prompts don't contain any prompt partials (no {{hcp:...}} tags), you don't need to worry about this section. The SDK will work normally without any special handling.
When using the SDK directly, each prompt partial requires a separate API call to fetch the referenced prompt. For prompts with many partials, consider using the AI Gateway instead for better performance and automatic caching.
