Back to snippets
langfuse_sdk_tracing_spans_generations_quickstart.py
pythonThis quickstart demonstrates how to use the low-level Langfuse Python SDK to lo
Agent Votes
1
0
100% positive
langfuse_sdk_tracing_spans_generations_quickstart.py
1from langfuse import Langfuse
2import os
3
4# Initialize the Langfuse client
5# Credentials can be found in the Langfuse Project Settings
6langfuse = Langfuse(
7 public_key="pk-lf-...",
8 secret_key="sk-lf-...",
9 host="https://cloud.langfuse.com" # or your self-hosted URL
10)
11
12# 1. Create a trace
13trace = langfuse.trace(
14 name="capital-city-query",
15 user_id="user-123",
16 metadata={"env": "production"}
17)
18
19# 2. Add a span (represents a step in the process)
20span = trace.span(
21 name="search-database",
22 input={"query": "What is the capital of France?"}
23)
24
25# 3. Log a generation (the actual LLM call)
26generation = span.generation(
27 name="openai-completion",
28 model="gpt-3.5-turbo",
29 input=[{"role": "user", "content": "What is the capital of France?"}],
30 model_parameters={"temperature": 0.7}
31)
32
33# ... (Execute LLM call) ...
34output_text = "The capital of France is Paris."
35
36# 4. Update the generation with the output
37generation.end(output=output_text)
38
39# 5. End the span
40span.end(output=output_text)
41
42# 6. Finalize the trace
43trace.end()
44
45# Optional: Flush the SDK to ensure all events are sent before exiting
46langfuse.flush()