LangSmith Python Client SDK
The `langchainplus-sdk` is the official Python client library designed to connect to the LangSmith LLM Tracing and Evaluation Platform. LangSmith is a unified developer platform that helps teams debug, evaluate, and monitor language models and intelligent agents. This SDK facilitates logging traces, creating datasets, and evaluating runs, offering seamless integration with the LangChain framework while also supporting standalone use with other LLM applications. The current version is 0.0.20, with updates to the underlying LangSmith platform being more frequent.
Warnings
- gotcha The PyPI package `langchainplus-sdk` is the Python client for the LangSmith platform. Users might be confused by the different naming conventions (`langchainplus` vs. `langsmith`) and might search for `langsmith-sdk` on PyPI, which does not exist under that exact name. The active development and more frequent releases are seen in the `langchain-ai/langsmith-sdk` GitHub repository, indicating `langchainplus-sdk` on PyPI might be a less frequently updated wrapper or a specific version of that client.
- breaking The `RunTree` API, used for logging traces outside of the main LangChain framework, is noted as 'experimental' and subject to future changes. This means code relying heavily on this specific API might require adjustments in newer versions.
- gotcha Tracing and platform connection heavily rely on environment variables (e.g., `LANGCHAIN_TRACING_V2`, `LANGCHAIN_ENDPOINT`, `LANGCHAIN_API_KEY`). Incorrectly set or missing environment variables are a common source of connection or tracing failures.
- gotcha There have been reports of `LangchainCallbackHandler` (which the SDK utilizes for tracing LangChain operations) losing cache token metrics and inflating input token costs in non-streaming paths for certain models (e.g., Anthropic, OpenAI). This can lead to inaccurate cost estimations in LangSmith.
Install
-
pip install langchainplus-sdk
Imports
- LangChainPlusClient
from langchainplus_sdk import LangChainPlusClient
Quickstart
import os
from langchainplus_sdk import LangChainPlusClient
# Set up LangSmith environment variables
# Replace with your actual API key and optionally project name
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_ENDPOINT"] = os.environ.get("LANGCHAIN_ENDPOINT", "https://api.langchain.plus")
os.environ["LANGCHAIN_API_KEY"] = os.environ.get("LANGCHAIN_API_KEY", "YOUR_LANGCHAINPLUS_API_KEY")
# os.environ["LANGCHAIN_PROJECT"] = os.environ.get("LANGCHAIN_PROJECT", "My Default Project")
# Initialize the client (optional, tracing often works via environment variables directly)
client = LangChainPlusClient()
# Example: Create a simple dataset entry
dataset_name = "My Example Dataset"
description = "A dataset for demonstrating langchainplus-sdk usage."
# In a real application, you would log traces from your LLM calls
# or manually create runs/examples. For this quickstart, we'll simulate a simple action.
try:
# This part would typically be driven by actual LLM runs being traced
# For a direct client interaction, you can create datasets and examples.
# Check if dataset exists, if not, create it
existing_datasets = client.list_datasets(name=dataset_name)
if not list(existing_datasets):
dataset = client.create_dataset(name=dataset_name, description=description)
print(f"Created dataset: {dataset.name} (ID: {dataset.id})")
else:
dataset = list(existing_datasets)[0]
print(f"Using existing dataset: {dataset.name} (ID: {dataset.id})")
# Example of creating a simple example within the dataset
example_name = "Initial Example"
example_inputs = {"question": "What is the capital of France?"}
example_outputs = {"answer": "Paris"}
# Check if example exists to avoid duplicates in quickstart re-runs
existing_examples = client.list_examples(dataset_id=dataset.id)
example_found = False
for ex in existing_examples:
if ex.inputs == example_inputs and ex.outputs == example_outputs:
example_found = True
break
if not example_found:
example = client.create_example(
dataset_id=dataset.id,
inputs=example_inputs,
outputs=example_outputs,
name=example_name
)
print(f"Created example: {example.name} (ID: {example.id})")
else:
print(f"Example '{example_name}' already exists in dataset.")
print("LangSmith client setup and basic interaction successful.")
print("Check your LangSmith UI for traces and datasets.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your LANGCHAIN_API_KEY is correct and LangSmith service is accessible.")