Component handlers
component_handlers ¶
Component handler implementations and registry.
This module provides the ComponentHandlerRegistry for managing component handlers, built-in handlers for instruction and output_schema, and convenience functions for accessing the default registry.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ComponentHandlerRegistry | Registry for component handlers. TYPE: |
InstructionHandler | Handler for agent.instruction component. TYPE: |
OutputSchemaHandler | Handler for agent.output_schema component. TYPE: |
component_handlers | Default registry instance. TYPE: |
get_handler | Get handler from default registry. TYPE: |
register_handler | Register handler in default registry. TYPE: |
Examples:
Use built-in handlers:
from gepa_adk.adapters import get_handler
handler = get_handler("instruction")
original = handler.apply(agent, "New instruction")
# ... evaluate ...
handler.restore(agent, original)
Register a custom handler:
from gepa_adk.adapters import register_handler
class MyHandler:
def serialize(self, agent):
return str(agent.my_attr)
def apply(self, agent, value):
original = agent.my_attr
agent.my_attr = value
return original
def restore(self, agent, original):
agent.my_attr = original
register_handler("my_component", MyHandler())
See Also
- [
gepa_adk.ports.component_handler]: ComponentHandler protocol. - [
gepa_adk.adapters.adk_adapter]: Usage in ADKAdapter._apply_candidate().
Note
This module follows hexagonal architecture - it imports the protocol from ports/ and implements concrete handlers in adapters/.
ComponentHandlerRegistry ¶
Registry for component handlers with O(1) lookup.
Stores component handlers keyed by component name, providing registration, lookup, and existence checking operations.
| ATTRIBUTE | DESCRIPTION |
|---|---|
_handlers | Internal dict mapping component names to handlers. TYPE: |
Examples:
Create and use a registry:
registry = ComponentHandlerRegistry()
registry.register("instruction", InstructionHandler())
handler = registry.get("instruction")
See Also
get_handler(): Convenience function for default registry.
Note
A default registry instance is available as component_handlers module variable, with convenience functions get_handler() and register_handler().
Source code in src/gepa_adk/adapters/component_handlers.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 | |
__init__ ¶
Initialize empty registry.
Examples:
Note
Creates an empty internal dict for handler storage.
Source code in src/gepa_adk/adapters/component_handlers.py
register ¶
register(name: str, handler: ComponentHandler) -> None
Register a handler for a component name.
| PARAMETER | DESCRIPTION |
|---|---|
name | Component name (e.g., "instruction", "output_schema"). Must be a non-empty string. TYPE: |
handler | Handler implementing ComponentHandler protocol. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If name is empty or None. |
TypeError | If handler doesn't implement ComponentHandler protocol. |
Examples:
registry.register("instruction", InstructionHandler())
registry.register("output_schema", OutputSchemaHandler())
Note
Overwrites existing handler if name already registered. Logs a debug message on replacement.
Source code in src/gepa_adk/adapters/component_handlers.py
get ¶
get(name: str) -> ComponentHandler
Retrieve handler for component name.
| PARAMETER | DESCRIPTION |
|---|---|
name | Component name to look up. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ComponentHandler | The registered ComponentHandler. |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If name is empty or None. |
KeyError | If no handler registered for name. |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
has ¶
Check if handler exists for component name.
| PARAMETER | DESCRIPTION |
|---|---|
name | Component name to check. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | True if handler registered, False otherwise. |
Examples:
Note
Outputs False for empty/None names (no ValueError). This allows safe checking without exception handling.
Source code in src/gepa_adk/adapters/component_handlers.py
names ¶
Return sorted list of registered handler names.
| RETURNS | DESCRIPTION |
|---|---|
list[str] | Sorted list of component names with registered handlers. |
Examples:
Note
Output is sorted alphabetically for consistent error messages and validation feedback.
Source code in src/gepa_adk/adapters/component_handlers.py
InstructionHandler ¶
Handler for agent.instruction component.
Manages serialization, application, and restoration of the agent's instruction (system prompt) during evolution.
Examples:
handler = InstructionHandler()
original = handler.serialize(agent) # "Be helpful"
handler.apply(agent, "Be concise")
# ... evaluate ...
handler.restore(agent, original) # Back to "Be helpful"
Note
All state is stored in the agent object - handler is stateless. No instance attributes are maintained.
Source code in src/gepa_adk/adapters/component_handlers.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 | |
serialize ¶
Extract instruction from agent as string.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The agent's instruction as string. |
str | Returns empty string if instruction is None. |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
apply ¶
Apply new instruction to agent, return original.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance to modify. TYPE: |
value | The new instruction string. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The original instruction value. |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
restore ¶
Restore original instruction to agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance to restore. TYPE: |
original | The original instruction value. TYPE: |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
GenerateContentConfigHandler ¶
Handler for agent.generate_content_config component.
Manages serialization, application, and restoration of the agent's LLM generation configuration during evolution.
Examples:
handler = GenerateContentConfigHandler()
original = handler.serialize(agent) # YAML string
handler.apply(agent, "temperature: 0.5")
# ... evaluate ...
handler.restore(agent, original_config)
Note
All state is stored in the agent object - handler is stateless. On invalid config, logs warning and keeps original.
Source code in src/gepa_adk/adapters/component_handlers.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
serialize ¶
Extract generate_content_config from agent as YAML.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | YAML string with parameter descriptions as comments. |
str | Returns empty string if generate_content_config is None. |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
apply ¶
Apply new generate_content_config to agent, return original.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance to modify. TYPE: |
value | YAML string defining the new config parameters. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The original GenerateContentConfig (or None). |
Examples:
original = handler.apply(agent, "temperature: 0.5")
# agent.generate_content_config.temperature is now 0.5
Note
On deserialization or validation failure, logs warning and keeps original config. Never raises exceptions.
Source code in src/gepa_adk/adapters/component_handlers.py
restore ¶
Restore original generate_content_config to agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance to restore. TYPE: |
original | The original GenerateContentConfig (or None). TYPE: |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
OutputSchemaHandler ¶
Handler for agent.output_schema component.
Manages serialization, application, and restoration of the agent's output schema (Pydantic model) during evolution.
| ATTRIBUTE | DESCRIPTION |
|---|---|
_constraints | Optional SchemaConstraints for field preservation. TYPE: |
Examples:
handler = OutputSchemaHandler()
handler.set_constraints(SchemaConstraints(required_fields=("score",)))
original_schema = handler.apply(agent, new_schema_text)
# ... evaluate ...
handler.restore(agent, original_schema)
Note
Applies serialize_pydantic_schema and deserialize_schema utilities. On invalid schema text, keeps original and logs warning. When constraints are set, validates proposed schemas before applying.
Source code in src/gepa_adk/adapters/component_handlers.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
__init__ ¶
Initialize handler with no constraints.
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
set_constraints ¶
set_constraints(
constraints: SchemaConstraints | None,
) -> None
Set schema constraints for field preservation.
| PARAMETER | DESCRIPTION |
|---|---|
constraints | SchemaConstraints specifying required fields and type preservation rules. Pass None to clear constraints. TYPE: |
Examples:
handler.set_constraints(SchemaConstraints(required_fields=("score",)))
handler.set_constraints(None) # Clear constraints
Note
Once set, constraints are checked during apply() - proposed schemas that violate constraints will be rejected and the original kept.
Source code in src/gepa_adk/adapters/component_handlers.py
serialize ¶
Extract output schema from agent as Python source.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | Python source code defining the schema class. |
str | Returns empty string if output_schema is None. |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
apply ¶
Apply new output schema to agent, return original.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance to modify. TYPE: |
value | Python source code defining the new schema. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The original output_schema (class or None). |
Examples:
Note
On deserialization failure, logs warning and keeps original. On constraint violation, keeps original. Never raises exceptions - graceful degradation.
Source code in src/gepa_adk/adapters/component_handlers.py
restore ¶
Restore original output schema to agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The LlmAgent instance to restore. TYPE: |
original | The original output_schema (class or None). TYPE: |
Examples:
Source code in src/gepa_adk/adapters/component_handlers.py
get_handler ¶
get_handler(name: str) -> ComponentHandler
Get handler from default registry.
| PARAMETER | DESCRIPTION |
|---|---|
name | Component name to look up. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ComponentHandler | The registered ComponentHandler. |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If name is empty or None. |
KeyError | If no handler registered for name. |
Examples:
See Also
- [
ComponentHandlerRegistry.get()]: Underlying registry method.
Note
Shortcut for component_handlers.get(name).
Source code in src/gepa_adk/adapters/component_handlers.py
register_handler ¶
register_handler(
name: str, handler: ComponentHandler
) -> None
Register handler in default registry.
| PARAMETER | DESCRIPTION |
|---|---|
name | Component name to register. TYPE: |
handler | Handler implementing ComponentHandler protocol. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If name is empty or None. |
TypeError | If handler doesn't implement ComponentHandler protocol. |
Examples:
See Also
- [
ComponentHandlerRegistry.register()]: Underlying registry method.
Note
Shortcut for component_handlers.register(name, handler).