Flow Decorator
Automatic tracing for AI features
Overview
The Flow decorator creates and manages traces for your AI feature. When applied to a function, it:
- Creates a new trace on function invocation.
- Adds all Humanloop logging calls made inside the function to the trace.
- Completes the trace when the function exits.
On Humanloop, a trace is the collection of Logs associated with a Flow Log.
Usage
The flow
decorator will trace all downstream Humanloop logs, whether they are created by other decorators or SDK calls.
Tracing Decorators
Tracing SDK Calls
Logs created through the Humanloop SDK are added to the trace.
Behavior
Python
TypeScript
The decorated function creates a Flow Log when called. All Logs created inside the decorated function are added to its trace.
The Flow Log’s fields are populated as follows:
If the decorated function returns a ChatMessage object, the output_message
field is populated. Otherwise, the output
field is populated with the stringified return value.
Definition
Python
TypeScript
The decorator will preserve the function’s signature.
The decorator accepts the following parameters:
SDK Interactions
Python
TypeScript
- It’s not possible to call
flows.log()
inside a decorated function. This will raise aHumanloopRuntimeError
- To create nested traces, call another flow-decorated function.
- Passing
trace_parent_id
argument to an SDK logging call inside the decorated function is ignored and emits a warning; the Log is added to the trace of the decorated function.
Error Handling
Python
TypeScript
- If user-written code (e.g. in code Evaluators) raises an exception, the relevant Log’s
error
field is populated with the exception message and the decorated function returnsNone
. HumanloopRuntimeError
exceptions indicate incorrect decorator or SDK usage and are re-raised instead of being logged undererror
.
Related Documentation
A explanation of Flows and their role in the Humanloop platform is found in our Flows documentation.