Skip to content

Commit be7d201

Browse files
committed
python script to test langfuse
1 parent 4c96727 commit be7d201

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Patch pydantic v1 for Python 3.14 (PEP 649 deferred annotations).
3+
4+
Import this module before importing any pydantic v1 models (e.g., langfuse).
5+
"""
6+
7+
import annotationlib
8+
9+
import pydantic.v1.main as pydantic_main
10+
11+
_orig = pydantic_main.ModelMetaclass.__new__
12+
13+
14+
def _patched(mcs, name, bases, ns, **kw):
15+
if not ns.get("__annotations__") and "__annotate_func__" in ns:
16+
try:
17+
ns["__annotations__"] = ns["__annotate_func__"](annotationlib.Format.VALUE)
18+
except Exception:
19+
pass
20+
return _orig(mcs, name, bases, ns, **kw)
21+
22+
23+
pydantic_main.ModelMetaclass.__new__ = staticmethod(_patched)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import time
3+
import patch_pydantic # noqa: F401
4+
from langfuse import Langfuse
5+
6+
7+
# --- CONFIGURATION ---
8+
LANGFUSE_PUBLIC_KEY = os.environ.get("LANGFUSE_PUBLIC_KEY")
9+
LANGFUSE_SECRET_KEY = os.environ.get("LANGFUSE_SECRET_KEY")
10+
# IMPORTANT: Use YOUR cloud instance URL here
11+
LANGFUSE_HOST = os.environ.get("LANGFUSE_HOST")
12+
13+
14+
def run_test():
15+
# 1. Initialize Client
16+
langfuse = Langfuse(
17+
public_key=LANGFUSE_PUBLIC_KEY,
18+
secret_key=LANGFUSE_SECRET_KEY,
19+
host=LANGFUSE_HOST,
20+
debug=True # Enable debug to see logs in console
21+
)
22+
23+
print("Sending trace...")
24+
25+
# Create a span using a context manager
26+
with langfuse.start_as_current_observation(as_type="span", name="process-request") as span:
27+
# Your processing logic here
28+
span.update(output="Processing complete")
29+
30+
# Create a nested generation for an LLM call
31+
with langfuse.start_as_current_observation(as_type="generation", name="llm-response", model="gpt-3.5-turbo") as generation:
32+
# Your LLM call logic here
33+
generation.update(output="Generated response")
34+
35+
# All spans are automatically closed when exiting their context blocks
36+
37+
38+
# Flush events in short-lived applications
39+
langfuse.flush()
40+
41+
if __name__ == "__main__":
42+
run_test()

0 commit comments

Comments
 (0)