diff --git a/apps/opik-documentation/documentation/docs/tracing/integrations/langgraph.md b/apps/opik-documentation/documentation/docs/tracing/integrations/langgraph.md index 70127a9b08..4362d0096f 100644 --- a/apps/opik-documentation/documentation/docs/tracing/integrations/langgraph.md +++ b/apps/opik-documentation/documentation/docs/tracing/integrations/langgraph.md @@ -34,18 +34,19 @@ You can use the [`OpikTracer`](https://www.comet.com/docs/opik/python-sdk-refere ```python from opik.integrations.langchain import OpikTracer -opik_tracer = OpikTracer() - # create your LangGraph graph graph = ... +app = graph.compile(...) + +opik_tracer = OpikTracer(graph=app.get_graph(xray=True)) -# Pass the OpikTracer callback to the graph.stream function -for s in graph.stream({"messages": [HumanMessage(content = QUESTION)]}, +# Pass the OpikTracer callback to the Graph.stream function +for s in app.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)]}, +# Pass the OpikTracer callback to the Graph.invoke function +result = app.invoke({"messages": [HumanMessage(content = QUESTION)]}, config={"callbacks": [opik_tracer]}) ``` diff --git a/sdks/python/src/opik/integrations/langchain/opik_tracer.py b/sdks/python/src/opik/integrations/langchain/opik_tracer.py index 783688f44d..d3945df877 100644 --- a/sdks/python/src/opik/integrations/langchain/opik_tracer.py +++ b/sdks/python/src/opik/integrations/langchain/opik_tracer.py @@ -14,6 +14,7 @@ from uuid import UUID from langchain_core.tracers.schemas import Run + from langchain_core.runnables.graph import Graph LOGGER = logging.getLogger(__name__) @@ -35,6 +36,7 @@ class OpikTracer(BaseTracer): Args: tags: List of tags to be applied to each trace logged by the tracer. metadata: Additional metadata for each trace logged by the tracer. + graph: A LangGraph Graph object to track the Graph Definition in Opik. project_name: The name of the project to log data. """ @@ -42,11 +44,19 @@ def __init__( self, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, + graph: Optional["Graph"] = None, project_name: Optional[str] = None, **kwargs: Any, ) -> None: super().__init__(**kwargs) self._trace_default_metadata = metadata if metadata is not None else {} + + if graph: + self._trace_default_metadata["_opik_graph_definition"] = { + "format": "mermaid", + "data": graph.draw_mermaid(), + } + self._trace_default_tags = tags self._span_data_map: Dict["UUID", span.SpanData] = {}