Skip to content

Commit f552064

Browse files
committed
Update docs
1 parent 551545e commit f552064

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

docs/index.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,58 @@ print(Settings().model_dump())
324324
`env_nested_delimiter` can be configured via the `model_config` as shown above, or via the
325325
`_env_nested_delimiter` keyword argument on instantiation.
326326

327+
By default environment variables are split by `env_nested_delimiter` into arbitrarily deep nested fields. You can limit
328+
the depth of the nested fields with the `env_nested_depth` config setting. A common use case this is particularly useful
329+
is for two-level deep settings, where the `env_nested_delimiter` (usually a single `_`) may be a substring of model
330+
field names. For example:
331+
332+
```bash
333+
# your environment
334+
export GENERATION_LLM_PROVIDER='anthropic'
335+
export GENERATION_LLM_API_KEY='your-api-key'
336+
export GENERATION_LLM_API_VERSION='2024-03-15'
337+
```
338+
339+
You could load them into the following settings model:
340+
341+
```py
342+
from pydantic import BaseModel
343+
344+
from pydantic_settings import BaseSettings, SettingsConfigDict
345+
346+
347+
class LLMConfig(BaseModel):
348+
provider: str = 'openai'
349+
api_key: str
350+
api_type: str = 'azure'
351+
api_version: str = '2023-03-15-preview'
352+
353+
354+
class GenerationConfig(BaseSettings):
355+
model_config = SettingsConfigDict(
356+
env_nested_delimiter='_', env_nested_depth=1, env_prefix='GENERATION_'
357+
)
358+
359+
llm: LLMConfig
360+
...
361+
362+
363+
print(GenerationConfig().model_dump())
364+
"""
365+
{
366+
'llm': {
367+
'provider': 'anthropic',
368+
'api_key': 'your-api-key',
369+
'api_type': 'azure',
370+
'api_version': '2024-03-15',
371+
}
372+
}
373+
"""
374+
```
375+
376+
Without `env_nested_depth=1` set, `GENERATION_LLM_API_KEY` would be parsed as `llm.api.key` instead of `llm.api_key`
377+
and it would raise a `ValidationError`.
378+
327379
Nested environment variables take precedence over the top-level environment variable JSON
328380
(e.g. in the example above, `SUB_MODEL__V2` trumps `SUB_MODEL`).
329381

0 commit comments

Comments
 (0)