LangInsight callback handler for LangChain - Monitor and trace your LangChain LLM applications
Project description
langinsight-langchain
LangInsight callback handler for LangChain. Sends input/output traces to LangInsight in parallel on each LLM end and supports feedback (thumbs up/down) via score(). TypeScript の @langinsight/langchain と同じ仕様です。
Installation
pip install langinsight-langchain
Or with Poetry:
poetry add langinsight-langchain
Usage
Basic usage
from langchain_anthropic import ChatAnthropic
from langinsight_langchain import CallbackHandler
import os
model = ChatAnthropic(model="claude-3-7-sonnet-20250219")
handler = CallbackHandler(
api_key=os.environ["LANGINSIGHT_API_KEY"],
endpoint=os.environ["LANGINSIGHT_ENDPOINT"],
metadata={"userId": "admin", "sessionId": "admin"},
on_success=lambda input_trace, output_trace: print("Trace sent:", output_trace.get("id")),
on_failure=lambda err: print("Trace failed:", err),
)
response = model.invoke("hi!", config={"callbacks": [handler]})
Feedback (score)
Trace ID は handler.result.result() で取得し、その output_trace["id"] を score() に渡してフィードバックを送信する。DB 永続化などは on_success コールバックでも行える。
from langinsight_langchain import CallbackHandler
handler = CallbackHandler(
api_key=os.environ["LANGINSIGHT_API_KEY"],
endpoint=os.environ["LANGINSIGHT_ENDPOINT"],
metadata={"userId": "admin", "sessionId": "admin"},
)
response = model.invoke("hi!", config={"callbacks": [handler]})
input_trace, output_trace = handler.result.result(timeout=10)
handler.score(trace_id=output_trace["id"], value=1) # 👍
handler.score(trace_id=output_trace["id"], value=-1) # 👎
Options
| Option | Type | Required | Description |
|---|---|---|---|
api_key |
str |
Yes | LangInsight API key |
endpoint |
str |
Yes | LangInsight API endpoint (e.g. https://api.langinsight.example.com) |
metadata |
dict |
Yes | Metadata attached to traces |
metadata.userId |
str |
Yes | User ID |
metadata.sessionId |
str |
Yes | Session ID (e.g. conversation id) |
metadata.modelName |
str |
No | Model name (defaults to run metadata when not set) |
metadata.* |
any |
No | Additional key-value pairs |
on_success |
(input_trace, output_trace) -> None |
No | Callback when trace submission succeeds. Use output_trace["id"] as traceId. |
on_failure |
(error: Exception) -> None |
No | Callback when trace submission fails |
API
score(trace_id, value)— Submits feedback (1 = thumbs up, -1 = thumbs down) for the given trace. Use(handler.result.result())[1]["id"]astrace_id.result— Aconcurrent.futures.Futurethat resolves to(input_trace, output_trace)when both traces are sent. Usehandler.result.result(timeout=10)to block and get the traces.
Development
poetry install
poetry run python examples/basic_usage.py
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langinsight_langchain-0.0.2.tar.gz.
File metadata
- Download URL: langinsight_langchain-0.0.2.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.14.3 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59092f33ef8d0f7e279675e0548e72f9c3cb27a007a655c06930e7332a4884a1
|
|
| MD5 |
f452dcb0ae0796147eaa9be369b19bc9
|
|
| BLAKE2b-256 |
ca005b8fa51960ff95ccb8acfa037128aee9e66b643028e72f1eaa2a12245a48
|
File details
Details for the file langinsight_langchain-0.0.2-py3-none-any.whl.
File metadata
- Download URL: langinsight_langchain-0.0.2-py3-none-any.whl
- Upload date:
- Size: 29.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.14.3 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd8b727301c69e29f9100a3956620ecdcd4948d84bbf83b3d4a4337881844c74
|
|
| MD5 |
cd51e68b296f53b9b1bd144ed2986e35
|
|
| BLAKE2b-256 |
581aba1c67e5c9ebe84058391c4dcbdddc3aeccad74197009d34c27b86fdaf19
|