PromptLayer

raw JSON →
1.4.3 verified Fri May 01 auth: no python

PromptLayer is a platform for prompt engineering and tracking LLM requests. It provides middleware to log, debug, and analyze prompts for OpenAI, Anthropic, and other providers. Current version 1.4.3, actively maintained with regular releases.

pip install promptlayer
error ModuleNotFoundError: No module named 'promptlayer'
cause PromptLayer not installed or installed in wrong environment.
fix
Run pip install promptlayer in your current Python environment.
error AttributeError: module 'promptlayer' has no attribute 'openai'
cause Outdated version of promptlayer that does not have the openai submodule (pre-1.0).
fix
Upgrade promptlayer: pip install --upgrade promptlayer
error InvalidRequestError: API key not provided. Set your PromptLayer API key via promptlayer.api_key or environment variable.
cause PromptLayer API key missing.
fix
Set promptlayer.api_key = 'your_key' or export PROMPTLAYER_API_KEY environment variable.
breaking In version 1.0, the default import changed. Previously `import promptlayer` would patch modules automatically; now you must explicitly use the wrapper classes (e.g., PromptLayerOpenAI) or call `promptlayer.openai.patch()`.
fix Use `from promptlayer.openai import PromptLayerOpenAI` and instantiate it, or call `promptlayer.openai.patch()` after import.
deprecated The method `promptlayer.openai.patch()` is deprecated. Direct class instantiation is preferred.
fix Use `PromptLayerOpenAI` wrapper class instead of patching.
gotcha The `api_key` must be set for PromptLayer to log requests. If not set, requests will fail silently or raise an error depending on the version.
fix Set `promptlayer.api_key = 'your_api_key'` or export `PROMPTLAYER_API_KEY` environment variable.

Initializes PromptLayer, sets API key, and uses the PromptLayerOpenAI client to track an OpenAI call.

import promptlayer

# Initialize PromptLayer with your API key
promptlayer.api_key = os.environ.get('PROMPTLAYER_API_KEY', '')

# Wrap OpenAI client for tracking
from promptlayer.openai import PromptLayerOpenAI
import openai

# Option 1: Use PromptLayerOpenAI client directly
client = PromptLayerOpenAI(api_key=os.environ.get('OPENAI_API_KEY', ''), pl_tags=['test'])
response = client.chat.completions.create(
    model='gpt-3.5-turbo',
    messages=[{'role': 'user', 'content': 'Hello!'}]
)
print(response.choices[0].message.content)

# Option 2: Patch the default OpenAI module (deprecated in v1.0)
# promptlayer.openai.patch()
# response = openai.ChatCompletion.create(...)