Python · Instrumentation
openinference-instrumentation-langchain
OpenInference LangChain Instrumentation
- Module
openinference.instrumentation.langchain- Version
- 0.1.87
- Python
- >=3.10, <3.15
- Instruments
langchain_core >= 0.3.9
Install
pip install openinference-instrumentation-langchainUsage
Instrument once, at startup, before the library is used. The LangChainInstrumentor reference below lists the options.
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.langchain import LangChainInstrumentor
tracer_provider = TracerProvider()
# ...add a span processor that exports to Phoenix or any OTLP backend.
LangChainInstrumentor().instrument(
tracer_provider=tracer_provider,
config=TraceConfig(hide_inputs=True), # optional
)openinference.instrumentation.langchain
LangChainInstrumentor
classSource ↗class LangChainInstrumentor(BaseInstrumentor)An instrumentor for LangChain
instrument
methoddef instrument(
*,
tracer_provider: Optional[TracerProvider] = None,
config: Optional[TraceConfig] = None,
skip_dep_check: bool = False,
) -> NoneStart tracing langchain_core: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s BaseInstrumentor.
Parameters
| Name | Description |
|---|---|
tracer_provider | The tracer provider the spans are made with. Defaults to the global one, |
config | What the spans record: hide inputs, outputs or messages, or limit image sizes. Defaults to |
skip_dep_check | Instrument even if the installed version of |
uninstrument
methoddef uninstrument() -> NoneStop tracing: undo the patches instrument() made. Inherited from OpenTelemetry’s BaseInstrumentor.
instrumentation_dependencies
methodSource ↗def instrumentation_dependencies() -> Collection[str]get_span
methodSource ↗def get_span(run_id: UUID) -> Optional[Span]get_ancestors
methodSource ↗def get_ancestors(run_id: UUID) -> List[Span]get_current_span
functionSource ↗def get_current_span() -> Optional[Span]get_ancestor_spans
functionSource ↗def get_ancestor_spans() -> List[Span]Retrieve the ancestor spans for the current LangChain run.
This function traverses the LangChain run tree from the current run’s parent up to the root, collecting the spans associated with each ancestor run. The list is ordered from the immediate parent of the current run to the root of the run tree.