Skip to main content

LangGraph

Opik provides a seamless integration with LangGraph, allowing you to easily log and trace your LangGraph-based applications. By using the OpikTracer callback, you can automatically capture detailed information about your LangGraph graph executions during both development and production.

You can check out the Colab Notebook if you'd like to jump straight to the code: Open In Colab

Getting Started

To use the OpikTracer with LangGraph, you'll need to have both the opik and langgraph packages installed. You can install them using pip:

pip install opik langgraph

In addition, you can configure Opik using the opik configure command which will prompt you for the correct local server address or if you are using the Cloud platfrom your API key:

opik configure

Using the OpikTracer

You can use the OpikTracer callback with any LangGraph graph by passing it in as an argument to the stream or invoke functions:

from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer()

# create your LangGraph graph
graph = ...

# Pass the OpikTracer callback to the graph.stream function
for s in graph.stream({"messages": [HumanMessage(content = QUESTION)]},
config={"callbacks": [opik_tracer]}):
print(s)

# Pass the OpikTracer callback to the graph.invoke function
result = graph.invoke({"messages": [HumanMessage(content = QUESTION)]},
config={"callbacks": [opik_tracer]})

Once the OpikTracer is configured, you will start to see the traces in the Opik UI:

langgraph

Updating logged traces

You can use the OpikTracer.created_traces method to access the trace IDs collected by the OpikTracer callback:

from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer()

# Calling LangGraph stream or invoke functions

traces = opik_tracer.created_traces()
print([trace.id for trace in traces])

These can then be used with the Opik.log_traces_feedback_scores method to update the logged traces.

Advanced usage

The OpikTracer object has a flush method that can be used to make sure that all traces are logged to the Opik platform before you exit a script. This method will return once all traces have been logged or if the timeout is reach, whichever comes first.

from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer()
opik_tracer.flush()