Skip to content

Commit a1846e0

Browse files
gustavocidornelaswhoseoyster
authored andcommitted
feat: log context for LangChain callback handler
1 parent 063fc77 commit a1846e0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/openlayer/lib/integrations/langchain_callback.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,23 @@ def _handle_chain_end(
574574
if run_id not in self.steps:
575575
return
576576

577+
# Check if this is a ConversationalRetrievalChain with source documents
578+
if isinstance(outputs, dict) and "source_documents" in outputs:
579+
source_docs = outputs["source_documents"]
580+
if source_docs:
581+
# Extract content from source documents
582+
context_list = []
583+
for doc in source_docs:
584+
if hasattr(doc, "page_content"):
585+
context_list.append(doc.page_content)
586+
else:
587+
context_list.append(str(doc))
588+
589+
if context_list:
590+
current_trace = tracer.get_current_trace()
591+
if current_trace:
592+
current_trace.update_metadata(context=context_list)
593+
577594
self._end_step(
578595
run_id=run_id,
579596
parent_run_id=parent_run_id,
@@ -754,6 +771,10 @@ def _handle_retriever_end(
754771
else:
755772
doc_contents.append(str(doc))
756773

774+
current_trace = tracer.get_current_trace()
775+
if current_trace:
776+
current_trace.update_metadata(context=doc_contents)
777+
757778
self._end_step(
758779
run_id=run_id,
759780
parent_run_id=parent_run_id,

0 commit comments

Comments
 (0)