Example
inference_result_id directly in the constructor:
Constructor Parameters
Galtea inference result ID to link traces to. When provided, the handler automatically manages
set_context / clear_context around LangChain callback lifecycles. All spans created during the invocation are stamped with this ID. Can also be set or changed later via set_inference_result_id().langchain.CallbackHandler.
Methods
Set or update the inference result ID for subsequent invocations. Use this when reusing a single handler across multiple requests (singleton pattern). Pass
None to disable Galtea correlation.Behavior
The handler automatically manages Galtea trace context using depth tracking across LangChain callbacks:- On the first
on_*_startcallback (root depth),set_context(inference_result_id)is called - On the last
on_*_endoron_*_errorcallback (back to root depth),clear_context()is called - Nested callbacks (tool calls, LLM calls inside a chain) increment/decrement the depth counter without affecting context
If a Galtea context is already active (e.g., from a parent
@observe decorator or set_context call), the handler does not override it. The outermost context always takes precedence.Reuse
A singleCallbackHandler instance can be reused across multiple chain.invoke() calls. Context is managed per-invocation — set on the first callback start and cleared on the last callback end.
Mixed Usage
CallbackHandler can be combined with @observe and start_as_current_observation. For example, you can use @observe on a parent function and pass a CallbackHandler to LangChain inside it — the parent-child hierarchy is preserved automatically.