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 Implicit Patch

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

Overview

Concrete tool for automatically patching LLM provider SDKs at initialization provided by the Wandb Weave library.

Description

implicit_patch() scans sys.modules for known integration module names and applies tracing patches. register_import_hook() installs a WeaveImportHook (MetaPathFinder) at sys.meta_path[0] that intercepts future imports and patches them via PatchingLoader.

The INTEGRATION_MODULE_MAPPING dict maps 25+ module names to their patch functions, supporting OpenAI, Anthropic, Mistral, Cohere, Google GenAI, Groq, LiteLLM, HuggingFace, LangChain, LlamaIndex, DSPy, CrewAI, and more.

Usage

These functions are called automatically by weave.init(). Call unregister_import_hook() to disable automatic patching of future imports.

Code Reference

Source Location

  • Repository: wandb/weave
  • File: weave/integrations/patch.py
  • Lines: L438-496 (implicit_patch, register_import_hook, unregister_import_hook)
  • Lines: L306-334 (INTEGRATION_MODULE_MAPPING)
  • Lines: L337-419 (WeaveImportHook, PatchingLoader)

Signature

def implicit_patch() -> None:
    """Check sys.modules and patch any already-imported integrations.
    Called during weave.init() to enable implicit patching.
    """

def register_import_hook() -> None:
    """Register import hook to auto-patch integrations imported after weave.init().
    Respects the implicitly_patch_integrations setting.
    """

def unregister_import_hook() -> None:
    """Unregister the import hook (for testing or cleanup)."""

class WeaveImportHook(MetaPathFinder):
    """Import hook that auto-patches supported integrations when imported."""
    def find_spec(self, fullname, path, target=None) -> None | ModuleSpec: ...

class PatchingLoader:
    """Loader wrapper that patches an integration after it's imported."""
    def __init__(self, original_loader, module_name: str) -> None: ...
    def exec_module(self, module): ...

Import

from weave.integrations.patch import implicit_patch, register_import_hook, unregister_import_hook

I/O Contract

Inputs

Name Type Required Description
(none) Reads from sys.modules and INTEGRATION_MODULE_MAPPING

Outputs

Name Type Description
side_effect patch Already-imported LLM libraries are monkey-patched
side_effect hook WeaveImportHook installed at sys.meta_path[0]

Usage Examples

Automatic (via weave.init)

import openai  # Already imported before init
import weave

# implicit_patch() will detect openai in sys.modules and patch it
# register_import_hook() will catch any future imports
client = weave.init("my-team/my-project")

# OpenAI calls are now automatically traced
response = openai.chat.completions.create(
    model="gpt-4", messages=[{"role": "user", "content": "Hello"}]
)

Related Pages

Implements Principle

Requires Environment

Page Connections

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