From 3aaca7db89bfa3ecdb05ebf3724183011fec35b6 Mon Sep 17 00:00:00 2001 From: jsonbailey Date: Mon, 20 Apr 2026 12:26:05 -0500 Subject: [PATCH 1/2] chore: update deprecated API usage in hello-python-ai examples Co-Authored-By: Claude Sonnet 4.6 --- examples/bedrock_example.py | 3 ++- examples/chat_judge_example.py | 9 +++++---- examples/chat_observability_example.py | 8 +++++--- examples/direct_judge_example.py | 5 +++-- examples/gemini_example.py | 3 ++- examples/langchain_example.py | 3 ++- examples/langgraph_agent_example.py | 3 ++- examples/langgraph_multi_agent_example.py | 3 ++- examples/openai_example.py | 11 +++++++---- 9 files changed, 30 insertions(+), 18 deletions(-) diff --git a/examples/bedrock_example.py b/examples/bedrock_example.py index f3990e9..a558398 100755 --- a/examples/bedrock_example.py +++ b/examples/bedrock_example.py @@ -85,5 +85,6 @@ def main(): # Continue the conversation by adding user input to the messages list and invoking the LLM again. print("Success.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/chat_judge_example.py b/examples/chat_judge_example.py index 90a4d18..1b91409 100644 --- a/examples/chat_judge_example.py +++ b/examples/chat_judge_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai import LDAIClient, AICompletionConfigDefault +from ldai.client import LDAIClient, AICompletionConfigDefault # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') @@ -47,8 +47,8 @@ async def async_main(): # provider={'name': 'openai'}, # messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}], # ) - # chat = await aiclient.create_chat(ai_config_key, context, default, {'companyName': 'LaunchDarkly'}) - chat = await aiclient.create_chat(ai_config_key, context, variables={ + # chat = await aiclient.create_model(ai_config_key, context, default, {'companyName': 'LaunchDarkly'}) + chat = await aiclient.create_model(ai_config_key, context, variables={ 'companyName': 'LaunchDarkly', }) @@ -98,7 +98,8 @@ async def async_main(): except Exception as err: print("Error:", err) finally: - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/chat_observability_example.py b/examples/chat_observability_example.py index 9751eed..41cb3df 100644 --- a/examples/chat_observability_example.py +++ b/examples/chat_observability_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai import LDAIClient, AICompletionConfigDefault +from ldai.client import LDAIClient, AICompletionConfigDefault from ldobserve import ObservabilityConfig, ObservabilityPlugin logging.getLogger('ldclient').setLevel(logging.WARNING) @@ -67,8 +67,8 @@ async def async_main(): # provider={'name': 'openai'}, # messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}], # ) - # chat = await aiclient.create_chat(ai_config_key, context, default, {'example_type': 'observability_demo'}) - chat = await aiclient.create_chat( + # chat = await aiclient.create_model(ai_config_key, context, default, {'example_type': 'observability_demo'}) + chat = await aiclient.create_model( ai_config_key, context, variables={ @@ -107,6 +107,8 @@ async def async_main(): except Exception as err: print("Error:", err) finally: + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/direct_judge_example.py b/examples/direct_judge_example.py index 510e41f..b94bc22 100644 --- a/examples/direct_judge_example.py +++ b/examples/direct_judge_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai import LDAIClient, AIJudgeConfigDefault +from ldai.client import LDAIClient, AIJudgeConfigDefault # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') @@ -86,7 +86,8 @@ async def async_main(): except Exception as err: print("Error:", err) finally: - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/gemini_example.py b/examples/gemini_example.py index 5c2939e..e27bd76 100644 --- a/examples/gemini_example.py +++ b/examples/gemini_example.py @@ -162,7 +162,8 @@ def main(): # Continue the conversation by adding user input to the messages list and invoking the LLM again. print("Success.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() if __name__ == "__main__": diff --git a/examples/langchain_example.py b/examples/langchain_example.py index 0fe3f70..eb9430b 100644 --- a/examples/langchain_example.py +++ b/examples/langchain_example.py @@ -104,7 +104,8 @@ async def async_main(): print(f"Error during completion: {e}") print("Please ensure you have the correct API keys and credentials set up for the detected provider.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/langgraph_agent_example.py b/examples/langgraph_agent_example.py index 93b072f..6df7e40 100644 --- a/examples/langgraph_agent_example.py +++ b/examples/langgraph_agent_example.py @@ -123,7 +123,8 @@ def main(): print(f"Error: {e}") print("Please ensure you have the correct API keys and credentials set up for the detected providers.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() if __name__ == "__main__": diff --git a/examples/langgraph_multi_agent_example.py b/examples/langgraph_multi_agent_example.py index 6989b4a..0a53f26 100644 --- a/examples/langgraph_multi_agent_example.py +++ b/examples/langgraph_multi_agent_example.py @@ -271,7 +271,8 @@ def calculate_average(numbers): print(f"❌ Error during workflow execution: {e}") print("Please ensure you have the correct API keys and credentials set up for the detected providers.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() if __name__ == "__main__": diff --git a/examples/openai_example.py b/examples/openai_example.py index 2330ab2..995de39 100755 --- a/examples/openai_example.py +++ b/examples/openai_example.py @@ -3,6 +3,7 @@ from ldclient import Context from ldclient.config import Config from ldai.client import LDAIClient +from ldai_openai import get_ai_metrics_from_response from openai import OpenAI openai_client = OpenAI() @@ -69,13 +70,14 @@ def main(): print("User Input:\n", USER_INPUT) messages.append({'role': 'user', 'content': USER_INPUT}) - # Track the OpenAI completion with LaunchDarkly metrics - completion = tracker.track_openai_metrics( + # Track the OpenAI completion with LaunchDarkly metrics using the LD OpenAI provider's extractor + completion = tracker.track_metrics_of( lambda: openai_client.chat.completions.create( model=config_value.model.name, messages=messages, - ) + ), + get_ai_metrics_from_response, ) ai_response = completion.choices[0].message.content @@ -86,5 +88,6 @@ def main(): # Continue the conversation by adding user input to the messages list and invoking the LLM again. print("Success.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() From a04e7c16f0f9d414c1523918e09ed783f48729da Mon Sep 17 00:00:00 2001 From: jsonbailey Date: Mon, 20 Apr 2026 12:49:22 -0500 Subject: [PATCH 2/2] chore: use package-root imports for public ldai names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Matches the import style used in the launchdarkly-server-sdk-ai README and tests (e.g. `from ldai import LDAIClient`), rather than reaching into the `ldai.client` submodule. Both forms resolve to the same symbols — `ldai/__init__.py` re-exports them via `__all__` — but the package root is the public API surface. Co-Authored-By: Claude Sonnet 4.6 --- examples/bedrock_example.py | 2 +- examples/chat_judge_example.py | 2 +- examples/chat_observability_example.py | 2 +- examples/direct_judge_example.py | 2 +- examples/gemini_example.py | 2 +- examples/langchain_example.py | 2 +- examples/langgraph_agent_example.py | 2 +- examples/langgraph_multi_agent_example.py | 2 +- examples/openai_example.py | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/bedrock_example.py b/examples/bedrock_example.py index a558398..ad87994 100755 --- a/examples/bedrock_example.py +++ b/examples/bedrock_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient import boto3 client = boto3.client("bedrock-runtime", region_name="us-east-1") diff --git a/examples/chat_judge_example.py b/examples/chat_judge_example.py index 1b91409..57094f8 100644 --- a/examples/chat_judge_example.py +++ b/examples/chat_judge_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient, AICompletionConfigDefault +from ldai import LDAIClient, AICompletionConfigDefault # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/chat_observability_example.py b/examples/chat_observability_example.py index 41cb3df..296ec4d 100644 --- a/examples/chat_observability_example.py +++ b/examples/chat_observability_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient, AICompletionConfigDefault +from ldai import LDAIClient, AICompletionConfigDefault from ldobserve import ObservabilityConfig, ObservabilityPlugin logging.getLogger('ldclient').setLevel(logging.WARNING) diff --git a/examples/direct_judge_example.py b/examples/direct_judge_example.py index b94bc22..7d2faa6 100644 --- a/examples/direct_judge_example.py +++ b/examples/direct_judge_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient, AIJudgeConfigDefault +from ldai import LDAIClient, AIJudgeConfigDefault # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/gemini_example.py b/examples/gemini_example.py index e27bd76..d53735e 100644 --- a/examples/gemini_example.py +++ b/examples/gemini_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient, LDMessage +from ldai import LDAIClient, LDMessage from ldai.tracker import TokenUsage from google import genai from google.genai import types diff --git a/examples/langchain_example.py b/examples/langchain_example.py index eb9430b..60e604f 100644 --- a/examples/langchain_example.py +++ b/examples/langchain_example.py @@ -3,7 +3,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model diff --git a/examples/langgraph_agent_example.py b/examples/langgraph_agent_example.py index 6df7e40..37f5212 100644 --- a/examples/langgraph_agent_example.py +++ b/examples/langgraph_agent_example.py @@ -3,7 +3,7 @@ from pprint import pprint from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai.tracker import TokenUsage from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model diff --git a/examples/langgraph_multi_agent_example.py b/examples/langgraph_multi_agent_example.py index 0a53f26..424b30f 100644 --- a/examples/langgraph_multi_agent_example.py +++ b/examples/langgraph_multi_agent_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai.tracker import TokenUsage from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model diff --git a/examples/openai_example.py b/examples/openai_example.py index 995de39..168d8e8 100755 --- a/examples/openai_example.py +++ b/examples/openai_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai_openai import get_ai_metrics_from_response from openai import OpenAI