Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions py/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def _pinned_python_version():
"opentelemetry-exporter-otlp-proto-http",
"google.genai",
"google.adk",
"langsmith",
"temporalio",
)

Expand Down Expand Up @@ -104,6 +105,7 @@ def _pinned_python_version():
DSPY_VERSIONS = (LATEST,)
GOOGLE_ADK_VERSIONS = (LATEST, "1.14.1")
LANGCHAIN_VERSIONS = (LATEST, "0.3.28")
LANGSMITH_VERSIONS = (LATEST, "0.7.12")
OPENROUTER_VERSIONS = (LATEST, "0.6.0")
# temporalio 1.19.0+ requires Python >= 3.10; skip Python 3.9 entirely
TEMPORAL_VERSIONS = (LATEST, "1.20.0", "1.19.0")
Expand Down Expand Up @@ -235,6 +237,17 @@ def test_langchain(session, version):
_run_core_tests(session)


@nox.session()
@nox.parametrize("version", LANGSMITH_VERSIONS, ids=LANGSMITH_VERSIONS)
def test_langsmith(session, version):
"""Test LangSmith integration."""
_install_test_deps(session)
_install(session, "langsmith", version)
_install(session, "langchain-core")
_install(session, "langchain-openai")
_run_tests(session, f"{INTEGRATION_DIR}/langsmith/test_langsmith.py")


@nox.session()
@nox.parametrize("version", OPENAI_VERSIONS, ids=OPENAI_VERSIONS)
def test_openai(session, version):
Expand Down Expand Up @@ -371,9 +384,8 @@ def pylint(session):
session.install("pydantic_ai>=1.10.0")
session.install("google-adk")
session.install("opentelemetry.instrumentation.openai")
# langsmith is needed for the langsmith_wrapper module but not in VENDOR_PACKAGES
# langchain-core, langchain-openai, langchain-anthropic are needed for the langchain integration
session.install("langsmith", "langchain-core", "langchain-openai", "langchain-anthropic")
session.install("langchain-core", "langchain-openai", "langchain-anthropic")

result = session.run("git", "ls-files", "**/*.py", silent=True, log=False)
files = [path for path in result.strip().splitlines() if path not in GENERATED_LINT_EXCLUDES]
Expand Down
5 changes: 5 additions & 0 deletions py/src/braintrust/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DSPyIntegration,
GoogleGenAIIntegration,
LangChainIntegration,
LangSmithIntegration,
LiteLLMIntegration,
OpenRouterIntegration,
PydanticAIIntegration,
Expand Down Expand Up @@ -52,6 +53,7 @@ def auto_instrument(
dspy: bool = True,
adk: bool = True,
langchain: bool = True,
langsmith: bool = True,
) -> dict[str, bool]:
"""
Auto-instrument supported AI/ML libraries for Braintrust tracing.
Expand All @@ -75,6 +77,7 @@ def auto_instrument(
dspy: Enable DSPy instrumentation (default: True)
adk: Enable Google ADK instrumentation (default: True)
langchain: Enable LangChain instrumentation (default: True)
langsmith: Enable LangSmith instrumentation (default: True)

Returns:
Dict mapping integration name to whether it was successfully instrumented.
Expand Down Expand Up @@ -146,6 +149,8 @@ def auto_instrument(
results["adk"] = _instrument_integration(ADKIntegration)
if langchain:
results["langchain"] = _instrument_integration(LangChainIntegration)
if langsmith:
results["langsmith"] = _instrument_integration(LangSmithIntegration)

return results

Expand Down
2 changes: 2 additions & 0 deletions py/src/braintrust/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .dspy import DSPyIntegration
from .google_genai import GoogleGenAIIntegration
from .langchain import LangChainIntegration
from .langsmith import LangSmithIntegration
from .litellm import LiteLLMIntegration
from .openrouter import OpenRouterIntegration
from .pydantic_ai import PydanticAIIntegration
Expand All @@ -21,6 +22,7 @@
"GoogleGenAIIntegration",
"LiteLLMIntegration",
"LangChainIntegration",
"LangSmithIntegration",
"OpenRouterIntegration",
"PydanticAIIntegration",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Test auto_instrument for LangSmith."""

import os
from pathlib import Path

import langsmith.client
import langsmith.evaluation._arunner
import langsmith.evaluation._runner
import langsmith.run_helpers
from braintrust.auto import auto_instrument
from braintrust.wrappers.test_utils import autoinstrument_test_context
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI


