-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlangchain_callback.py
More file actions
1430 lines (1245 loc) · 49.8 KB
/
langchain_callback.py
File metadata and controls
1430 lines (1245 loc) · 49.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Module with the Openlayer callback handler for LangChain."""
# pylint: disable=unused-argument
import contextvars
import time
from typing import Any, Callable, Dict, List, Optional, Union
from uuid import UUID
try:
try:
from langchain_core import messages as langchain_schema
from langchain_core.callbacks.base import (
AsyncCallbackHandler,
BaseCallbackHandler,
)
except ImportError:
from langchain import schema as langchain_schema
from langchain.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler
HAVE_LANGCHAIN = True
except ImportError:
HAVE_LANGCHAIN = False
from .. import utils
from ..tracing import enums, steps, tracer, traces
LANGCHAIN_TO_OPENLAYER_PROVIDER_MAP = {
"azure-openai-chat": "Azure",
"openai-chat": "OpenAI",
"chat-ollama": "Ollama",
"vertexai": "Google",
"amazon_bedrock_converse_chat": "Bedrock",
}
# LiteLLM model prefixes to provider names.
# When models are accessed via a LiteLLM proxy (e.g. "gemini/gemini-2.5-flash"),
# the LangChain _type is "openai-chat" which incorrectly maps to "OpenAI".
# This map resolves the actual provider from the model prefix.
LITELLM_PREFIX_TO_PROVIDER_MAP = {
"gemini": "Google",
"anthropic": "Anthropic",
"cohere": "Cohere",
"mistral": "Mistral",
"bedrock": "Bedrock",
"vertex_ai": "Google",
"azure": "Azure",
"huggingface": "Hugging Face",
"replicate": "Replicate",
"together_ai": "Together AI",
"groq": "Groq",
"deepseek": "DeepSeek",
"fireworks_ai": "Fireworks AI",
"perplexity": "Perplexity",
"ollama": "Ollama",
"openai": "OpenAI",
}
if HAVE_LANGCHAIN:
BaseCallbackHandlerClass = BaseCallbackHandler
AsyncCallbackHandlerClass = AsyncCallbackHandler
else:
BaseCallbackHandlerClass = object
AsyncCallbackHandlerClass = object
class OpenlayerHandlerMixin:
"""Mixin class containing shared logic for both sync and async Openlayer
handlers."""
def __init__(self, **kwargs: Any) -> None:
if not HAVE_LANGCHAIN:
raise ImportError(
"LangChain library is not installed. Please install it with: pip "
"install langchain"
)
super().__init__()
self.metadata: Dict[str, Any] = kwargs or {}
self.steps: Dict[UUID, steps.Step] = {}
self.root_steps: set[UUID] = set() # Track which steps are root
# Track standalone traces (consistent with async handler)
self._traces_by_root: Dict[UUID, traces.Trace] = {}
# Extract inference_id from kwargs if provided
self._inference_id = kwargs.get("inference_id")
# Extract metadata_transformer from kwargs if provided
self._metadata_transformer = kwargs.get("metadata_transformer")
def _start_step(
self,
run_id: UUID,
parent_run_id: Optional[UUID],
name: str,
step_type: enums.StepType = enums.StepType.CHAT_COMPLETION,
inputs: Optional[Any] = None,
metadata: Optional[Dict[str, Any]] = None,
**step_kwargs: Any,
) -> steps.Step:
"""Start a new step - use parent_run_id for proper nesting."""
if run_id in self.steps:
return self.steps[run_id]
# Create the step with raw inputs and metadata
step = steps.step_factory(
step_type=step_type,
name=name,
inputs=inputs,
metadata={**self.metadata, **(metadata or {})},
)
step.start_time = time.time()
# Set step-specific attributes
for key, value in step_kwargs.items():
if hasattr(step, key):
setattr(step, key, value)
# Use parent_run_id to establish proper parent-child relationships
if parent_run_id is not None and parent_run_id in self.steps:
# This step has a parent - add it as a nested step
parent_step = self.steps[parent_run_id]
parent_step.add_nested_step(step)
else:
# This is a root step - check if we're in an existing trace context
current_step = tracer.get_current_step()
current_trace = tracer.get_current_trace()
if current_step is not None:
# We're inside an existing step context - add as nested
current_step.add_nested_step(step)
elif current_trace is not None:
# Existing trace but no current step - add to trace
current_trace.add_step(step)
# Don't track in _traces_by_root since we're using external trace
else:
# No existing context - create standalone trace
trace = traces.Trace()
trace.add_step(step)
self._traces_by_root[run_id] = trace
# Track root steps (those without parent_run_id)
if parent_run_id is None:
self.root_steps.add(run_id)
# Override step ID with custom inference_id if provided
if self._inference_id is not None:
step.id = self._inference_id
self.steps[run_id] = step
return step
def _end_step(
self,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
output: Optional[Any] = None,
error: Optional[str] = None,
**step_kwargs: Any,
) -> None:
"""End a step and handle final processing."""
if run_id not in self.steps:
return
step = self.steps.pop(run_id)
is_root_step = run_id in self.root_steps
if is_root_step:
self.root_steps.remove(run_id)
# Update step with final data
if step.end_time is None:
step.end_time = time.time()
if step.latency is None:
step.latency = (step.end_time - step.start_time) * 1000
# Set raw output and additional attributes
if output is not None:
step.output = output # Keep raw
if error is not None:
step.metadata = {**step.metadata, "error": error}
# Set additional step attributes
for key, value in step_kwargs.items():
if hasattr(step, key):
setattr(step, key, value)
# Only upload if this is a standalone trace (not integrated with external trace)
# If current_step is set, we're part of a larger trace and shouldn't upload
if (
is_root_step
and run_id in self._traces_by_root
and tracer.get_current_step() is None
):
trace = self._traces_by_root.pop(run_id)
if tracer._configured_background_publish_enabled:
ctx = contextvars.copy_context()
tracer._get_background_executor().submit(
ctx.run, self._process_and_upload_trace, trace
)
else:
self._process_and_upload_trace(trace)
def _process_and_upload_trace(self, trace: traces.Trace) -> None:
"""Process and upload the completed trace (only for standalone root steps)."""
if not trace:
return
# Convert all LangChain objects in the trace once at the end
for step in trace.steps:
self._convert_step_objects_recursively(step)
trace_data, input_variable_names = tracer.post_process_trace(trace)
config = dict(
tracer.ConfigLlmData(
output_column_name="output",
input_variable_names=input_variable_names,
latency_column_name="latency",
cost_column_name="cost",
timestamp_column_name="inferenceTimestamp",
inference_id_column_name="inferenceId",
num_of_token_column_name="tokens",
)
)
# Add reserved column configurations for user context
if "user_id" in trace_data:
config.update({"user_id_column_name": "user_id"})
if "session_id" in trace_data:
config.update({"session_id_column_name": "session_id"})
if "groundTruth" in trace_data:
config.update({"ground_truth_column_name": "groundTruth"})
if "context" in trace_data:
config.update({"context_column_name": "context"})
root_step = trace.steps[0] if trace.steps else None
if (
root_step
and isinstance(root_step, steps.ChatCompletionStep)
and root_step.inputs
and "prompt" in root_step.inputs
):
config.update({"prompt": utils.json_serialize(root_step.inputs["prompt"])})
if tracer._publish:
try:
client = tracer._get_client()
if client:
# Apply final JSON serialization to ensure everything is serializable
serialized_trace_data = utils.json_serialize(trace_data)
serialized_config = utils.json_serialize(config)
client.inference_pipelines.data.stream(
inference_pipeline_id=utils.get_env_variable(
"OPENLAYER_INFERENCE_PIPELINE_ID"
),
rows=[serialized_trace_data],
config=serialized_config,
)
except Exception as err: # pylint: disable=broad-except
tracer.logger.error("Could not stream data to Openlayer %s", err)
# Reset trace context only for standalone traces
tracer._current_trace.set(None)
def _process_metadata(self, metadata: Dict[str, Any]) -> Dict[str, Any]:
"""Apply user-defined metadata transformation if provided."""
if not metadata:
return {}
# First convert LangChain objects to JSON-serializable format
converted_metadata = self._convert_langchain_objects(metadata)
# Then apply custom transformer if provided
if self._metadata_transformer:
try:
return self._metadata_transformer(converted_metadata)
except Exception as e:
# Log warning but continue with unconverted metadata
tracer.logger.warning(f"Metadata transformer failed: {e}")
return converted_metadata
return converted_metadata
def _convert_step_objects_recursively(self, step: steps.Step) -> None:
"""Convert all LangChain objects in a step and its nested steps."""
# Convert step attributes
if step.inputs is not None:
step.inputs = self._convert_langchain_objects(step.inputs)
if step.output is not None:
# For outputs, first convert then serialize
converted_output = self._convert_langchain_objects(step.output)
step.output = utils.json_serialize(converted_output)
if step.metadata is not None:
step.metadata = self._process_metadata(step.metadata)
# Convert nested steps recursively
for nested_step in step.steps:
self._convert_step_objects_recursively(nested_step)
def _convert_langchain_objects(self, obj: Any) -> Any:
"""Recursively convert LangChain objects to JSON-serializable format."""
# Explicit check for LangChain BaseMessage and its subclasses
if HAVE_LANGCHAIN and isinstance(obj, langchain_schema.BaseMessage):
return self._message_to_dict(obj)
# Handle ChatPromptValue objects which contain messages
if (
hasattr(obj, "messages")
and hasattr(obj, "__class__")
and "ChatPromptValue" in obj.__class__.__name__
):
return [self._convert_langchain_objects(msg) for msg in obj.messages]
# Handle dictionaries
if isinstance(obj, dict):
return {k: self._convert_langchain_objects(v) for k, v in obj.items()}
# Handle lists and tuples
if isinstance(obj, (list, tuple)):
return [self._convert_langchain_objects(item) for item in obj]
# Handle objects with messages attribute
if hasattr(obj, "messages"):
return [self._convert_langchain_objects(m) for m in obj.messages]
# Handle Pydantic model instances
if hasattr(obj, "model_dump") and callable(getattr(obj, "model_dump")):
try:
return self._convert_langchain_objects(obj.model_dump())
except Exception:
pass
# Handle Pydantic model classes/metaclasses (type objects)
if isinstance(obj, type):
return str(obj.__name__ if hasattr(obj, "__name__") else obj)
# Handle other LangChain objects with common attributes
if hasattr(obj, "dict") and callable(getattr(obj, "dict")):
# Many LangChain objects have a dict() method
try:
return self._convert_langchain_objects(obj.dict())
except Exception:
pass
# Handle objects with content attribute
if hasattr(obj, "content") and not isinstance(
obj, langchain_schema.BaseMessage
):
return obj.content
# Handle objects with value attribute
if hasattr(obj, "value"):
return self._convert_langchain_objects(obj.value)
# Handle objects with kwargs attribute
if hasattr(obj, "kwargs"):
return self._convert_langchain_objects(obj.kwargs)
# Return primitive types as-is
if isinstance(obj, (str, int, float, bool, type(None))):
return obj
# For everything else, convert to string
return str(obj)
def _message_to_dict(
self, message: "langchain_schema.BaseMessage"
) -> Dict[str, str]:
"""Convert a LangChain message to a JSON-serializable dictionary."""
message_type = getattr(message, "type", "user")
role = "user" if message_type == "human" else message_type
if message_type == "ai":
role = "assistant"
elif message_type == "system":
role = "system"
return {"role": role, "content": str(message.content)}
def _messages_to_prompt_format(
self, messages: List[List["langchain_schema.BaseMessage"]]
) -> List[Dict[str, str]]:
"""Convert LangChain messages to Openlayer prompt format using
unified conversion."""
prompt = []
for message_batch in messages:
for message in message_batch:
prompt.append(self._message_to_dict(message))
return prompt
def _extract_model_info(
self,
serialized: Dict[str, Any],
invocation_params: Dict[str, Any],
metadata: Dict[str, Any],
) -> Dict[str, Any]:
"""Extract model information generically."""
# Handle case where parameters can be None
serialized = serialized or {}
invocation_params = invocation_params or {}
metadata = metadata or {}
provider = invocation_params.get("_type")
if provider in LANGCHAIN_TO_OPENLAYER_PROVIDER_MAP:
provider = LANGCHAIN_TO_OPENLAYER_PROVIDER_MAP[provider]
model = (
invocation_params.get("model_name")
or invocation_params.get("model")
or metadata.get("ls_model_name")
or serialized.get("name")
)
# Handle LiteLLM model prefix (e.g. "gemini/gemini-2.5-flash"):
# extract the actual provider and strip the prefix from the model name.
if model and "/" in model:
prefix, model_name = model.split("/", 1)
litellm_provider = LITELLM_PREFIX_TO_PROVIDER_MAP.get(prefix)
if litellm_provider:
provider = litellm_provider
model = model_name
# Clean invocation params (remove internal LangChain params)
clean_params = {
k: v for k, v in invocation_params.items() if not k.startswith("_")
}
return {
"provider": provider,
"model": model,
"model_parameters": clean_params,
}
def _extract_token_info(
self, response: "langchain_schema.LLMResult"
) -> Dict[str, Any]:
"""Extract token information generically from LLM response."""
llm_output = response.llm_output or {}
# Try standard token_usage location first
token_usage = (
llm_output.get("token_usage") or llm_output.get("estimatedTokens") or {}
)
# Fallback to generation info for providers like Ollama/Google
if not token_usage and response.generations:
gen = response.generations[0][0]
generation_info = gen.generation_info or {}
# Ollama style
if "prompt_eval_count" in generation_info:
prompt_tokens = generation_info.get("prompt_eval_count", 0)
completion_tokens = generation_info.get("eval_count", 0)
token_usage = {
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": prompt_tokens + completion_tokens,
}
# Google style
elif "usage_metadata" in generation_info:
usage = generation_info["usage_metadata"]
token_usage = {
"prompt_tokens": usage.get("prompt_token_count", 0),
"completion_tokens": usage.get("candidates_token_count", 0),
"total_tokens": usage.get("total_token_count", 0),
}
# AWS Bedrock / newer LangChain style - usage_metadata on the message
elif hasattr(gen, "message") and hasattr(gen.message, "usage_metadata"):
usage = gen.message.usage_metadata
if usage:
token_usage = {
"prompt_tokens": usage.get("input_tokens", 0),
"completion_tokens": usage.get("output_tokens", 0),
"total_tokens": usage.get("total_tokens", 0),
}
return {
"prompt_tokens": token_usage.get("prompt_tokens", 0),
"completion_tokens": token_usage.get("completion_tokens", 0),
"tokens": token_usage.get("total_tokens", 0),
}
def _extract_output(self, response: "langchain_schema.LLMResult") -> str:
"""Extract output text from LLM response."""
output = ""
for generations in response.generations:
for generation in generations:
output += generation.text.replace("\n", " ")
return output
def _safe_parse_json(self, input_str: str) -> Any:
"""Safely parse JSON string, returning the string if parsing fails."""
try:
import json
return json.loads(input_str)
except (json.JSONDecodeError, TypeError):
return input_str
# ---------------------- Common Callback Logic ---------------------- #
def _handle_llm_start(
self,
serialized: Dict[str, Any],
prompts: List[str],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
**kwargs: Any,
) -> Any:
"""Common logic for LLM start."""
invocation_params = kwargs.get("invocation_params", {})
model_info = self._extract_model_info(
serialized, invocation_params, metadata or {}
)
step_name = f"{model_info['provider'] or 'LLM'} Chat Completion"
prompt = [{"role": "user", "content": text} for text in prompts]
self._start_step(
run_id=run_id,
parent_run_id=parent_run_id,
name=step_name,
step_type=enums.StepType.CHAT_COMPLETION,
inputs={"prompt": prompt},
metadata={"tags": tags} if tags else None,
**model_info,
)
def _handle_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List["langchain_schema.BaseMessage"]],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
**kwargs: Any,
) -> Any:
"""Common logic for chat model start."""
invocation_params = kwargs.get("invocation_params", {})
model_info = self._extract_model_info(
serialized, invocation_params, metadata or {}
)
# Always use provider-based name for chat completions (e.g. "Google Chat Completion")
# rather than the run_name from the caller (e.g. "Language Model") which is generic.
step_name = f"{model_info['provider'] or 'Chat Model'} Chat Completion"
prompt = self._messages_to_prompt_format(messages)
self._start_step(
run_id=run_id,
parent_run_id=parent_run_id,
name=step_name,
step_type=enums.StepType.CHAT_COMPLETION,
inputs={"prompt": prompt},
metadata={"tags": tags} if tags else None,
**model_info,
)
def _handle_llm_end(
self,
response: "langchain_schema.LLMResult",
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for LLM end."""
if run_id not in self.steps:
return
output = self._extract_output(response)
# Only extract token info if it hasn't been set during streaming
step = self.steps[run_id]
token_info = {}
if not (
hasattr(step, "prompt_tokens")
and step.prompt_tokens is not None
and step.prompt_tokens > 0
):
token_info = self._extract_token_info(response)
self._end_step(
run_id=run_id,
parent_run_id=parent_run_id,
output=output,
**token_info,
)
def _handle_llm_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Common logic for LLM error."""
self._end_step(run_id=run_id, parent_run_id=parent_run_id, error=str(error))
def _handle_chain_start(
self,
serialized: Dict[str, Any],
inputs: Dict[str, Any],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
**kwargs: Any,
) -> Any:
"""Common logic for chain start."""
# Extract chain name from serialized data or use provided name
# Handle case where serialized can be None
serialized = serialized or {}
chain_name = (
name
or (serialized.get("id", [])[-1] if serialized.get("id") else None)
or "Chain"
)
# Skip chains marked as hidden (e.g., internal LangGraph chains)
if tags and "langsmith:hidden" in tags:
return
if chain_name == "LangGraph" and inputs.get("messages"):
inputs = {
"prompt": inputs.get("messages"),
}
self._start_step(
run_id=run_id,
parent_run_id=parent_run_id,
name=chain_name,
step_type=enums.StepType.USER_CALL,
inputs=inputs,
metadata={
"tags": tags,
"serialized": serialized,
**(metadata or {}),
**kwargs,
},
)
def _handle_chain_end(
self,
outputs: Dict[str, Any],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for chain end."""
if run_id not in self.steps:
return
# Check if this is a ConversationalRetrievalChain with source documents
if isinstance(outputs, dict) and "source_documents" in outputs:
source_docs = outputs["source_documents"]
if source_docs:
# Extract content from source documents
context_list = []
for doc in source_docs:
if hasattr(doc, "page_content"):
context_list.append(doc.page_content)
else:
context_list.append(str(doc))
if context_list:
current_trace = tracer.get_current_trace()
if current_trace:
current_trace.update_metadata(context=context_list)
# Parse output for LangGraph
step = self.steps[run_id]
if step.name == "LangGraph" and outputs.get("messages"):
if isinstance(outputs.get("messages"), list):
if isinstance(
outputs.get("messages")[-1], langchain_schema.BaseMessage
):
outputs = outputs.get("messages")[-1].content
self._end_step(
run_id=run_id,
parent_run_id=parent_run_id,
output=outputs,
)
def _handle_chain_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Common logic for chain error."""
self._end_step(run_id=run_id, parent_run_id=parent_run_id, error=str(error))
def _handle_tool_start(
self,
serialized: Dict[str, Any],
input_str: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
inputs: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for tool start."""
# Handle case where serialized can be None
serialized = serialized or {}
tool_name = (
name
or serialized.get("name")
or (serialized.get("id", [])[-1] if serialized.get("id") else None)
or "Tool"
)
# Parse input - prefer structured inputs over string
tool_input = inputs or self._safe_parse_json(input_str)
self._start_step(
run_id=run_id,
parent_run_id=parent_run_id,
name=tool_name,
step_type=enums.StepType.TOOL,
inputs=tool_input,
metadata={
"tags": tags,
"serialized": serialized,
**(metadata or {}),
**kwargs,
},
function_name=tool_name,
arguments=tool_input,
)
def _handle_tool_end(
self,
output: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Common logic for tool end."""
if run_id not in self.steps:
return
self._end_step(
run_id=run_id,
parent_run_id=parent_run_id,
output=output,
)
def _handle_tool_error(
self,
error: Union[Exception, KeyboardInterrupt],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> Any:
"""Common logic for tool error."""
self._end_step(run_id=run_id, parent_run_id=parent_run_id, error=str(error))
def _handle_agent_action(
self,
action: "langchain_schema.AgentAction",
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for agent action."""
self._start_step(
run_id=run_id,
parent_run_id=parent_run_id,
name=f"Agent Tool: {action.tool}",
step_type=enums.StepType.AGENT,
inputs={
"tool": action.tool,
"tool_input": action.tool_input,
"log": action.log,
},
metadata={"agent_action": True, **kwargs},
tool=action.tool,
action=action,
agent_type="langchain_agent",
)
def _handle_agent_finish(
self,
finish: "langchain_schema.AgentFinish",
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for agent finish."""
if run_id not in self.steps:
return
self._end_step(
run_id=run_id,
parent_run_id=parent_run_id,
output=finish.return_values,
)
def _handle_retriever_start(
self,
serialized: Dict[str, Any],
query: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for retriever start."""
# Handle case where serialized can be None
serialized = serialized or {}
retriever_name = (
serialized.get("id", [])[-1] if serialized.get("id") else "Retriever"
)
self._start_step(
run_id=run_id,
parent_run_id=parent_run_id,
name=retriever_name,
step_type=enums.StepType.RETRIEVER,
inputs={"query": query},
metadata={
"tags": tags,
"serialized": serialized,
**(metadata or {}),
**kwargs,
},
)
def _handle_retriever_end(
self,
documents: List[Any],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for retriever end."""
if run_id not in self.steps:
return
# Extract document content
doc_contents = []
for doc in documents:
if hasattr(doc, "page_content"):
doc_contents.append(doc.page_content)
else:
doc_contents.append(str(doc))
current_trace = tracer.get_current_trace()
if current_trace:
current_trace.update_metadata(context=doc_contents)
# Update the step with RetrieverStep-specific attributes
step = self.steps[run_id]
if isinstance(step, steps.RetrieverStep):
step.documents = doc_contents
self._end_step(
run_id=run_id,
parent_run_id=parent_run_id,
output={"documents": doc_contents, "count": len(documents)},
)
def _handle_retriever_error(
self,
error: Exception,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
**kwargs: Any,
) -> Any:
"""Common logic for retriever error."""
self._end_step(run_id=run_id, parent_run_id=parent_run_id, error=str(error))
def _handle_llm_new_token(self, token: str, **kwargs: Any) -> Any:
"""Common logic for LLM new token."""
# Safely check for chunk and usage_metadata
chunk = kwargs.get("chunk")
if (
chunk
and hasattr(chunk, "message")
and hasattr(chunk.message, "usage_metadata")
):
usage = chunk.message.usage_metadata
# Only proceed if usage is not None
if usage:
# Extract run_id from kwargs (should be provided by LangChain)
run_id = kwargs.get("run_id")
if run_id and run_id in self.steps:
# Convert usage to the expected format like _extract_token_info does
token_info = {
"prompt_tokens": usage.get("input_tokens", 0),
"completion_tokens": usage.get("output_tokens", 0),
"tokens": usage.get("total_tokens", 0),
}
# Update the step with token usage information
step = self.steps[run_id]
if isinstance(step, steps.ChatCompletionStep):
step.log(**token_info)
return
class OpenlayerHandler(OpenlayerHandlerMixin, BaseCallbackHandlerClass): # type: ignore[misc]
"""LangChain callback handler that logs to Openlayer."""
def __init__(
self,
ignore_llm=False,
ignore_chat_model=False,
ignore_chain=False,
ignore_retriever=False,
ignore_agent=False,
inference_id: Optional[Any] = None,
metadata_transformer: Optional[
Callable[[Dict[str, Any]], Dict[str, Any]]
] = None,
**kwargs: Any,
) -> None:
# Add both inference_id and metadata_transformer to kwargs so they get passed to mixin
if inference_id is not None:
kwargs["inference_id"] = inference_id
if metadata_transformer is not None:
kwargs["metadata_transformer"] = metadata_transformer
super().__init__(**kwargs)
# Store the ignore flags as instance variables
self._ignore_llm = ignore_llm
self._ignore_chat_model = ignore_chat_model
self._ignore_chain = ignore_chain
self._ignore_retriever = ignore_retriever
self._ignore_agent = ignore_agent
@property
def ignore_llm(self) -> bool:
return self._ignore_llm
@property
def ignore_chat_model(self) -> bool:
return self._ignore_chat_model
@property
def ignore_chain(self) -> bool:
return self._ignore_chain
@property
def ignore_retriever(self) -> bool:
return self._ignore_retriever
@property
def ignore_agent(self) -> bool:
return self._ignore_agent
def on_llm_start(
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
) -> Any:
"""Run when LLM starts running."""
if self.ignore_llm:
return
return self._handle_llm_start(serialized, prompts, **kwargs)
def on_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List["langchain_schema.BaseMessage"]],
**kwargs: Any,
) -> Any:
"""Run when Chat Model starts running."""
if self.ignore_chat_model:
return