Sara
Sara10mo ago

Nested generations / spans in traces

Hi everyone! I'm trying to use the trace and observation IDs to create a nested set of observations instead of using the objects in the Python SDK. I was able to use langfuse.generation(CreateGeneration(traceId=trace.id)) instead of trace.generation(CreateGeneration()) to create a generation inside a trace but I'm having trouble creating observations under another observation. I tried the following which didn't work. I was expecting the following code to create, a span inside that trace, and a generation inside the span. But instead it creates a span and a generation inside the trace. Would appreciate some guidance on this.
langfuse = Langfuse(
public_key=Config.fetch("langfuse-public-key"),
secret_key=Config.fetch("langfuse-secret-key"),
host=Config.fetch("langfuse-host")
)

trace = langfuse.trace(CreateTrace(
user_id="test-user",
metadata = {
"env": "local",
}
))
print("trace", trace.id)

span = langfuse.span(CreateSpan(
traceId=trace.id,
name="test-span",
prompt="Hello world from span",
))
print("span", span.id)

generation = langfuse.generation(CreateGeneration(
traceId=trace.id,
parentObservationId=span.id,
name="test-generation",
prompt="Hello world from generation",
))
print("generation", generation.id)
langfuse = Langfuse(
public_key=Config.fetch("langfuse-public-key"),
secret_key=Config.fetch("langfuse-secret-key"),
host=Config.fetch("langfuse-host")
)

trace = langfuse.trace(CreateTrace(
user_id="test-user",
metadata = {
"env": "local",
}
))
print("trace", trace.id)

span = langfuse.span(CreateSpan(
traceId=trace.id,
name="test-span",
prompt="Hello world from span",
))
print("span", span.id)

generation = langfuse.generation(CreateGeneration(
traceId=trace.id,
parentObservationId=span.id,
name="test-generation",
prompt="Hello world from generation",
))
print("generation", generation.id)
2 Replies
Max
Max10mo ago
Hi Sara, great to have you here! Our pydantic interfaces are a bit confisung. You have to take InitialGeneration instead of CreateGeneration. We are working on this as we remove pydantic interfaces from the SDK (https://github.com/langfuse/langfuse-python/pull/215)
Sara
Sara10mo ago
Oh I see! Thanks for the help Max! I'll give that a try