_CASSETTES_DIR = Path(__file__).resolve().parent.parent / "langsmith" / "cassettes"


# 1. Verify not patched initially.
assert not getattr(langsmith.run_helpers.traceable, "__braintrust_patched_langsmith_traceable__", False)
assert not getattr(langsmith.evaluation._runner.evaluate, "__braintrust_patched_langsmith_evaluate_sync__", False)
assert not getattr(langsmith.evaluation._arunner.aevaluate, "__braintrust_patched_langsmith_evaluate_async__", False)
assert not getattr(langsmith.client.Client.evaluate, "__braintrust_patched_langsmith_client_evaluate__", False)
assert not getattr(langsmith.client.Client.aevaluate, "__braintrust_patched_langsmith_client_aevaluate__", False)


# 2. Instrument with standalone mode so only Braintrust runs.
os.environ["BRAINTRUST_LANGSMITH_STANDALONE"] = "1"
results = auto_instrument(
openai=False,
anthropic=False,
litellm=False,
pydantic_ai=False,
google_genai=False,
openrouter=False,
agno=False,
agentscope=False,
claude_agent_sdk=False,
dspy=False,
adk=False,
langchain=False,
langsmith=True,
)
assert results.get("langsmith") == True

assert getattr(langsmith.run_helpers.traceable, "__braintrust_patched_langsmith_traceable__", False)
assert getattr(langsmith.evaluation._runner.evaluate, "__braintrust_patched_langsmith_evaluate_sync__", False)
assert getattr(langsmith.evaluation._arunner.aevaluate, "__braintrust_patched_langsmith_evaluate_async__", False)
assert getattr(langsmith.client.Client.evaluate, "__braintrust_patched_langsmith_client_evaluate__", False)
assert getattr(langsmith.client.Client.aevaluate, "__braintrust_patched_langsmith_client_aevaluate__", False)


# 3. Idempotent.
results2 = auto_instrument(
openai=False,
anthropic=False,
litellm=False,
pydantic_ai=False,
google_genai=False,
openrouter=False,
agno=False,
agentscope=False,
claude_agent_sdk=False,
dspy=False,
adk=False,
langchain=False,
langsmith=True,
)
assert results2.get("langsmith") == True


# 4. Make an API call and verify span.
with autoinstrument_test_context("test_auto_langsmith", cassettes_dir=_CASSETTES_DIR) as memory_logger:
prompt = ChatPromptTemplate.from_template("What is 1 + {number}?")
model = ChatOpenAI(
model="gpt-4o-mini",
temperature=1,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
n=1,
)
chain = prompt | model

@langsmith.traceable(name="auto-langsmith")
def run_chain(inputs: dict[str, str]) -> dict[str, str]:
return {"answer": chain.invoke(inputs).content}

result = run_chain({"number": "2"})
assert result == {"answer": "1 + 2 equals 3."}

spans = memory_logger.pop()
assert len(spans) == 1, f"Expected 1 span, got {len(spans)}"
span = spans[0]
assert span["span_attributes"]["name"] == "auto-langsmith"
assert span["output"] == {"answer": "1 + 2 equals 3."}

print("SUCCESS")
38 changes: 38 additions & 0 deletions py/src/braintrust/integrations/langsmith/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Braintrust integration for LangSmith."""

import logging
import os

from braintrust.logger import NOOP_SPAN, current_span, init_logger

from .integration import LangSmithIntegration


logger = logging.getLogger(__name__)

__all__ = [
"LangSmithIntegration",
"setup_langsmith",
]


def setup_langsmith(
api_key: str | None = None,
project_id: str | None = None,
project_name: str | None = None,
standalone: bool = False,
) -> bool:
"""Setup Braintrust integration with LangSmith."""
resolved_project_name = project_name or os.environ.get("LANGCHAIN_PROJECT")
if current_span() == NOOP_SPAN:
init_logger(project=resolved_project_name, api_key=api_key, project_id=project_id)

try:
import langsmith # noqa: F401
except ImportError as exc:
logger.error("Failed to import langsmith: %s", exc)
logger.error("langsmith is not installed. Please install it with: pip install langsmith")
return False

logger.info("LangSmith integration with Braintrust enabled")
return LangSmithIntegration.setup(standalone=True if standalone else None)
Loading
Loading