Skip to content

Commit 1f4f219

Browse files
Refactor llm compliance example trace observations
1 parent f954296 commit 1f4f219

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

examples/llm-compliance/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The workflow:
99
- loads a concise incident-summary document from [`incident-summary.md`](./incident-summary.md)
1010
- checks one graded rule with `gpt-5.5`
1111
- parses a structured object with `compliance_score` from `0` to `10` and `reasoning`
12-
- records a trace with a workflow span, a document-loading span, and one captured LLM generation
12+
- records one trace with two root observations: a document-loading span and one captured LLM generation
1313
- uses the Langfuse OpenAI integration to capture the LLM generation automatically
1414

1515
The compliance rule is intentionally graded instead of binary:

examples/llm-compliance/python/compliance_workflow.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from pathlib import Path
33

4-
from langfuse import get_client, observe, propagate_attributes
4+
from langfuse import get_client, propagate_attributes
55
from langfuse.openai import OpenAI
66
from pydantic import BaseModel, Field
77

@@ -40,9 +40,20 @@ def require_env():
4040
)
4141

4242

43-
@observe(name="load incident summary", as_type="span")
44-
def load_incident_summary():
45-
return DOCUMENT_PATH.read_text(encoding="utf-8")
43+
def load_incident_summary(langfuse, trace_id):
44+
load_span = langfuse.start_observation(
45+
trace_context={"trace_id": trace_id},
46+
name="load incident summary",
47+
as_type="span",
48+
input={
49+
"document_type": "incident-summary",
50+
"path": str(DOCUMENT_PATH),
51+
},
52+
)
53+
document = DOCUMENT_PATH.read_text(encoding="utf-8")
54+
load_span.update(output={"document": document})
55+
load_span.end()
56+
return document
4657

4758

4859
def compliance_messages(document):
@@ -62,7 +73,7 @@ def compliance_messages(document):
6273
]
6374

6475

65-
def check_document(document):
76+
def check_document(trace_id, document):
6677
openai_client = OpenAI()
6778
messages = compliance_messages(document)
6879
completion = openai_client.chat.completions.parse(
@@ -74,16 +85,14 @@ def check_document(document):
7485
"structured_output": "ComplianceResult",
7586
"compliance_rule": COMPLIANCE_RULE,
7687
},
88+
trace_id=trace_id,
7789
)
7890
return completion.choices[0].message.parsed
7991

8092

81-
@observe(
82-
name="incident summary compliance workflow",
83-
as_type="span",
84-
capture_input=False,
85-
)
8693
def run_workflow():
94+
langfuse = get_client()
95+
trace_id = langfuse.create_trace_id()
8796
with propagate_attributes(
8897
trace_name="incident summary compliance workflow",
8998
session_id="llm-compliance-example",
@@ -94,10 +103,9 @@ def run_workflow():
94103
"compliance_rule": COMPLIANCE_RULE,
95104
},
96105
):
97-
document = load_incident_summary()
98-
langfuse = get_client()
99-
result = check_document(document)
100-
return langfuse.get_current_trace_id(), result
106+
document = load_incident_summary(langfuse, trace_id)
107+
result = check_document(trace_id, document)
108+
return trace_id, result
101109

102110

103111
def require_trace_id(trace_id):

0 commit comments

Comments
 (0)