A production-ready wrapper for Langfuse prompts and tracing with LLM integrations
Project description
Langfuse Prompt Library
A production-ready Python wrapper for Langfuse prompts and tracing with integrated LLM support (OpenAI, Anthropic).
Features
- Prompt Management: Fetch and manage prompts from Langfuse with built-in caching
- Multi-Provider Support: Works with OpenAI (GPT models) and Anthropic (Claude models)
- Automatic Tracing: Built-in observability and tracing for all LLM calls
- Token Tracking: Automatic token usage tracking and reporting
- Retry Logic: Configurable retry mechanism with exponential backoff
- Type Safety: Full type hints and validation
- Thread-Safe Caching: Efficient prompt caching with TTL support
- Error Handling: Comprehensive error handling and custom exceptions
- Production Ready: Designed for enterprise use with logging, metrics, and cleanup handlers
Installation
pip install langfuse-prompt-library-iauro
With Anthropic Support
pip install langfuse-prompt-library-iauro[anthropic]
Quick Start
Basic Usage
from langfuse_prompt_library import LangfuseManager
# Initialize manager (loads from environment variables)
lf = LangfuseManager()
# Call LLM with a prompt
response = lf.call_llm(
prompt_name="customer_support_agent",
user_input="How do I reset my password?",
prompt_label="production",
model="gpt-3.5-turbo"
)
Using Claude (Anthropic)
# Claude models are auto-detected
response = lf.call_llm(
prompt_name="assistant",
user_input="Explain quantum computing",
model="claude-sonnet-4",
temperature=0.7
)
Advanced Usage
from langfuse_prompt_library import LangfuseManager, LangfuseConfig
# Custom configuration
config = LangfuseConfig(
secret_key="your-secret-key",
public_key="your-public-key",
host="https://cloud.langfuse.com",
openai_api_key="your-openai-key",
enable_caching=True,
cache_ttl=3600,
request_timeout=60.0
)
lf = LangfuseManager(config=config)
# Fetch prompt manually
prompt = lf.get_prompt("my_prompt", label="production")
messages = lf.compile_prompt(prompt, user_input="Hello")
# Get cache statistics
stats = lf.get_cache_stats()
# Flush traces before shutdown
lf.flush()
Configuration
Set these environment variables or pass them via LangfuseConfig:
# Required
LANGFUSE_SECRET_KEY=your-secret-key
LANGFUSE_PUBLIC_KEY=your-public-key
# Optional
LANGFUSE_HOST=https://cloud.langfuse.com
OPENAI_API_KEY=your-openai-key
ANTHROPIC_API_KEY=your-anthropic-key
# Advanced options
LANGFUSE_ENABLE_CACHING=true
LANGFUSE_CACHE_TTL=3600
LANGFUSE_REQUEST_TIMEOUT=60.0
LANGFUSE_DEBUG=false
LANGFUSE_LOG_LEVEL=INFO
API Reference
LangfuseManager
Main entry point for the library.
Methods
call_llm(prompt_name, user_input, ...)- High-level method to fetch prompt and call LLMget_prompt(name, version, label, cache)- Fetch a prompt from Langfusecompile_prompt(prompt, **variables)- Compile prompt with variablesflush()- Flush pending traces to Langfuseget_cache_stats()- Get cache statisticsclear_cache()- Clear the prompt cache
LLMResponse
Response object containing:
content- The generated textmodel- Model usedusage- Token usage dict with 'input', 'output', 'total'metadata- Additional metadataraw_response- Raw API response
Exceptions
LangfuseLibraryError- Base exceptionConfigurationError- Configuration issuesPromptNotFoundError- Prompt not foundProviderError- LLM provider errorsAPITimeoutError- API timeoutRateLimitError- Rate limit exceededValidationError- Input validation failed
Examples
Error Handling
from langfuse_prompt_library import (
LangfuseManager,
PromptNotFoundError,
ProviderError
)
lf = LangfuseManager()
try:
response = lf.call_llm(
prompt_name="nonexistent_prompt",
user_input="Hello"
)
except PromptNotFoundError as e:
print(f"Prompt not found: {e}")
except ProviderError as e:
print(f"Provider error: {e}")
Specific Prompt Version
# Use specific version
response = lf.call_llm(
prompt_name="assistant",
user_input="Hello",
prompt_version=5
)
# Or use label
response = lf.call_llm(
prompt_name="assistant",
user_input="Hello",
prompt_label="production"
)
Requirements
- Python >= 3.8
- langfuse >= 3.0.0
- openai >= 1.0.0
- python-dotenv >= 1.0.0
- anthropic >= 0.18.0 (optional)
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Support
For issues and questions:
- GitHub Issues: https://gitlab.iauro.co/hpe/backend/langfuse-prompt-library/issues
- Langfuse Documentation: https://langfuse.com/docs
Changelog
0.1.0
- Initial public release
- Support for OpenAI and Anthropic
- Automatic tracing and observability
- Thread-safe caching
- Comprehensive error handling
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 langfuse_prompt_library_iauro-0.1.1.tar.gz.
File metadata
- Download URL: langfuse_prompt_library_iauro-0.1.1.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e5aec5fd823f842404ae8c385066f83355a7583072fb6a0d130a7e0ef76c139
|
|
| MD5 |
eaf9c46e69a874a409f0313e177d7d11
|
|
| BLAKE2b-256 |
44f37772e45542e5afee72ebfa93ed91f1224ec23c65c86476c88e0f7447d2e4
|
File details
Details for the file langfuse_prompt_library_iauro-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langfuse_prompt_library_iauro-0.1.1-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8042b9d4777b3f4acde47b3f81fb90d9e74a384e4e90278439be0d78ad766242
|
|
| MD5 |
8df1f1a22865907f1212942f8c1cb4bc
|
|
| BLAKE2b-256 |
9c93f6393b178ed6dc80cee5dc6e1fcd602b7ec34fcaa0bdf18bc88783a076fb
|