Skip to content

Commit adaa6c2

Browse files
committed
doc: add lazy loading in GCP Secret Manager
1 parent 52ee47f commit adaa6c2

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/index.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,74 @@ For nested models, Secret Manager supports the `env_nested_delimiter` setting as
23922392

23932393
For more details on creating and managing secrets in Google Cloud Secret Manager, see the [official Google Cloud documentation](https://cloud.google.com/secret-manager/docs).
23942394

2395+
### Lazy Loading
2396+
2397+
Lazy loading defers field value resolution until fields are actually accessed, rather than eagerly fetching all values during settings initialization. This is particularly useful when working with Google Cloud Secret Manager where each field access triggers an API call, avoiding unnecessary network requests for fields that may never be used.
2398+
2399+
2400+
#### Basic Usage
2401+
2402+
You can enable lazy loading for Google Cloud Secret Manager via the `lazy_load` parameter when configuring `GoogleSecretManagerSettingsSource`:
2403+
2404+
```py
2405+
import os
2406+
2407+
from pydantic_settings import (
2408+
BaseSettings,
2409+
GoogleSecretManagerSettingsSource,
2410+
PydanticBaseSettingsSource,
2411+
)
2412+
2413+
2414+
class Settings(BaseSettings):
2415+
secret1: str = ''
2416+
secret2: str = ''
2417+
2418+
@classmethod
2419+
def settings_customise_sources(
2420+
cls,
2421+
settings_cls: type[BaseSettings],
2422+
init_settings: PydanticBaseSettingsSource,
2423+
env_settings: PydanticBaseSettingsSource,
2424+
dotenv_settings: PydanticBaseSettingsSource,
2425+
file_secret_settings: PydanticBaseSettingsSource,
2426+
) -> tuple[PydanticBaseSettingsSource, ...]:
2427+
gcp_settings = GoogleSecretManagerSettingsSource(
2428+
settings_cls,
2429+
project_id=os.environ.get('GCP_PROJECT_ID', 'my-project'),
2430+
lazy_load=True,
2431+
)
2432+
return (
2433+
init_settings,
2434+
env_settings,
2435+
dotenv_settings,
2436+
gcp_settings,
2437+
file_secret_settings,
2438+
)
2439+
```
2440+
2441+
When initializing `Settings()` the secrets will not be fetched. When accessing a secret for the first time, for example `secret1`, an API call will be made to fetch that secret, and will then be catched. Next access to that same secret will not trigger an API call, but accessing another one will. Operations that require all secrets, like `model_dump` triggers the fetching of all secrets.
2442+
2443+
#### Behavior and Caching
2444+
2445+
When lazy loading is enabled:
2446+
2447+
1. **Initialization**: Settings are created with minimal overhead. Sources return empty dictionaries instead of eagerly fetching all values.
2448+
2449+
2. **First Access**: When you access a field for the first time (e.g., `settings.api_key`), the value is fetched from the configured source and cached in memory.
2450+
2451+
3. **Subsequent Access**: Accessing the same field again returns the cached value without making another API call.
2452+
2453+
4. **All Fields**: Iteration over all fields (via `model_dump()`, etc.) will trigger resolution of all fields at once.
2454+
2455+
***When to use lazy loading:**
2456+
2457+
* Your settings have many fields but your application only uses a subset of them
2458+
* You want to reduce initialization time and API call costs
2459+
* Network latency to GCP Secret Manager is significant
2460+
2461+
2462+
23952463
## Other settings source
23962464

23972465
Other settings sources are available for common configuration files:

0 commit comments

Comments
 (0)