Skip to main content

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"] 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langinsight_langchain-0.0.2.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langinsight_langchain-0.0.2-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

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

Hashes for langinsight_langchain-0.0.2.tar.gz
Algorithm Hash digest
SHA256 59092f33ef8d0f7e279675e0548e72f9c3cb27a007a655c06930e7332a4884a1
MD5 f452dcb0ae0796147eaa9be369b19bc9
BLAKE2b-256 ca005b8fa51960ff95ccb8acfa037128aee9e66b643028e72f1eaa2a12245a48

See more details on using hashes here.

File details

Details for the file langinsight_langchain-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for langinsight_langchain-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cd8b727301c69e29f9100a3956620ecdcd4948d84bbf83b3d4a4337881844c74
MD5 cd51e68b296f53b9b1bd144ed2986e35
BLAKE2b-256 581aba1c67e5c9ebe84058391c4dcbdddc3aeccad74197009d34c27b86fdaf19

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page