> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# op

> Python SDK reference for weave.trace.op

export const SourceLink = ({url}) => <a href={url} target="_blank" rel="noopener noreferrer" className="source-link">
    Source
  </a>;

# API Overview

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L85" />

## <kbd>class</kbd> `DisplayNameFuncError`

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L88" />

## <kbd>class</kbd> `OpCallError`

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L204" />

## <kbd>class</kbd> `OpKwargs`

TypedDict for op() keyword arguments.

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L109" />

## <kbd>class</kbd> `Sentinel`

Sentinel(package: 'str', path: 'str', name: 'str')

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/../../../../weave/trace/op/__init__" />

### <kbd>method</kbd> `__init__`

```python theme={null}
__init__(package: 'str', path: 'str', name: 'str') → None
```

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L198" />

## <kbd>class</kbd> `WeaveKwargs`

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1421" />

### <kbd>function</kbd> `as_op`

```python theme={null}
as_op(fn: 'Callable[P, R]') → Op[P, R]
```

Given a @weave.op decorated function, return its Op.

@weave.op decorated functions are instances of Op already, so this function should be a no-op at runtime. But you can use it to satisfy type checkers if you need to access OpDef attributes in a typesafe way.

**Args:**

* <b>`fn`</b>: A weave.op decorated function.
  **Returns:**
  The Op of the function.

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1150" />

### <kbd>function</kbd> `call`

```python theme={null}
call(
    op: 'Op',
    *args: 'Any',
    __weave: 'WeaveKwargs | None' = None,
    __should_raise: 'bool' = False,
    __require_explicit_finish: 'bool' = False,
    **kwargs: 'Any'
) → tuple[Any, Call] | Coroutine[Any, Any, tuple[Any, Call]]
```

Executes the op and returns both the result and a Call representing the execution.

This function will never raise.  Any errors are captured in the Call object.

This method is automatically bound to any function decorated with `@weave.op`, allowing for usage like:

```python theme={null}
@weave.op
def add(a: int, b: int) -> int:
     return a + b

result, call = add.call(1, 2)
```

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1196" />

### <kbd>function</kbd> `calls`

```python theme={null}
calls(op: 'Op') → CallsIter
```

Get an iterator over all calls to this op.

This method is automatically bound to any function decorated with `@weave.op`, allowing for usage like:

```python theme={null}
@weave.op
def add(a: int, b: int) -> int:
     return a + b

calls = add.calls()
for call in calls:
     print(call)
```

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1367" />

### <kbd>function</kbd> `get_captured_code`

```python theme={null}
get_captured_code(op: 'Op') → str
```

Get the captured code of the op.

This only works when you get an op back from a ref.  The pattern is:

ref = weave.publish(func) op = ref.get() captured\_code = op.get\_captured\_code()

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1413" />

### <kbd>function</kbd> `is_op`

```python theme={null}
is_op(obj: 'Any') → TypeIs[Op]
```

Check if an object is an Op.

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L423" />

### <kbd>function</kbd> `is_placeholder_call`

```python theme={null}
is_placeholder_call(call: 'Call') → TypeIs[NoOpCall]
```

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L391" />

### <kbd>function</kbd> `is_tracing_setting_disabled`

```python theme={null}
is_tracing_setting_disabled() → bool
```

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1384" />

### <kbd>function</kbd> `maybe_bind_method`

```python theme={null}
maybe_bind_method(func: 'Callable', self: 'Any' = None) → Callable | MethodType
```

Bind a function to any object (even if it's not a class).

If self is None, return the function as is.

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1396" />

### <kbd>function</kbd> `maybe_unbind_method`

```python theme={null}
maybe_unbind_method(oplike: 'Op | MethodType | partial') → Op
```

Unbind an Op-like method or partial to a plain Op function.

For:

* methods, remove set `self` param
* partials, remove any preset params

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L1220" />

### <kbd>function</kbd> `op`

```python theme={null}
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. Works for both sync and async. Automatically detects iterator functions and applies appropriate behavior.

**Args:**

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L416" />

### <kbd>function</kbd> `placeholder_call`

```python theme={null}
placeholder_call() → Call
```

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L219" />

### <kbd>function</kbd> `setup_dunder_weave_dict`

```python theme={null}
setup_dunder_weave_dict(op: 'Op', d: 'WeaveKwargs | None' = None) → WeaveKwargs
```

Sets up a \_\_weave dict used to pass WeaveKwargs to ops.

* <b>`func`</b>: The function to decorate.

* <b>`name`</b>: Custom name for the op. Defaults to the function name.

* <b>`call_display_name`</b>: Display name for calls, can be a string or callable.

* <b>`postprocess_inputs`</b>: Function to transform inputs before logging.

* <b>`postprocess_output`</b>: Function to transform output before logging.

* <b>`tracing_sample_rate`</b>: Fraction of calls to trace (0.0 to 1.0).

* <b>`enable_code_capture`</b>: Whether to capture source code for this op.

* <b>`accumulator`</b>: Function to accumulate results for streaming ops.

* <b>`eager_call_start`</b>: If True, call starts are sent immediately rather than batched.  Useful for long-running operations like evaluations that should  be visible in the UI immediately.
  **Args:**

* <b>`d`</b>: Optional existing WeaveKwargs dict to update.

* <b>`op`</b>: Op to extract kind and color from.
  **Returns:**
  WeaveKwargs dict with attributes, display\_name, and optionally kind/color set.

***

<SourceLink url="https://github.com/wandb/weave/blob/v0.52.35/weave/trace/op.py#L402" />

### <kbd>function</kbd> `should_skip_tracing_for_op`

```python theme={null}
should_skip_tracing_for_op(op: 'Op') → bool
```
