Skip to main content

launchdarkly-ai-langchain-messages

LangChain handler for launchdarkly-ai-server using LangChain chat models (langchain-core). Works with any BaseChatModel — defaults to ChatOpenAI. Runs a manual tool-call loop using LangChain's bind_tools API.

provides_for: ['*', 'messages'] — matches any flag variation where meta.mode is "messages" and no more-specific handler is registered. LangChain is a framework adapter, not a provider: it routes through langchain-anthropic, langchain-openai, and others at runtime based on config.provider.name. Use '*' so that flags configured with provider.name = "Anthropic" or "OpenAI" are automatically handled without requiring a separate native handler.

Pass providers=["Bedrock"] to create_langchain_messages_handler (and to langchain_messages) to accept only those config.provider.name values. Names must match the config exactly. Several scoped handlers can be registered together; the shortest list that contains the name wins. Omit providers to keep matching every provider.

Installation

pip install "launchdarkly-ai-server[ld]" launchdarkly-ai-langchain-messages

The default model is ChatOpenAI, so set OPENAI_API_KEY unless you pass a custom BaseChatModel.

langchain-openai is a dependency, so the default model works with the install above. For AI configs whose provider is Anthropic or Bedrock, install the matching extra:

pip install "launchdarkly-ai-langchain-messages[anthropic]"  # langchain-anthropic
pip install "launchdarkly-ai-langchain-messages[aws]"        # langchain-aws (Bedrock)

Usage

With the default model (ChatOpenAI)

import asyncio
from launchdarkly_ai_server import config, shutdown
from launchdarkly_ai_langchain_messages import create_langchain_messages_handler

async def main():
    result = await config(
        key="my-ai-config-flag",
        handler=create_langchain_messages_handler(),
    ).invoke("What is feature flagging?", {"kind": "user", "key": "user-123"})

    print(result.response)
    await shutdown()

asyncio.run(main())

With a custom BaseChatModel

from langchain_anthropic import ChatAnthropic
from launchdarkly_ai_langchain_messages import create_langchain_messages_handler

handler = create_langchain_messages_handler(ChatAnthropic(model="claude-opus-4-5"))

A constructed instance cannot see flag parameters. Pass a function instead if the model should be built after evaluation:

handler = create_langchain_messages_handler(
    lambda config: ChatAnthropic(
        **{
            **(config["model"].get("parameters") or {}),
            "model": config["model"]["name"],
        }
    )
)

Convenience wrapper

import asyncio
from launchdarkly_ai_langchain_messages import langchain_messages

async def main():
    user_input = "What is feature flagging?"
    result = await langchain_messages(
        user_input,
        {"kind": "user", "key": "user-123"},
        {"key": "my-ai-config-flag"},
        variables={"user_input": user_input},
    )
    print(result.response)

asyncio.run(main())

How It Works

  • Uses the system prompt and conversation history defined in your LaunchDarkly flag config.
  • Template placeholders ({{variable}}) in the prompt are substituted using variables before the call.
  • If tools are defined in the flag config, binds them to the model and executes them as requested, feeding results back until the model produces a final response.
  • Emits an OTel span and LaunchDarkly telemetry for every call.

Choosing Between langchain-agents and langchain-messages

langchain-agents langchain-messages
Orchestration LangGraph StateGraph Manual tool loop
Reasoning style ReAct (reason + act cycles) Single invoke per tool round-trip
Best for Complex multi-step reasoning Straightforward tool calls

Environment Variables

Variable Description
OPENAI_API_KEY Required when using the default ChatOpenAI model
LD_SDK_KEY LaunchDarkly server-side SDK key
LD_SERVICE_NAME OTel service.name resource attribute (default: python-sdk)
LD_ENVIRONMENT deployment.environment attribute attached to telemetry
OTEL_EXPORTER_OTLP_ENDPOINT OTLP endpoint override (default: LaunchDarkly Observability backend)

Metadata

Release files for launchdarkly-ai-langchain-messages 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for launchdarkly-ai-langchain-messages 1.1.0
File Size Uploaded Provenance Actions
launchdarkly_ai_langchain_messages-1.1.0.tar.gz 56.0 kB Without provenance Details for launchdarkly_ai_langchain_messages-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for launchdarkly-ai-langchain-messages 1.1.0
File Interpreter ABI Platform Actions
launchdarkly_ai_langchain_messages-1.1.0-py3-none-any.whl Python 3 none any Details for launchdarkly_ai_langchain_messages-1.1.0-py3-none-any.whl

Total release size: 81.1 kB

Release files / launchdarkly_ai_langchain_messages-1.1.0.tar.gz

Download URL launchdarkly_ai_langchain_messages-1.1.0.tar.gz
Size 56.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7b978126f76cc15aa3e1be8d379e52f806f23dec67c8c388f1e15f0055a589d8
BLAKE2b-256 checksum
How to use checksums
9b9106523f568e0925dcf54813d0e4a9495910a33fcd1bcfceb4f732ad1689df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / launchdarkly_ai_langchain_messages-1.1.0-py3-none-any.whl

Download URL launchdarkly_ai_langchain_messages-1.1.0-py3-none-any.whl
Size 25.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef539a0e0b09a9e7fd8abec5395bb33baca04464757da3b3caabafbe5b0a699a
BLAKE2b-256 checksum
How to use checksums
d6b3182e8edcf5ab85f70c67c6ca40eca6ad00cda426dc4eaa12f5f8938dd603
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for launchdarkly-ai-langchain-messages 1.1.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page