Skip to main content

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"] as trace_id.
  • result — A concurrent.futures.Future that resolves to (input_trace, output_trace) when both traces are sent. Use handler.result.result(timeout=10) to block and get the traces.

Development

poetry install
poetry run python examples/basic_usage.py

License

MIT

Release files for langinsight-langchain 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langinsight-langchain 0.0.2
File Size Uploaded
langinsight_langchain-0.0.2.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langinsight-langchain 0.0.2
File Interpreter ABI Platform
langinsight_langchain-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 44.3 kB

Release files / langinsight_langchain-0.0.2.tar.gz

Download URL langinsight_langchain-0.0.2.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
59092f33ef8d0f7e279675e0548e72f9c3cb27a007a655c06930e7332a4884a1
BLAKE2b-256 checksum
How to use checksums
ca005b8fa51960ff95ccb8acfa037128aee9e66b643028e72f1eaa2a12245a48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.3 Darwin/24.6.0

Release files / langinsight_langchain-0.0.2-py3-none-any.whl

Download URL langinsight_langchain-0.0.2-py3-none-any.whl
Size 29.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd8b727301c69e29f9100a3956620ecdcd4948d84bbf83b3d4a4337881844c74
BLAKE2b-256 checksum
How to use checksums
581aba1c67e5c9ebe84058391c4dcbdddc3aeccad74197009d34c27b86fdaf19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.3 Darwin/24.6.0

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page