LangSmith Python Client SDK

0.0.20 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `LangChainPlusClient` and interact with the LangSmith platform by creating a dataset and an example. It highlights the use of environment variables for configuration, which is the recommended way for tracing. Replace 'YOUR_LANGCHAINPLUS_API_KEY' with your actual LangSmith API key.

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.")

view raw JSON →