1+ from contextlib import contextmanager
12from functools import wraps
2- from typing import Any , Callable , List , Optional
3+ from typing import Any , AsyncIterator , Callable , Iterator , List , Optional , Tuple
34
45import sentry_sdk
56from sentry_sdk .ai .utils import (
1920 has_span_streaming_enabled ,
2021 should_truncate_gen_ai_input ,
2122)
22- from sentry_sdk .utils import has_data_collection_enabled , safe_serialize
23+ from sentry_sdk .utils import ContextVar , has_data_collection_enabled , safe_serialize
2324
2425try :
2526 from langgraph .errors import GraphBubbleUp
2829except ImportError :
2930 raise DidNotEnable ("langgraph not installed" )
3031
32+ # The graph whose invoke() or ainvoke() is running. LangGraph runs invoke() through
33+ # stream() and ainvoke() through astream(), so the stream wrappers skip that inner
34+ # call instead of creating a second span.
35+ _invoking_graph = ContextVar ("sentry_langgraph_invoking_graph" , default = None )
36+
3137
3238class LanggraphIntegration (Integration ):
3339 identifier = "langgraph"
@@ -42,15 +48,17 @@ def setup_once() -> None:
4248 # LangGraph lets users create agents using a StateGraph or the Functional API.
4349 # StateGraphs are then compiled to a CompiledStateGraph. Both CompiledStateGraph and
4450 # the functional API execute on a Pregel instance. Pregel is the runtime for the graph
45- # and the invocation happens on Pregel, so patching the invoke methods takes care of both.
46- # The streaming methods are not patched, because due to some internal reasons, LangGraph
47- # will automatically patch the streaming methods to run through invoke, and by doing this
48- # we prevent duplicate spans for invocations.
51+ # and the invocation happens on Pregel, so patching its invoke and stream methods
52+ # takes care of both.
4953 StateGraph .compile = _wrap_state_graph_compile (StateGraph .compile )
5054 if hasattr (Pregel , "invoke" ):
5155 Pregel .invoke = _wrap_pregel_invoke (Pregel .invoke )
5256 if hasattr (Pregel , "ainvoke" ):
5357 Pregel .ainvoke = _wrap_pregel_ainvoke (Pregel .ainvoke )
58+ if hasattr (Pregel , "stream" ):
59+ Pregel .stream = _wrap_pregel_stream (Pregel .stream )
60+ if hasattr (Pregel , "astream" ):
61+ Pregel .astream = _wrap_pregel_astream (Pregel .astream )
5462
5563
5664def _should_record_inputs (integration : "LanggraphIntegration" ) -> bool :
@@ -184,204 +192,169 @@ def new_compile(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
184192 return new_compile
185193
186194
187- def _wrap_pregel_invoke (f : "Callable[..., Any]" ) -> "Callable[..., Any]" :
188- @wraps (f )
189- def new_invoke (self : "Any" , * args : "Any" , ** kwargs : "Any" ) -> "Any" :
190- client = sentry_sdk .get_client ()
191- integration = client .get_integration (LanggraphIntegration )
192- if integration is None :
193- return f (self , * args , ** kwargs )
195+ @contextmanager
196+ def _invoke_agent_span (
197+ graph : "Any" , args : "Any" , integration : "LanggraphIntegration"
198+ ) -> "Iterator[Tuple[Any, Optional[List[Any]]]]" :
199+ """Start an invoke_agent span for one graph run and set its request data."""
200+ client = sentry_sdk .get_client ()
201+ graph_name = _get_graph_name (graph )
202+ span_name = f"invoke_agent { graph_name } " .strip () if graph_name else "invoke_agent"
194203
195- graph_name = _get_graph_name (self )
196- span_name = (
197- f"invoke_agent { graph_name } " .strip () if graph_name else "invoke_agent"
204+ if has_span_streaming_enabled (client .options ):
205+ span_context : "Any" = sentry_sdk .traces .start_span (
206+ name = span_name ,
207+ attributes = {
208+ "sentry.op" : OP .GEN_AI_INVOKE_AGENT ,
209+ "sentry.origin" : LanggraphIntegration .origin ,
210+ },
211+ )
212+ else :
213+ span_context = get_start_span_function ()(
214+ op = OP .GEN_AI_INVOKE_AGENT ,
215+ name = span_name ,
216+ origin = LanggraphIntegration .origin ,
198217 )
199218
200- if has_span_streaming_enabled (client .options ):
201- with sentry_sdk .traces .start_span (
202- name = span_name ,
203- attributes = {
204- "sentry.op" : OP .GEN_AI_INVOKE_AGENT ,
205- "sentry.origin" : LanggraphIntegration .origin ,
206- SPANDATA .GEN_AI_OPERATION_NAME : "invoke_agent" ,
207- },
208- ) as span :
209- if graph_name :
210- span .set_attribute (SPANDATA .GEN_AI_PIPELINE_NAME , graph_name )
211- span .set_attribute (SPANDATA .GEN_AI_AGENT_NAME , graph_name )
212-
213- # Store input messages to later compare with output
214- input_messages = None
215- if len (args ) > 0 :
216- input_messages = _parse_langgraph_messages (args [0 ])
217- if input_messages and _should_record_inputs (integration ):
218- normalized_input_messages = normalize_message_roles (
219- input_messages
220- )
221-
222- scope = sentry_sdk .get_current_scope ()
223- messages_data = (
224- truncate_and_annotate_messages (
225- normalized_input_messages , span , scope
226- )
227- if should_truncate_gen_ai_input (client .options )
228- else normalized_input_messages
229- )
230- if messages_data is not None :
231- set_data_normalized (
232- span ,
233- SPANDATA .GEN_AI_REQUEST_MESSAGES ,
234- messages_data ,
235- unpack = False ,
236- )
219+ with span_context as span :
220+ set_on_span = (
221+ span .set_attribute if isinstance (span , StreamedSpan ) else span .set_data
222+ )
223+ set_on_span (SPANDATA .GEN_AI_OPERATION_NAME , "invoke_agent" )
224+ if graph_name :
225+ set_on_span (SPANDATA .GEN_AI_PIPELINE_NAME , graph_name )
226+ set_on_span (SPANDATA .GEN_AI_AGENT_NAME , graph_name )
227+
228+ # Store input messages to later compare with output
229+ input_messages = None
230+ if len (args ) > 0 :
231+ input_messages = _parse_langgraph_messages (args [0 ])
232+ if input_messages and _should_record_inputs (integration ):
233+ normalized_input_messages = normalize_message_roles (input_messages )
234+
235+ scope = sentry_sdk .get_current_scope ()
236+ messages_data = (
237+ truncate_and_annotate_messages (
238+ normalized_input_messages , span , scope
239+ )
240+ if should_truncate_gen_ai_input (client .options )
241+ else normalized_input_messages
242+ )
243+ if messages_data is not None :
244+ set_data_normalized (
245+ span ,
246+ SPANDATA .GEN_AI_REQUEST_MESSAGES ,
247+ messages_data ,
248+ unpack = False ,
249+ )
237250
238- result = f ( self , * args , ** kwargs )
251+ yield span , input_messages
239252
240- _set_response_attributes (span , input_messages , result , integration )
241253
242- return result
243- else :
244- with get_start_span_function ()(
245- op = OP .GEN_AI_INVOKE_AGENT ,
246- name = span_name ,
247- origin = LanggraphIntegration .origin ,
248- ) as span :
249- if graph_name :
250- span .set_data (SPANDATA .GEN_AI_PIPELINE_NAME , graph_name )
251- span .set_data (SPANDATA .GEN_AI_AGENT_NAME , graph_name )
252-
253- span .set_data (SPANDATA .GEN_AI_OPERATION_NAME , "invoke_agent" )
254-
255- # Store input messages to later compare with output
256- input_messages = None
257- if len (args ) > 0 :
258- input_messages = _parse_langgraph_messages (args [0 ])
259- if input_messages and _should_record_inputs (integration ):
260- normalized_input_messages = normalize_message_roles (
261- input_messages
262- )
263-
264- scope = sentry_sdk .get_current_scope ()
265- messages_data = (
266- truncate_and_annotate_messages (
267- normalized_input_messages , span , scope
268- )
269- if should_truncate_gen_ai_input (client .options )
270- else normalized_input_messages
271- )
272- if messages_data is not None :
273- set_data_normalized (
274- span ,
275- SPANDATA .GEN_AI_REQUEST_MESSAGES ,
276- messages_data ,
277- unpack = False ,
278- )
254+ def _wrap_pregel_invoke (f : "Callable[..., Any]" ) -> "Callable[..., Any]" :
255+ @wraps (f )
256+ def new_invoke (self : "Any" , * args : "Any" , ** kwargs : "Any" ) -> "Any" :
257+ integration = sentry_sdk .get_client ().get_integration (LanggraphIntegration )
258+ if integration is None :
259+ return f (self , * args , ** kwargs )
279260
261+ with _invoke_agent_span (self , args , integration ) as (span , input_messages ):
262+ token = _invoking_graph .set (self )
263+ try :
280264 result = f (self , * args , ** kwargs )
265+ finally :
266+ _invoking_graph .reset (token )
281267
282- _set_response_attributes (span , input_messages , result , integration )
268+ _set_response_attributes (span , input_messages , result , integration )
283269
284- return result
270+ return result
285271
286272 return new_invoke
287273
288274
289275def _wrap_pregel_ainvoke (f : "Callable[..., Any]" ) -> "Callable[..., Any]" :
290276 @wraps (f )
291277 async def new_ainvoke (self : "Any" , * args : "Any" , ** kwargs : "Any" ) -> "Any" :
292- client = sentry_sdk .get_client ()
293- integration = client .get_integration (LanggraphIntegration )
278+ integration = sentry_sdk .get_client ().get_integration (LanggraphIntegration )
294279 if integration is None :
295280 return await f (self , * args , ** kwargs )
296281
297- graph_name = _get_graph_name (self )
298- span_name = (
299- f"invoke_agent { graph_name } " .strip () if graph_name else "invoke_agent"
300- )
282+ with _invoke_agent_span (self , args , integration ) as (span , input_messages ):
283+ token = _invoking_graph .set (self )
284+ try :
285+ result = await f (self , * args , ** kwargs )
286+ finally :
287+ _invoking_graph .reset (token )
301288
302- if has_span_streaming_enabled (client .options ):
303- with sentry_sdk .traces .start_span (
304- name = span_name ,
305- attributes = {
306- "sentry.op" : OP .GEN_AI_INVOKE_AGENT ,
307- "sentry.origin" : LanggraphIntegration .origin ,
308- SPANDATA .GEN_AI_OPERATION_NAME : "invoke_agent" ,
309- },
310- ) as span :
311- if graph_name :
312- span .set_attribute (SPANDATA .GEN_AI_PIPELINE_NAME , graph_name )
313- span .set_attribute (SPANDATA .GEN_AI_AGENT_NAME , graph_name )
314-
315- input_messages = None
316- if len (args ) > 0 :
317- input_messages = _parse_langgraph_messages (args [0 ])
318- if input_messages and _should_record_inputs (integration ):
319- normalized_input_messages = normalize_message_roles (
320- input_messages
321- )
322-
323- scope = sentry_sdk .get_current_scope ()
324- messages_data = (
325- truncate_and_annotate_messages (
326- normalized_input_messages , span , scope
327- )
328- if should_truncate_gen_ai_input (client .options )
329- else normalized_input_messages
330- )
331- if messages_data is not None :
332- set_data_normalized (
333- span ,
334- SPANDATA .GEN_AI_REQUEST_MESSAGES ,
335- messages_data ,
336- unpack = False ,
337- )
289+ _set_response_attributes (span , input_messages , result , integration )
338290
339- result = await f ( self , * args , ** kwargs )
291+ return result
340292
341- _set_response_attributes ( span , input_messages , result , integration )
293+ return new_ainvoke
342294
343- return result
344295
345- with get_start_span_function ()(
346- op = OP .GEN_AI_INVOKE_AGENT ,
347- name = span_name ,
348- origin = LanggraphIntegration .origin ,
349- ) as span :
350- if graph_name :
351- span .set_data (SPANDATA .GEN_AI_PIPELINE_NAME , graph_name )
352- span .set_data (SPANDATA .GEN_AI_AGENT_NAME , graph_name )
353-
354- span .set_data (SPANDATA .GEN_AI_OPERATION_NAME , "invoke_agent" )
355-
356- input_messages = None
357- if len (args ) > 0 :
358- input_messages = _parse_langgraph_messages (args [0 ])
359- if input_messages and _should_record_inputs (integration ):
360- normalized_input_messages = normalize_message_roles (input_messages )
361-
362- scope = sentry_sdk .get_current_scope ()
363- messages_data = (
364- truncate_and_annotate_messages (
365- normalized_input_messages , span , scope
366- )
367- if should_truncate_gen_ai_input (client .options )
368- else normalized_input_messages
369- )
370- if messages_data is not None :
371- set_data_normalized (
372- span ,
373- SPANDATA .GEN_AI_REQUEST_MESSAGES ,
374- messages_data ,
375- unpack = False ,
376- )
296+ def _wrap_pregel_stream (f : "Callable[..., Any]" ) -> "Callable[..., Any]" :
297+ @wraps (f )
298+ def new_stream (self : "Any" , * args : "Any" , ** kwargs : "Any" ) -> "Any" :
299+ integration = sentry_sdk .get_client ().get_integration (LanggraphIntegration )
300+ # Pregel.invoke runs through Pregel.stream, and the invoke wrapper already
301+ # created the span for this run.
302+ if integration is None or _invoking_graph .get () is self :
303+ return f (self , * args , ** kwargs )
377304
378- result = await f (self , * args , ** kwargs )
305+ def stream_with_span () -> "Iterator[Any]" :
306+ with _invoke_agent_span (self , args , integration ) as (
307+ span ,
308+ input_messages ,
309+ ):
310+ stream = f (self , * args , ** kwargs )
311+ last_chunk = None
312+ try :
313+ for chunk in stream :
314+ last_chunk = chunk
315+ yield chunk
316+ except GeneratorExit :
317+ # The caller stopped iterating early, which isn't an error.
318+ stream .close ()
319+ return
379320
380- _set_response_attributes (span , input_messages , result , integration )
321+ _set_response_attributes (span , input_messages , last_chunk , integration )
381322
382- return result
323+ return stream_with_span ()
383324
384- return new_ainvoke
325+ return new_stream
326+
327+
328+ def _wrap_pregel_astream (f : "Callable[..., Any]" ) -> "Callable[..., Any]" :
329+ @wraps (f )
330+ def new_astream (self : "Any" , * args : "Any" , ** kwargs : "Any" ) -> "Any" :
331+ integration = sentry_sdk .get_client ().get_integration (LanggraphIntegration )
332+ # Pregel.ainvoke runs through Pregel.astream, and the ainvoke wrapper already
333+ # created the span for this run.
334+ if integration is None or _invoking_graph .get () is self :
335+ return f (self , * args , ** kwargs )
336+
337+ async def astream_with_span () -> "AsyncIterator[Any]" :
338+ with _invoke_agent_span (self , args , integration ) as (
339+ span ,
340+ input_messages ,
341+ ):
342+ stream = f (self , * args , ** kwargs )
343+ last_chunk = None
344+ try :
345+ async for chunk in stream :
346+ last_chunk = chunk
347+ yield chunk
348+ except GeneratorExit :
349+ # The caller stopped iterating early, which isn't an error.
350+ await stream .aclose ()
351+ return
352+
353+ _set_response_attributes (span , input_messages , last_chunk , integration )
354+
355+ return astream_with_span ()
356+
357+ return new_astream
385358
386359
387360def _get_new_messages (
0 commit comments