# braintrust-langchain
SDK for integrating [Braintrust](https://braintrust.dev) with [LangChain](https://langchain.com/). This package provides a callback handler to automatically log LangChain executions to Braintrust.
## Installation
```bash
pip install braintrust-langchain
```
## Requirements
- Python >= 3.9
- LangChain >= 0.1.0
## Usage
First, make sure you have your Braintrust API key set in your environment:
```bash
export BRAINTRUST_API_KEY="your-api-key"
```
### Basic Usage
```python
import asyncio
from braintrust import init_logger
from braintrust_langchain import BraintrustCallbackHandler, set_global_handler
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
async def main():
# Initialize the logger with your project
init_logger(project="your-project-name")
# Create the callback handler and set it globally for all LangChain components
handler = BraintrustCallbackHandler()
set_global_handler(handler)
# Initialize your LangChain components
prompt = ChatPromptTemplate.from_template("What is 1 + {number}?")
model = ChatOpenAI()
# Create a simple chain
chain = prompt | model
# Use LangChain as normal - all calls will be logged to Braintrust
response = await chain.ainvoke({"number": "2"})
if __name__ == "__main__":
asyncio.run(main())
```
If you'd prefer to pass the callback handler to specific LangChain calls instead of setting it globally, you can do so using the `callbacks` config option:
```python
async def main():
handler = BraintrustCallbackHandler()
# Pass the handler to specific calls
response = await chain.ainvoke(
{"number": "2"},
config={"callbacks": [handler]}
)
# Or initialize components with the handler
model = ChatOpenAI(callbacks=[handler])
```
### Supported Features
The callback handler supports logging for:
- LLM calls (including streaming)
- Chat model interactions
- Chain executions
- Tool/Agent usage
- Memory operations
- State management (LangGraph)
Review the [LangChain documentation](https://python.langchain.com/docs/modules/callbacks/) for more information on how to use callbacks.
## Development
Contributions are welcomed!
```bash
git clone https://github.com/braintrustdata/sdk.git
cd sdk/integrations/langchain-py
pip install -e ".[dev]"
# work on the code
pytest
```
Raw data
{
"_id": null,
"home_page": null,
"name": "braintrust-langchain",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "braintrust, langchain, llm, tracing, ai, agents",
"author": null,
"author_email": "Braintrust <info@braintrust.dev>",
"download_url": "https://files.pythonhosted.org/packages/88/84/e3a68feb963bc50d0ea208a1083b68d14bde0ba722c237c3a03349ee7c6c/braintrust_langchain-0.1.2.tar.gz",
"platform": null,
"description": "# braintrust-langchain\n\nSDK for integrating [Braintrust](https://braintrust.dev) with [LangChain](https://langchain.com/). This package provides a callback handler to automatically log LangChain executions to Braintrust.\n\n## Installation\n\n```bash\npip install braintrust-langchain\n```\n\n## Requirements\n\n- Python >= 3.9\n- LangChain >= 0.1.0\n\n## Usage\n\nFirst, make sure you have your Braintrust API key set in your environment:\n\n```bash\nexport BRAINTRUST_API_KEY=\"your-api-key\"\n```\n\n### Basic Usage\n\n```python\nimport asyncio\nfrom braintrust import init_logger\nfrom braintrust_langchain import BraintrustCallbackHandler, set_global_handler\nfrom langchain_openai import ChatOpenAI\nfrom langchain_core.prompts import ChatPromptTemplate\n\n\nasync def main():\n # Initialize the logger with your project\n init_logger(project=\"your-project-name\")\n\n # Create the callback handler and set it globally for all LangChain components\n handler = BraintrustCallbackHandler()\n set_global_handler(handler)\n\n # Initialize your LangChain components\n prompt = ChatPromptTemplate.from_template(\"What is 1 + {number}?\")\n model = ChatOpenAI()\n\n # Create a simple chain\n chain = prompt | model\n\n # Use LangChain as normal - all calls will be logged to Braintrust\n response = await chain.ainvoke({\"number\": \"2\"})\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\nIf you'd prefer to pass the callback handler to specific LangChain calls instead of setting it globally, you can do so using the `callbacks` config option:\n\n```python\nasync def main():\n handler = BraintrustCallbackHandler()\n\n # Pass the handler to specific calls\n response = await chain.ainvoke(\n {\"number\": \"2\"},\n config={\"callbacks\": [handler]}\n )\n\n # Or initialize components with the handler\n model = ChatOpenAI(callbacks=[handler])\n```\n\n### Supported Features\n\nThe callback handler supports logging for:\n\n- LLM calls (including streaming)\n- Chat model interactions\n- Chain executions\n- Tool/Agent usage\n- Memory operations\n- State management (LangGraph)\n\nReview the [LangChain documentation](https://python.langchain.com/docs/modules/callbacks/) for more information on how to use callbacks.\n\n## Development\n\nContributions are welcomed!\n\n```bash\ngit clone https://github.com/braintrustdata/sdk.git\n\ncd sdk/integrations/langchain-py\n\npip install -e \".[dev]\"\n\n# work on the code\n\npytest\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Integration for LangChain and Braintrust Tracing",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://www.braintrust.dev",
"Repository": "https://github.com/braintrustdata/braintrust-sdk"
},
"split_keywords": [
"braintrust",
" langchain",
" llm",
" tracing",
" ai",
" agents"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "3d4645c466e16f44be09cdac41405e736de15b4e37391285eb24c87ce7955a22",
"md5": "e83f94f730789cc0130b54579ae2275a",
"sha256": "cbdfe61da789a14b05c30900242cbb099ac2b946f452ada093d05937427e4e09"
},
"downloads": -1,
"filename": "braintrust_langchain-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e83f94f730789cc0130b54579ae2275a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 17067,
"upload_time": "2025-11-03T05:14:36",
"upload_time_iso_8601": "2025-11-03T05:14:36.259217Z",
"url": "https://files.pythonhosted.org/packages/3d/46/45c466e16f44be09cdac41405e736de15b4e37391285eb24c87ce7955a22/braintrust_langchain-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "8884e3a68feb963bc50d0ea208a1083b68d14bde0ba722c237c3a03349ee7c6c",
"md5": "1abdeef9bf335106e7b629a09dcc2e1d",
"sha256": "9ed36c9c615f14ad0df1f0845581a4f67ebd056ab1c010bf3c756cc81a0d98f0"
},
"downloads": -1,
"filename": "braintrust_langchain-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "1abdeef9bf335106e7b629a09dcc2e1d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 15099,
"upload_time": "2025-11-03T05:14:38",
"upload_time_iso_8601": "2025-11-03T05:14:38.587053Z",
"url": "https://files.pythonhosted.org/packages/88/84/e3a68feb963bc50d0ea208a1083b68d14bde0ba722c237c3a03349ee7c6c/braintrust_langchain-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-11-03 05:14:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "braintrustdata",
"github_project": "braintrust-sdk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "braintrust-langchain"
}