Skip to main content
W&B Weave supports tracing both sync and async generator functions, including deeply nested patterns.
Because generators yield values lazily, Weave logs outputs only when the generator is fully consumed (for example, when you convert it to a list). To ensure Weave captures outputs in the trace, fully consume the generator (for example, with list()).
The following screenshot shows the Traces page with a selected trace of the preceding code. The center panel shows the trace tree for the selected trace. The trace tree shows the deeply_nested_generator, nested_generator, and inner Ops in the trace tree hierarchy. Weave Traces page showing a selected trace tree illustrating deeply nested Ops

Consuming generators

Weave captures generator outputs only after you fully consume the generator. Consume the generator by iterating over it (for example, with list(), a for loop, or next() until exhaustion). The same applies to async generators when you use async for or equivalent consumption. For more on decorating functions and methods with @weave.op, see Create calls.

Accumulate yielded values into a single trace

You can use the weave.op’s accumulator parameter to customize how yielded values are combined from generator functions, for example, to join streamed text tokens into a single string. The accumulator is a two-argument function that Weave calls once per yielded value, building up a result incrementally.
The accumulator parameter is not available for the TypeScript.
The following example demonstrates a custom accumulator that appends each yielded value to a list, so Weave records that list as the call output after the generator is fully consumed.