isaaccs
isaaccs•10mo ago

python sdk: observations n same trace

Hey, I try to have one trace for all my endpoints without succeed example of my code
def call_llm(trace: StatefulTraceClient = None):
span = trace.span(
CreateSpan(
name="name",
input={some_values},
)
) if trace else None
handler = span.get_langchain_handler() if span else create_langfuse_callback()

## llm call
output = await model.apredict(prompt, callbacks=[handler] if handler else None)
if span:
span.update(
UpdateSpan(
output={
"response": output
}
)
)

def endpoint_1():
langfuse = Langfuse(langfuse_key, langfuse_secret, langfuse_url, release=os.getenv("DD_VERSION", "local"))
# create trace_id for future reference
span = tracer.current_root_span()
trace_id = span.trace_id if span else None
# create the Trace
trace = langfuse.trace(CreateTrace(trace_id=trace_id))


call_llm = await llm_call(
trace=trace
)
def call_llm(trace: StatefulTraceClient = None):
span = trace.span(
CreateSpan(
name="name",
input={some_values},
)
) if trace else None
handler = span.get_langchain_handler() if span else create_langfuse_callback()

## llm call
output = await model.apredict(prompt, callbacks=[handler] if handler else None)
if span:
span.update(
UpdateSpan(
output={
"response": output
}
)
)

def endpoint_1():
langfuse = Langfuse(langfuse_key, langfuse_secret, langfuse_url, release=os.getenv("DD_VERSION", "local"))
# create trace_id for future reference
span = tracer.current_root_span()
trace_id = span.trace_id if span else None
# create the Trace
trace = langfuse.trace(CreateTrace(trace_id=trace_id))


call_llm = await llm_call(
trace=trace
)
9 Replies
Marc
Marc•10mo ago
Hi @isaaccs, is this stable across all of your endpoints? span = tracer.current_root_span()
isaaccs
isaaccs•10mo ago
yes i also try with harcoded trace_id
Marc
Marc•10mo ago
found it, try langfuse.trace(CreateTrace(id=trace_id)), trace_id does not exist on the Pydantic object
Marc
Marc•10mo ago
Python SDK - Langfuse
Fully async and typed Python SDK. Uses Pydantic objects for data verification.
isaaccs
isaaccs•10mo ago
oh yes thank you
Marc
Marc•10mo ago
🤗 Happy that it works for you @Max currently improving the python interfces, with v2 this will be much simpler (without having to change much though)
isaaccs
isaaccs•10mo ago
perfect
isaaccs
isaaccs•10mo ago
last question let suppose my code is like that
def endpoint_1():
langfuse = Langfuse(langfuse_key, langfuse_secret, langfuse_url, release=os.getenv("DD_VERSION", "local"))
# create trace_id for future reference
span = tracer.current_root_span()
trace_id = span.trace_id if span else None
# create the Trace
trace = langfuse.trace(CreateTrace(id=trace_id))


call_llm = await llm_call(
trace=trace
)
call_llm = await llm_call(
trace=trace
)
call_llm = await llm_call(
trace=trace
)
def endpoint_1():
langfuse = Langfuse(langfuse_key, langfuse_secret, langfuse_url, release=os.getenv("DD_VERSION", "local"))
# create trace_id for future reference
span = tracer.current_root_span()
trace_id = span.trace_id if span else None
# create the Trace
trace = langfuse.trace(CreateTrace(id=trace_id))


call_llm = await llm_call(
trace=trace
)
call_llm = await llm_call(
trace=trace
)
call_llm = await llm_call(
trace=trace
)
how can i get something like this
No description
Marc
Marc•10mo ago
depends, you can either use the sdk clients, each call returns the object which can create child objects
trace = langfuse.trace()
span = trace.span()
generation = span.generation()
trace = langfuse.trace()
span = trace.span()
generation = span.generation()
alternatively, you can nest with ids youself
trace_id = "no1"
span_id = "no2"
langfuse.trace(CreateTrace(id=trace_id))
langfuse.span(InitialSpan(trace_id=trace_id, id=span_id))

nested_generation = langfuse.generation(InitialGeneration(trace_id=trace_id, parent_observation_id=span_id))
trace_id = "no1"
span_id = "no2"
langfuse.trace(CreateTrace(id=trace_id))
langfuse.span(InitialSpan(trace_id=trace_id, id=span_id))

nested_generation = langfuse.generation(InitialGeneration(trace_id=trace_id, parent_observation_id=span_id))
instead of creating your own ids, you can also let the sdk generate ids
span = langfuse.span(InitialSpan(trace_id=trace_id))
nested_generation = langfuse.generation(InitialGeneration(trace_id=trace_id, id=span_id, parent_observation_id=span.id))
span = langfuse.span(InitialSpan(trace_id=trace_id))
nested_generation = langfuse.generation(InitialGeneration(trace_id=trace_id, id=span_id, parent_observation_id=span.id))
hope this helps