OTEL-compliant instrumentation for Langfuse to send traces to Keywords AI
Project description
Keywords AI Instrumentation for Langfuse
OTEL-compliant automatic instrumentation for Langfuse that sends all traces to Keywords AI.
✅ Why This Implementation is OTEL-Compliant
This package follows OpenTelemetry best practices:
- ✅ Uses
BaseInstrumentor- Standard OTEL instrumentation interface - ✅ Uses
wraptfor safe patching - Reliable, reversible monkey-patching of httpx.Client - ✅ Single responsibility - Only does instrumentation (HTTP interception), not span processing
- ✅ No import substitution - Use
from langfuse import Langfusenormally! - ✅ Minimal interception - Only redirects Langfuse's HTTP requests to Keywords AI
- ✅ Supports uninstrumentation - Can be cleanly enabled/disabled
- ✅ Compatible with auto-instrumentation - Works with
opentelemetry-instrument
Installation
pip install keywordsai-instrumentation-langfuse
Usage
Option 1: Standard OTEL Pattern (Recommended)
# IMPORTANT: Instrument BEFORE importing Langfuse
from keywordsai_instrumentation_langfuse import LangfuseInstrumentor
LangfuseInstrumentor().instrument(api_key="your-keywordsai-api-key")
# Now import and use Langfuse normally - NO code changes needed!
from langfuse import Langfuse, observe
langfuse = Langfuse(
public_key="your-langfuse-public-key",
secret_key="your-langfuse-secret-key",
)
@observe()
def my_llm_function(query: str):
# Your LLM code here
return f"Response to: {query}"
# All traces automatically go to Keywords AI!
result = my_llm_function("Hello!")
langfuse.flush()
Option 2: Using Environment Variables
export KEYWORDSAI_API_KEY="your-api-key"
export LANGFUSE_PUBLIC_KEY="your-langfuse-public-key"
export LANGFUSE_SECRET_KEY="your-langfuse-secret-key"
# Instrument before importing Langfuse
from keywordsai_instrumentation_langfuse import LangfuseInstrumentor
LangfuseInstrumentor().instrument()
# Use Langfuse normally
from langfuse import Langfuse, observe
Option 3: Zero-Code Auto-Instrumentation
If you have opentelemetry-instrument configured:
export KEYWORDSAI_API_KEY="your-api-key"
export LANGFUSE_PUBLIC_KEY="your-langfuse-public-key"
export LANGFUSE_SECRET_KEY="your-langfuse-secret-key"
opentelemetry-instrument python your_app.py
How It Works
The instrumentor patches Langfuse's HTTP client to redirect trace data from Langfuse backend to Keywords AI:
- You call
LangfuseInstrumentor().instrument()before importing Langfuse - This patches
httpx.Client.send()usingwrapt(safe, reversible) - You use Langfuse normally with
@observe()decorators - When Langfuse sends data to its backend, the patch intercepts the HTTP request
- Data is transformed from Langfuse format to Keywords AI format
- Request is redirected to Keywords AI API instead
Why patch before import? The HTTP client must be patched before Langfuse creates its client instances. This is standard OTEL instrumentation behavior.
Configuration
Environment Variables
KEYWORDSAI_API_KEY- Your Keywords AI API key (required)KEYWORDSAI_ENDPOINT- Custom endpoint URL (optional, defaults to production)KEYWORDSAI_AUTO_INSTRUMENT- Set to "true" to auto-instrument on import (optional)
Programmatic Configuration
from keywordsai_instrumentation_langfuse import instrument
instrument(
api_key="your-api-key",
endpoint="https://custom-endpoint.com/api/v1/traces/ingest",
)
Advanced Usage
Custom Endpoint
from keywordsai_instrumentation_langfuse import LangfuseInstrumentor
LangfuseInstrumentor().instrument(
api_key="your-api-key",
endpoint="https://your-instance.com/api/v1/traces/ingest"
)
from langfuse import Langfuse, observe
Disabling Instrumentation
from keywordsai_instrumentation_langfuse import LangfuseInstrumentor
instrumentor = LangfuseInstrumentor()
instrumentor.instrument(api_key="your-key")
# ... use Langfuse ...
# Disable when done
instrumentor.uninstrument()
Troubleshooting
Spans Not Appearing in Keywords AI
- Check your API key is set correctly
- Ensure
langfuse.flush()is called before program exit - Check logs for any error messages
- Verify network connectivity to Keywords AI API
Import Errors
Make sure all dependencies are installed:
pip install opentelemetry-api opentelemetry-sdk opentelemetry-instrumentation wrapt langfuse requests
Compatibility Issues
- Requires Python 3.8+
- Requires Langfuse SDK v2.0.0+
- Requires OpenTelemetry SDK v1.20.0+
Development
# Install with dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black src/
# Lint
ruff check src/
What Makes This OTEL-Compliant?
- Uses
BaseInstrumentor- Standard OTEL pattern with.instrument()and.uninstrument() - Uses
wraptfor Patching - Safe, reversible monkey-patching of httpx client - Single Responsibility - Only intercepts HTTP requests, doesn't create custom span processors
- No Import Substitution - Use
from langfuse import Langfusenormally - Entry Points - Supports
opentelemetry-instrumentauto-instrumentation
License
Apache 2.0
Support
- Documentation: https://docs.keywordsai.co
- Issues: https://github.com/keywordsai/keywordsai-instrumentation-langfuse/issues
- Email: support@keywordsai.co
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
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 keywordsai_instrumentation_langfuse-0.1.0.tar.gz.
File metadata
- Download URL: keywordsai_instrumentation_langfuse-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.8 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ebd415907b34174b9b0a90d33d4c2c8f67d13a171f65bc6ef868cd32e838844
|
|
| MD5 |
14471582d5a0acb8da6078ee2fbc16b2
|
|
| BLAKE2b-256 |
ab34dd5558120e5844dd4233d55c92eeda77ce6dba3196b61f9f0f72d1a91cc9
|
File details
Details for the file keywordsai_instrumentation_langfuse-0.1.0-py3-none-any.whl.
File metadata
- Download URL: keywordsai_instrumentation_langfuse-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.8 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
103d84a96a9f59aa568c8eabe91491aa69cee75c4439fc5189470e1b310d3847
|
|
| MD5 |
79eade20fdb5ff5a898ab062aa7f48c1
|
|
| BLAKE2b-256 |
10e6cbfe10c700e0c5bf37c03dd73f03f0256f3deec558c062dd1f171e7aef94
|