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 Patcher Undo

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

Overview

Concrete tool for undoing integration patches and restoring original SDK methods provided by the Wandb Weave library.

Description

The Patcher hierarchy (Patcher, NoOpPatcher, MultiPatcher, SymbolPatcher) provides undo_patch() methods. SymbolPatcher.undo_patch() restores the original method via setattr. MultiPatcher.undo_patch() iterates all sub-patchers. unregister_import_hook() removes the WeaveImportHook from sys.meta_path.

Usage

Call patcher.undo_patch() on a patcher instance to reverse its patches. Call unregister_import_hook() to stop auto-patching future imports.

Code Reference

Source Location

  • Repository: wandb/weave
  • File: weave/integrations/patcher.py
  • Lines: L13-127
  • File: weave/integrations/patch.py
  • Lines: L487-496 (unregister_import_hook)

Signature

class Patcher:
    def attempt_patch(self) -> bool: ...
    def undo_patch(self) -> bool: ...

class MultiPatcher(Patcher):
    def __init__(self, patchers: Sequence[Patcher]) -> None: ...
    def undo_patch(self) -> bool:
        """Undo all sub-patcher patches."""

class SymbolPatcher(Patcher):
    def __init__(self, get_base_symbol: Callable, attribute_name: str, make_new_value: Callable) -> None: ...
    def undo_patch(self) -> bool:
        """Restore original value via setattr."""

Import

from weave.integrations.patcher import Patcher, MultiPatcher, SymbolPatcher
from weave.integrations.patch import unregister_import_hook

I/O Contract

Inputs

Name Type Required Description
(none) Operates on patcher's stored state

Outputs

Name Type Description
return bool True if all patches successfully undone
side_effect restore Original provider SDK methods restored

Usage Examples

Undo Patching

from weave.integrations.openai.openai_sdk import get_openai_patcher
from weave.integrations.patch import unregister_import_hook

# Get the patcher
patcher = get_openai_patcher()
patcher.attempt_patch()

# ... use OpenAI with tracing ...

# Undo patches
patcher.undo_patch()
unregister_import_hook()

Related Pages

Implements Principle

Requires Environment

Page Connections

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