Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Wandb Weave Weave Op

From Leeroopedia
Knowledge Sources
Domains Observability, Instrumentation
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete tool for decorating Python functions with Weave tracing provided by the Wandb Weave library.

Description

The @weave.op decorator wraps a Python function (sync, async, generator, or async generator) into an Op object that transparently traces all calls. When a traced function is invoked, Weave automatically captures its inputs, outputs, timing, exceptions, and parent-child call relationships.

The decorator conforms to the Op Protocol (a runtime-checkable Protocol class) ensuring type safety and introspection support.

Usage

Apply this decorator to any function whose execution should be recorded in the Weave trace timeline. It works with or without parentheses and supports configuration parameters for sampling, display customization, and input/output postprocessing.

Code Reference

Source Location

  • Repository: wandb/weave
  • File: weave/trace/op.py
  • Lines: L1203-1280

Signature

def op(
    func: Callable[P, R] | None = None,
    *,
    name: str | None = None,
    call_display_name: str | CallDisplayNameFunc | None = None,
    postprocess_inputs: PostprocessInputsFunc | None = None,
    postprocess_output: PostprocessOutputFunc | None = None,
    tracing_sample_rate: float = 1.0,
    enable_code_capture: bool = True,
    accumulator: Callable[[Any | None, Any], Any] | None = None,
    kind: OpKind | None = None,
    color: OpColor | None = None,
    eager_call_start: bool = False,
) -> Callable[[Callable[P, R]], Op[P, R]] | Op[P, R]:
    """A decorator to weave op-ify a function or method.

    Args:
        func: The function to decorate.
        name: Custom name for the op. Defaults to the function name.
        call_display_name: Display name for calls, can be a string or callable.
        postprocess_inputs: Function to transform inputs before logging.
        postprocess_output: Function to transform output before logging.
        tracing_sample_rate: Fraction of calls to trace (0.0 to 1.0).
        enable_code_capture: Whether to capture source code for this op.
        accumulator: Function to accumulate results for streaming ops.
        eager_call_start: If True, call starts are sent immediately.
    """

Import

import weave

I/O Contract

Inputs

Name Type Required Description
func None No The function to decorate (None when used with parentheses)
name None No Custom op name (defaults to function name)
call_display_name CallDisplayNameFunc | None No Display name for calls in the UI
postprocess_inputs None No Transform inputs before logging
postprocess_output None No Transform output before logging
tracing_sample_rate float No Fraction of calls to trace (0.0-1.0, default 1.0)
enable_code_capture bool No Capture source code (default True)
accumulator None No Accumulate streaming results
eager_call_start bool No Send call start immediately (default False)

Outputs

Name Type Description
return Op[P, R] Callable wrapper conforming to the Op Protocol

Usage Examples

Basic Usage

import weave

weave.init("my-team/my-project")

@weave.op
def my_function(x: int, y: int) -> int:
    return x + y

# Calls are automatically traced
result = my_function(3, 4)

With Configuration

import weave

@weave.op(
    name="custom_scorer",
    tracing_sample_rate=0.5,
    postprocess_inputs=lambda inputs: {k: v for k, v in inputs.items() if k != "secret"},
)
def score_response(prompt: str, response: str, secret: str) -> float:
    return len(response) / len(prompt)

Async Function

import weave
import asyncio

@weave.op
async def async_predict(text: str) -> str:
    await asyncio.sleep(0.1)
    return f"Processed: {text}"

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment