# openinference-instrumentation-langchain

**Python · Instrumentation**

OpenInference LangChain Instrumentation

- **Module**: `openinference.instrumentation.langchain`
- **Version**: 0.1.88
- **Python**: >=3.10, <3.15
- **Instruments**: `langchain_core >= 0.3.9`

[PyPI](https://pypi.org/project/openinference-instrumentation-langchain/) · [README](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-langchain)

## Install

```sh
pip install openinference-instrumentation-langchain
```

## Usage

Instrument once, at startup, before the library is used. The [`LangChainInstrumentor`](https://openinf.dev/python/openinference-instrumentation-langchain.html.md#openinference.instrumentation.langchain.LangChainInstrumentor) reference below lists the options.

```python
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`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/src/openinference/instrumentation/langchain/__init__.py#L26-L96)

```python
class LangChainInstrumentor(BaseInstrumentor)
```

An instrumentor for LangChain

#### `instrument`

_method_

```python
def instrument(
    *,
    tracer_provider: Optional[TracerProvider] = None,
    config: Optional[TraceConfig] = None,
    skip_dep_check: bool = False,
) -> None
```

Start tracing `langchain_core`: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

**Parameters**

| Name | Description |
| - | - |
| `tracer_provider` | The tracer provider the spans are made with. Defaults to the global one, `opentelemetry.trace.get_tracer_provider()`. |
| `config` | What the spans record: hide inputs, outputs or messages, or limit image sizes. Defaults to `TraceConfig()`, which reads the `OPENINFERENCE_*` environment variables. See [`TraceConfig`](https://openinf.dev/python/openinference-instrumentation.html.md#openinference.instrumentation.TraceConfig). |
| `skip_dep_check` | Instrument even if the installed version of `langchain_core` is outside the supported range. |

#### `uninstrument`

_method_

```python
def uninstrument() -> None
```

Stop tracing: undo the patches `instrument()` made. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

#### `instrumentation_dependencies`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/src/openinference/instrumentation/langchain/__init__.py#L31-L32)

```python
def instrumentation_dependencies() -> Collection[str]
```

#### `get_span`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/src/openinference/instrumentation/langchain/__init__.py#L67-L71)

```python
def get_span(run_id: UUID) -> Optional[Span]
```

#### `get_ancestors`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/src/openinference/instrumentation/langchain/__init__.py#L73-L96)

```python
def get_ancestors(run_id: UUID) -> List[Span]
```

### `get_current_span`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/src/openinference/instrumentation/langchain/__init__.py#L123-L137)

```python
def get_current_span() -> Optional[Span]
```

### `get_ancestor_spans`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-langchain/src/openinference/instrumentation/langchain/__init__.py#L140-L161)

```python
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.
