diff --git a/docs/md_v2/api/parameters.md b/docs/md_v2/api/parameters.md index 9d907516..f1ebeb73 100644 --- a/docs/md_v2/api/parameters.md +++ b/docs/md_v2/api/parameters.md @@ -367,6 +367,55 @@ no_cache_config = base_config.clone( The `clone()` method is particularly useful when you need slightly different configurations for different use cases, without modifying the original config. +### Class-Level Defaults (`set_defaults` / `get_defaults` / `reset_defaults`) + +Both config classes support class-level default overrides. When deploying in a server or cloud context, this eliminates the need to pass the same parameters at every call site. + +**Resolution order:** explicit arg > class-level default > hardcoded default + +```python +from crawl4ai import BrowserConfig, CrawlerRunConfig + +# Set once at application startup +BrowserConfig.set_defaults( + cache_cdp_connection=True, + cdp_close_delay=0, + create_isolated_context=True, +) +CrawlerRunConfig.set_defaults(verbose=False) + +# All new instances inherit the class defaults +cfg1 = BrowserConfig(cdp_url="ws://localhost:9222") +# → cache_cdp_connection=True, cdp_close_delay=0 + +cfg2 = BrowserConfig(cdp_url="ws://localhost:9222", cache_cdp_connection=False) +# → cache_cdp_connection=False (explicit value wins) + +# Inspect current defaults +BrowserConfig.get_defaults() +# → {"cache_cdp_connection": True, "cdp_close_delay": 0, "create_isolated_context": True} + +# Remove a single default +BrowserConfig.reset_defaults("cdp_close_delay") + +# Remove all defaults +BrowserConfig.reset_defaults() +``` + +**API Reference:** + +| Method | Signature | Description | +|--------|-----------|-------------| +| `set_defaults` | `set_defaults(**kwargs)` | Set class-level defaults for new instances. Raises `ValueError` if any key is not a valid `__init__` parameter. | +| `get_defaults` | `get_defaults() → dict` | Return a deep copy of the current class-level defaults. | +| `reset_defaults` | `reset_defaults(*names)` | With no args, clears all defaults. With args, removes only the named defaults. | + +**Notes:** +- Defaults are independent per class — `BrowserConfig.set_defaults()` has no effect on `CrawlerRunConfig`. +- Mutable values (lists, dicts) are deep-copied on storage and on each instance creation, so instances do not share objects. +- `clone()`, `dump()`/`load()`, and `from_kwargs()` all work correctly with class defaults — serialized data is self-contained and independent of the current class defaults. +- Defaults are stored in memory for the lifetime of the process. They are not persisted to disk. + ## 2.3 Example Usage ```python @@ -467,4 +516,8 @@ stream_cfg = run_cfg.clone( stream=True, cache_mode=CacheMode.BYPASS ) + +# Or set project-wide defaults once at startup +BrowserConfig.set_defaults(headless=True, text_mode=True) +CrawlerRunConfig.set_defaults(cache_mode=CacheMode.BYPASS) ``` diff --git a/docs/md_v2/core/browser-crawler-config.md b/docs/md_v2/core/browser-crawler-config.md index a0e59fd0..13f43262 100644 --- a/docs/md_v2/core/browser-crawler-config.md +++ b/docs/md_v2/core/browser-crawler-config.md @@ -136,6 +136,41 @@ debug_browser = base_browser.clone( ) ``` +### Class-Level Defaults + +Both `BrowserConfig` and `CrawlerRunConfig` support **class-level default overrides** via `set_defaults()`. This is useful in server/cloud deployments where every config instance needs the same base settings — set them once at startup instead of repeating at every call site. + +```python +from crawl4ai import BrowserConfig, CrawlerRunConfig + +# At application startup — one time +BrowserConfig.set_defaults( + cache_cdp_connection=True, + cdp_close_delay=0, + create_isolated_context=True, +) +CrawlerRunConfig.set_defaults(verbose=False) + +# Every new instance automatically inherits those defaults +cfg = BrowserConfig(cdp_url="ws://localhost:9222") +# → cache_cdp_connection=True, cdp_close_delay=0, create_isolated_context=True + +# Explicit values still win +cfg = BrowserConfig(cdp_url="ws://localhost:9222", cache_cdp_connection=False) +# → cache_cdp_connection=False (explicit overrides the class default) +``` + +**Available methods** (on both `BrowserConfig` and `CrawlerRunConfig`): + +| Method | Description | +|--------|-------------| +| `set_defaults(**kwargs)` | Set class-level defaults. Invalid parameter names raise `ValueError`. | +| `get_defaults()` | Return a copy of the current class-level defaults. | +| `reset_defaults()` | Clear all class-level defaults. | +| `reset_defaults("param1", "param2")` | Clear only the named defaults. | + +> **Note:** Class defaults are independent per class — `BrowserConfig.set_defaults()` does not affect `CrawlerRunConfig`, and vice versa. Defaults are stored in memory and apply for the lifetime of the process. + **Minimal Example**: ```python