The @weave.op decorator is used to track function calls in Weave. It automatically logs inputs, outputs, and metadata for decorated functions.
Basic Usage
import weave
@weave.op
def my_function(x: int, y: int) -> int:
return x + y
# When called, this function is automatically tracked
result = my_function(3, 4)
Decorator Options
name
Set a custom name for the operation.
@weave.op(name="custom_operation_name")
def my_function():
pass
call_display_name
Set a display name for all calls of this operation.
@weave.op(call_display_name="My Custom Display")
def my_function():
pass
You can also use a function to dynamically generate display names:
def generate_name(call):
return f"Processing {call.inputs['item_id']}"
@weave.op(call_display_name=generate_name)
def process_item(item_id: str):
pass
postprocess_inputs
Transform inputs before they are logged.
def redact_sensitive(inputs):
if 'password' in inputs:
inputs['password'] = '[REDACTED]'
return inputs
@weave.op(postprocess_inputs=redact_sensitive)
def login(username: str, password: str):
pass
postprocess_output
Transform outputs before they are logged.
def format_output(output):
return weave.Markdown(f"**Result:** {output}")
@weave.op(postprocess_output=format_output)
def generate_report():
return "Analysis complete"
Op Methods
call
Get both the result and the Call object.
@weave.op
def my_function(x: int) -> int:
return x * 2
result, call = my_function.call(5)
print(f"Result: {result}")
print(f"Call ID: {call.id}")
name
Get or set the operation name.
@weave.op
def my_function():
pass
# Get the current name
print(my_function.name)
# Set a new name
my_function.name = "new_operation_name"
Working with Async Functions
Weave supports async operations:
@weave.op
async def async_operation(prompt: str) -> str:
# Async operations are tracked the same way
result = await some_async_api(prompt)
return result
Working with Generators
Weave can track generator functions:
@weave.op
def generate_items(count: int):
for i in range(count):
yield f"Item {i}"
# The output is captured when the generator is consumed
items = list(generate_items(5))
Class Methods
Decorate methods on classes:
class MyClass:
@weave.op
def process(self, data: str) -> str:
return data.upper()
instance = MyClass()
result = instance.process("hello")
Static and Class Methods
class MyClass:
@staticmethod
@weave.op
def static_method(x: int) -> int:
return x * 2
@classmethod
@weave.op
def class_method(cls, x: int) -> int:
return x * 3
The @weave.op decorator should be the innermost decorator when stacking decorators.