Config utils
config_utils ¶
Utilities for GenerateContentConfig serialization, deserialization, and validation.
This module provides functions for converting GenerateContentConfig objects to and from YAML format, with support for partial config merging and parameter validation.
| ATTRIBUTE | DESCRIPTION |
|---|---|
EVOLVABLE_PARAMS | Parameter descriptions for evolvable config fields. TYPE: |
serialize_generate_config | Convert config to YAML with descriptions. TYPE: |
deserialize_generate_config | Parse YAML to config, with merge support. TYPE: |
validate_generate_config | Validate config dict against constraints. TYPE: |
Examples:
Serialize and deserialize a config:
from google.genai.types import GenerateContentConfig
from gepa_adk.utils.config_utils import (
serialize_generate_config,
deserialize_generate_config,
validate_generate_config,
)
config = GenerateContentConfig(temperature=0.7, top_p=0.9)
yaml_text = serialize_generate_config(config)
# Parse with merge
new_config = deserialize_generate_config("temperature: 0.5", existing=config)
# new_config.temperature == 0.5, new_config.top_p == 0.9 (preserved)
# Validate before applying
errors = validate_generate_config({"temperature": 3.0})
# errors == ["temperature must be 0.0-2.0, got 3.0"]
Note
This module is in the utils/ layer and may import from adapters/ types (google.genai.types) following hexagonal architecture.
serialize_generate_config ¶
Convert GenerateContentConfig to YAML string with parameter descriptions.
Serializes only the evolvable parameters (temperature, top_p, top_k, max_output_tokens, presence_penalty, frequency_penalty) with YAML comments describing each parameter.
| PARAMETER | DESCRIPTION |
|---|---|
config | The GenerateContentConfig instance to serialize. Returns empty string if None. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | YAML string with parameter descriptions as comments. |
str | Empty string if config is None or has no evolvable parameters set. |
Examples:
config = GenerateContentConfig(temperature=0.7, top_p=0.9)
yaml_text = serialize_generate_config(config)
# yaml_text contains:
# # temperature: Controls randomness (0.0=deterministic, 2.0=creative)
# temperature: 0.7
# # top_p: Nucleus sampling threshold (0.0-1.0)
# top_p: 0.9
Note
Output is parseable by yaml.safe_load() and includes only evolvable parameters that have non-None values.
Source code in src/gepa_adk/utils/config_utils.py
deserialize_generate_config ¶
deserialize_generate_config(
yaml_text: str,
existing: "GenerateContentConfig | None" = None,
) -> "GenerateContentConfig"
Parse YAML text into GenerateContentConfig, optionally merging with existing.
Parses the YAML text and creates a new GenerateContentConfig. If an existing config is provided, unspecified parameters retain existing values (merge).
| PARAMETER | DESCRIPTION |
|---|---|
yaml_text | YAML string to parse. May be empty. TYPE: |
existing | Optional existing config to merge with. If provided, unspecified parameters retain existing values. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
'GenerateContentConfig' | New GenerateContentConfig instance. |
| RAISES | DESCRIPTION |
|---|---|
ConfigValidationError | If yaml_text is malformed YAML. |
Examples:
# Parse standalone
config = deserialize_generate_config("temperature: 0.5")
assert config.temperature == 0.5
# Merge with existing
existing = GenerateContentConfig(temperature=0.7, top_p=0.9)
merged = deserialize_generate_config("temperature: 0.5", existing)
assert merged.temperature == 0.5
assert merged.top_p == 0.9 # Preserved from existing
Note
Does NOT validate parameter constraints - use validate_generate_config() separately. This allows inspection of invalid values before rejection.
Source code in src/gepa_adk/utils/config_utils.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
validate_generate_config ¶
Validate config dict against known parameter constraints.
Checks each parameter value against its defined constraints: - temperature: 0.0 to 2.0 - top_p: 0.0 to 1.0 - top_k: > 0 - max_output_tokens: > 0 - presence_penalty: -2.0 to 2.0 - frequency_penalty: -2.0 to 2.0
Unknown parameters trigger a warning log but do NOT cause errors (they may be model-specific parameters).
| PARAMETER | DESCRIPTION |
|---|---|
config_dict | Dictionary of parameter name to value. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[str] | List of validation error messages. Empty list means valid. |
Examples:
# Valid config
errors = validate_generate_config({"temperature": 0.7, "top_p": 0.9})
assert errors == []
# Invalid config
errors = validate_generate_config({"temperature": 3.0})
assert len(errors) == 1
assert "temperature" in errors[0]
# Unknown param - no error, just warning
errors = validate_generate_config({"unknown_param": 42})
assert errors == [] # Warning logged, but no error
Note
Only validates known evolvable parameters. Unknown parameters are logged as warnings but accepted (may be model-specific).
Source code in src/gepa_adk/utils/config_utils.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |