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 Init

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

Overview

Concrete tool for initializing a Weave tracing session provided by the Wandb Weave library.

Description

The weave.init() function establishes an authenticated connection to a Weights & Biases project and returns a WeaveClient instance. It authenticates via the W&B API, resolves the project entity, creates the client, and automatically patches imported LLM provider SDKs (OpenAI, Anthropic, etc.) for transparent tracing.

Internally, it calls init_weave() which handles W&B login, server initialization, caching middleware setup, and integration patching via implicit_patch() and register_import_hook().

Usage

Import and call this function at the start of any application that requires Weave tracing. All subsequent @weave.op decorated function calls will be logged to the specified project.

Code Reference

Source Location

  • Repository: wandb/weave
  • File: weave/trace/api.py
  • Lines: L43-111

Signature

def init(
    project_name: str,
    *,
    settings: UserSettings | dict[str, Any] | None = None,
    autopatch_settings: AutopatchSettings | None = None,
    global_postprocess_inputs: PostprocessInputsFunc | None = None,
    global_postprocess_output: PostprocessOutputFunc | None = None,
    global_attributes: dict[str, Any] | None = None,
) -> WeaveClient:
    """Initialize weave tracking, logging to a wandb project.

    Logging is initialized globally, so you do not need to keep a reference
    to the return value of init.

    Args:
        project_name: The name of the Weights & Biases team and project
            to log to (e.g., "entity/project").
        settings: Configuration for the Weave client.
        autopatch_settings: (Deprecated) Configuration for autopatch integrations.
        global_postprocess_inputs: Function applied to all op inputs after logging.
        global_postprocess_output: Function applied to all op outputs after logging.
        global_attributes: Dictionary of attributes applied to all traces.

    Returns:
        A WeaveClient instance.
    """

Import

import weave

I/O Contract

Inputs

Name Type Required Description
project_name str Yes W&B team/project identifier (e.g., "entity/project")
settings dict | None No Client configuration settings
autopatch_settings None No Deprecated. Use explicit patching instead
global_postprocess_inputs None No Function applied to all op inputs
global_postprocess_output None No Function applied to all op outputs
global_attributes None No Attributes applied to all traces

Outputs

Name Type Description
return WeaveClient Authenticated client instance, also set globally
side_effect implicit_patch() Patches already-imported LLM libraries
side_effect register_import_hook() Installs import hook for future LLM library imports

Usage Examples

Basic Initialization

import weave

# Initialize with project name
client = weave.init("my-team/my-project")

# All @weave.op calls are now traced

With Global Postprocessing

import weave

def redact_inputs(inputs: dict) -> dict:
    """Remove sensitive fields before logging."""
    return {k: v for k, v in inputs.items() if k != "api_key"}

client = weave.init(
    "my-team/my-project",
    global_postprocess_inputs=redact_inputs,
    global_attributes={"environment": "production"},
)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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