LaunchDarkly AI SDK OpenAI Provider
This package provides an OpenAI integration for the LaunchDarkly AI SDK.
Installation
pip install launchdarkly-server-sdk-ai-openai
Quick Start
import asyncio
from ldclient import LDClient, Config, Context
from ldai import LDAIClient
from ldai.models import AICompletionConfigDefault, ModelConfig, ProviderConfig
# Initialize LaunchDarkly client
ld_client = LDClient(Config("your-sdk-key"))
ai_client = LDAIClient(ld_client)
context = Context.builder("user-123").build()
async def main():
# Create a ManagedModel backed by the OpenAI provider
model = ai_client.create_model(
"ai-config-key",
context,
AICompletionConfigDefault(
enabled=True,
model=ModelConfig("gpt-4"),
provider=ProviderConfig("openai"),
),
)
if model:
result = await model.run("Hello, how are you?")
print(result.content)
asyncio.run(main())
Usage
Using create_model (recommended)
The recommended entry point is LDAIClient.create_model, which evaluates a
LaunchDarkly AI config flag, selects the OpenAI runner automatically, and
returns a ManagedModel that wraps the runner:
model = ai_client.create_model("ai-config-key", context)
if model:
result = await model.run("What is feature flagging?")
print(result.content)
Using the runner directly
If you need to construct a runner manually (e.g. for testing), you can use
OpenAIRunnerFactory from the ldai_openai package:
from ldai_openai import OpenAIRunnerFactory
factory = OpenAIRunnerFactory() # uses OPENAI_API_KEY from environment
runner = factory.create_model(ai_config)
result = await runner.run("Hello!")
print(result.content)
Structured Output
Pass a JSON schema dict as output_type to request structured output:
response_structure = {
"type": "object",
"properties": {
"sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]},
"confidence": {"type": "number"},
},
"required": ["sentiment", "confidence"],
}
result = await runner.run(messages, output_type=response_structure)
print(result.parsed) # {"sentiment": "positive", "confidence": 0.95}
Tracking Metrics
ManagedModel.run() automatically tracks metrics via the associated
LDAIConfigTracker. For manual tracking, use the tracker directly:
model = ai_client.create_model("ai-config-key", context)
if model:
result = await model.run("Explain feature flags.")
# Metrics are tracked automatically; access them via result.metrics
print(result.metrics.tokens)
Static Utility Methods
The ldai_openai helper module provides several utility functions:
Converting Messages
from ldai.models import LDMessage
from ldai_openai import convert_messages_to_openai
messages = [
LDMessage(role="system", content="You are helpful."),
LDMessage(role="user", content="Hello!"),
]
openai_messages = convert_messages_to_openai(messages)
Extracting Metrics
from ldai_openai import get_ai_metrics_from_response
# After getting a response from OpenAI
metrics = get_ai_metrics_from_response(response)
print(f"Success: {metrics.success}")
print(f"Tokens used: {metrics.tokens.total if metrics.tokens else 'N/A'}")
Documentation
For full documentation, please refer to the LaunchDarkly AI SDK documentation.
Contributing
See CONTRIBUTING.md in the repository root.
License
Apache-2.0
Metadata
Release files for launchdarkly-server-sdk-ai-openai 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| launchdarkly_server_sdk_ai_openai-0.7.0.tar.gz | 21.8 kB | Without provenance | Details for launchdarkly_server_sdk_ai_openai-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| launchdarkly_server_sdk_ai_openai-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for launchdarkly_server_sdk_ai_openai-0.7.0-py3-none-any.whl |
Total release size: 35.7 kB
Release files / launchdarkly_server_sdk_ai_openai-0.7.0.tar.gz
| Download URL | launchdarkly_server_sdk_ai_openai-0.7.0.tar.gz |
|---|---|
| Size | 21.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8fac9b3d908e9002fae539bd585eda86e92e342fd4c88d78bbde823296efe4db
|
|
BLAKE2b-256 checksum How to use checksums |
e93a90dddbfcff9511d6f4f3fb4cc7d4d1c3de741d7df39c4d3634656f8624c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / launchdarkly_server_sdk_ai_openai-0.7.0-py3-none-any.whl
| Download URL | launchdarkly_server_sdk_ai_openai-0.7.0-py3-none-any.whl |
|---|---|
| Size | 13.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
38a4a3681736969f0d9846dbe1f2a9ccd86e2f1331ace92716ad0a22b1c326ba
|
|
BLAKE2b-256 checksum How to use checksums |
da3bdf40547f2460818692e7d7f86ef9d836c81eeb2398652e0e2e4f2ced0b6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for launchdarkly-server-sdk-ai-openai 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.