untilhamza
untilhamza12mo ago

Langchain handler with multiple spans as parent elements

I end up with a trace like this
TRACE (id: trace_id)
|-- GENERATION: OpenAI (id: generated by Langfuse)
... (there is a lot more of these from the first loop)
|-- GENERATION: OpenAI (id: generated by Langfuse)
|-- (there is more from the second loop)
TRACE (id: trace_id)
|-- GENERATION: OpenAI (id: generated by Langfuse)
... (there is a lot more of these from the first loop)
|-- GENERATION: OpenAI (id: generated by Langfuse)
|-- (there is more from the second loop)
How do i get this instead
TRACE (id: trace_id)
|-- Span: A span from the first async loop
| |
| |-- GENERATION: OpenAI (id: generated by Langfuse)
| |-- GENERATION: OpenAI (id: generated by Langfuse)
... (there is a lot more of these from the first loop)
|
|-- Span: A span from the second async loop
| |
| |-- GENERATION: OpenAI (id: generated by Langfuse)
| |-- GENERATION: OpenAI (id: generated by Langfuse)
... (there is a lot more of these from the second loop)
TRACE (id: trace_id)
|-- Span: A span from the first async loop
| |
| |-- GENERATION: OpenAI (id: generated by Langfuse)
| |-- GENERATION: OpenAI (id: generated by Langfuse)
... (there is a lot more of these from the first loop)
|
|-- Span: A span from the second async loop
| |
| |-- GENERATION: OpenAI (id: generated by Langfuse)
| |-- GENERATION: OpenAI (id: generated by Langfuse)
... (there is a lot more of these from the second loop)
I am sorry for the abstract functions, but I am not able to share the exact code. Thanks a lot in advance.
3 Replies
Marc
Marc12mo ago
HI @untilhamza, happy to help with this. I'd recommend to use the following approach: - create trace: trace=langfuse.trace(...) - create the parent spans for each async loop: span=trace.span(...) - get callback handlers scoped to each of the spans to use langchain to make your LLM calls: scoped_handler = span.get_langchain_handler() Let me know if this helps solve it for you
untilhamza
untilhamza12mo ago
Thanks a lot! I will try it tomorrow and let you know.
Marc
Marc12mo ago
Perfect