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.
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 langchain
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 platform 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 typing import List, Annotated
from pydantic import BaseModel
from opik.integrations.langchain import OpikTracer
from langchain_core.messages import HumanMessage
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
# create your LangGraph graph
class State(BaseModel):
messages: Annotated[list, add_messages]
def chatbot(state):
# Typically your LLM calls would be done here
return {"messages": "Hello, how can I help you today?"}
graph = StateGraph(State)
graph.add_node("chatbot", chatbot)
graph.add_edge(START, "chatbot")
graph.add_edge("chatbot", END)
app = graph.compile()
# Create the OpikTracer
opik_tracer = OpikTracer(graph=app.get_graph(xray=True))
# Pass the OpikTracer callback to the Graph.stream function
for s in app.stream({"messages": [HumanMessage(content = "How to use LangGraph ?")]},
config={"callbacks": [opik_tracer]}):
print(s)
# Pass the OpikTracer callback to the Graph.invoke function
result = app.invoke({"messages": [HumanMessage(content = "How to use LangGraph ?")]},
config={"callbacks": [opik_tracer]})
Once the OpikTracer is configured, you will start to see the traces in the Opik UI:
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()