Skip to main content

LangChain callback handlers for the Grafana Agent Observability Python SDK

Project description

Sigil Python Framework Module: LangChain

agento11y-langchain provides callback handlers that map LangChain lifecycle events into Sigil generation recorder lifecycles.

Installation

pip install agento11y agento11y-langchain
pip install langchain-openai

Usage

from agento11y import Client
from agento11y_langchain import with_agento11y_langchain_callbacks

client = Client()
config = with_agento11y_langchain_callbacks(None, client=client, provider_resolver="auto")

End-to-end example (invoke + stream)

from langchain_openai import ChatOpenAI
from agento11y import Client
from agento11y_langchain import Agento11yLangChainHandler, with_agento11y_langchain_callbacks

client = Client()
handler = Agento11yLangChainHandler(
    client=client,
    provider_resolver="auto",
    agent_name="langchain-example",
    agent_version="1.0.0",
)

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

# Non-stream call -> SYNC generation mode.
result = llm.invoke(
    "Summarize why retry budgets matter.",
    config=with_agento11y_langchain_callbacks(None, client=client, provider_resolver="auto"),
)
print(result.content)

# Stream call -> STREAM generation mode + TTFT tracking.
for chunk in llm.stream(
    "Give me three short reliability tips.",
    config=with_agento11y_langchain_callbacks(None, client=client, provider_resolver="auto"),
):
    if chunk.content:
        print(chunk.content, end="", flush=True)
print()

# Advanced usage: explicit handler wiring remains supported.
_ = llm.invoke("manual handler wiring", config={"callbacks": [handler]})

client.shutdown()

Behavior

  • Lifecycle mapping:
    • on_llm_start / on_chat_model_start -> generation recorder
    • on_tool_start / on_tool_end / on_tool_error -> start_tool_execution
    • on_chain_start / on_chain_end / on_chain_error -> framework chain spans
    • on_retriever_start / on_retriever_end / on_retriever_error -> framework retriever spans
    • on_llm_new_token -> first-token timestamp for stream mode
  • Mode mapping: non-stream -> SYNC, stream -> STREAM.
  • Provider resolver parity:
    • explicit provider metadata when available
    • model-name inference (gpt-/o1/o3/o4 -> openai, claude- -> anthropic, gemini- -> gemini)
    • fallback -> custom
  • Framework tags/metadata are always set:
    • agento11y.framework.name=langchain
    • agento11y.framework.source=handler
    • agento11y.framework.language=python
    • metadata["agento11y.framework.run_id"]=<run id>
    • metadata["agento11y.framework.thread_id"]=<thread id> (when present in callback metadata/config)
    • metadata["agento11y.framework.parent_run_id"] (when available)
    • metadata["agento11y.framework.component_name"] (serialized component identity)
    • metadata["agento11y.framework.run_type"] (llm, chat, tool, chain, retriever)
    • metadata["agento11y.framework.tags"] (normalized callback tags)
    • metadata["agento11y.framework.retry_attempt"] (when available)
    • generation span attributes mirror low-cardinality framework metadata keys

Call client.shutdown() during teardown to flush buffered telemetry.

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

agento11y_langchain-0.10.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

agento11y_langchain-0.10.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file agento11y_langchain-0.10.0.tar.gz.

File metadata

  • Download URL: agento11y_langchain-0.10.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for agento11y_langchain-0.10.0.tar.gz
Algorithm Hash digest
SHA256 0a90dec515da596c05eaf8cf37c1ad7931a1b515c26e7bf207fc03a9519008d9
MD5 47c19d29f511e299918091c1d32a4bc9
BLAKE2b-256 ffd0545cd54380a64643aeceef0f14afcadd71bc198044bdf942353865b2a24e

See more details on using hashes here.

Provenance

The following attestation bundles were made for agento11y_langchain-0.10.0.tar.gz:

Publisher: python-sdks-publish.yml on grafana/agento11y

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file agento11y_langchain-0.10.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agento11y_langchain-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a9224f9c24ec4ec59ee252ae3e790c35cbd63bb7ea7b44dfe32f5e583224e54
MD5 001993deaef53610cfc93a8497cdee11
BLAKE2b-256 92ef889ea8301244c30f540992bdf4b3bc1b890dab2fc22e0c4167856320baeb

See more details on using hashes here.

Provenance

The following attestation bundles were made for agento11y_langchain-0.10.0-py3-none-any.whl:

Publisher: python-sdks-publish.yml on grafana/agento11y

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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