Components
components ¶
Evolvable surface handlers for component mutation.
Currently contains the ComponentHandlerRegistry and built-in handlers for instruction, output_schema, and generate_content_config components.
Anticipated growth: custom handler registration, handler composition, component validation middleware.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ComponentHandlerRegistry | Registry for component handlers. TYPE: |
InstructionHandler | Handler for agent.instruction component. TYPE: |
OutputSchemaHandler | Handler for agent.output_schema component. TYPE: |
GenerateContentConfigHandler | Handler for LLM generation config. TYPE: |
component_handlers | Default registry instance. TYPE: |
get_handler | Convenience function for getting handlers. TYPE: |
register_handler | Convenience function for registering handlers. TYPE: |
MappingComponentHandler | Handler for one key of a caller-owned mapping. TYPE: |
register_mapping_components | Register a handler for every key of a mapping. TYPE: |
Examples:
Retrieve a handler for a specific component:
Register a custom handler:
from gepa_adk.adapters.components import register_handler
register_handler("my_component", MyCustomHandler())
See Also
gepa_adk.adapters: Parent adapter layer re-exports.gepa_adk.ports.component_handler: ComponentHandler protocol that handlers implement.gepa_adk.adapters.evolution: Adapters that use component handlers during evolution.
Notes
This package centralizes component handler registration for evolution surfaces.
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.
Notes
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/components/component_handlers.py
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 253 254 255 256 257 258 | |
__init__ ¶
Initialize empty registry.
Examples:
Notes
Creates an empty internal dict for handler storage.
Source code in src/gepa_adk/adapters/components/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())
Notes
Overwrites existing handler if name already registered. Logs a debug message on replacement.
Source code in src/gepa_adk/adapters/components/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/components/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:
Notes
Outputs False for empty/None names (no ValueError). This allows safe checking without exception handling.
Source code in src/gepa_adk/adapters/components/component_handlers.py
names ¶
Return sorted list of registered handler names.
| RETURNS | DESCRIPTION |
|---|---|
list[str] | Sorted list of component names with registered handlers. |
Examples:
Notes
Output is sorted alphabetically for consistent error messages and validation feedback.
Source code in src/gepa_adk/adapters/components/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)
Notes
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/components/component_handlers.py
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 465 466 467 468 469 470 471 472 473 | |
serialize ¶
Extract generate_content_config from agent as YAML.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to read. 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/components/component_handlers.py
apply ¶
Apply new generate_content_config to agent, return original.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to modify. TYPE: |
value | YAML string defining the new config parameters. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The original GenerateContentConfig (or None), whether or not the |
Any | new config was applied. |
Examples:
original = handler.apply(agent, "temperature: 0.5")
# agent.generate_content_config.temperature is now 0.5
Notes
On deserialization or validation failure, logs warning and keeps original config. Never raises exceptions.
Source code in src/gepa_adk/adapters/components/component_handlers.py
restore ¶
Restore the generate_content_config that apply() returned.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to restore. TYPE: |
original | The GenerateContentConfig apply() returned, or None. TYPE: |
Examples:
Source code in src/gepa_adk/adapters/components/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"
Notes
All state is stored in the agent object - handler is stateless. No instance attributes are maintained.
Source code in src/gepa_adk/adapters/components/component_handlers.py
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 346 | |
serialize ¶
Extract instruction from agent as string.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to read. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The agent's instruction converted with |
str | Returns empty string if instruction is None. |
Examples:
Source code in src/gepa_adk/adapters/components/component_handlers.py
apply ¶
Apply new instruction to agent, return original.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to modify. TYPE: |
value | The new instruction string. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The original instruction as serialize() renders it (empty string |
str | when it was None). |
Examples:
Source code in src/gepa_adk/adapters/components/component_handlers.py
restore ¶
Restore the instruction that apply() returned.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to restore. TYPE: |
original | The instruction apply() returned. An agent whose instruction was None comes back with an empty string. TYPE: |
Examples:
Source code in src/gepa_adk/adapters/components/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)
Notes
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/components/component_handlers.py
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 634 635 636 637 638 639 640 641 642 643 644 | |
__init__ ¶
Initialize handler with no constraints.
Examples:
Source code in src/gepa_adk/adapters/components/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
Notes
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/components/component_handlers.py
serialize ¶
Extract output schema from agent as Python source.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to read. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | Python source code defining the schema class. Returns an empty |
str | string if output_schema is None or its source cannot be read |
str | ( |
Examples:
Source code in src/gepa_adk/adapters/components/component_handlers.py
apply ¶
Apply new output schema to agent, return original.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to modify. TYPE: |
value | Python source code defining the new schema. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The original output_schema (class or None), whether or not the new |
Any | schema was applied. |
Examples:
Notes
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/components/component_handlers.py
restore ¶
Restore the output schema that apply() returned.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The agent to restore. TYPE: |
original | The schema class apply() returned, or None. TYPE: |
Examples:
Source code in src/gepa_adk/adapters/components/component_handlers.py
MappingComponentHandler ¶
Handler for one string value in a mapping the caller owns.
Serializes, applies and restores mapping[key]. The agent argument of each method is ignored, because the value does not live on the agent.
| ATTRIBUTE | DESCRIPTION |
|---|---|
mapping | The caller's mapping, held by reference so writes are visible to its other readers. TYPE: |
key | The mapping key this handler evolves. TYPE: |
Examples:
prompts = {"greeting": "hello"}
handler = MappingComponentHandler(prompts, key="greeting")
original = handler.apply(None, "hi") # prompts["greeting"] == "hi"
handler.restore(None, original) # prompts["greeting"] == "hello"
Notes
The handler keeps a reference to the mapping, not a copy. Replacing the mapping object elsewhere detaches the handler from it.
Source code in src/gepa_adk/adapters/components/mapping_handler.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 | |
__init__ ¶
Bind the handler to one key of the caller's mapping.
| PARAMETER | DESCRIPTION |
|---|---|
mapping | The caller-owned mapping holding the prompt text. TYPE: |
key | The key whose value this handler evolves. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If |
TypeError | If |
Examples:
Source code in src/gepa_adk/adapters/components/mapping_handler.py
serialize ¶
Return the current text of the mapping key.
| PARAMETER | DESCRIPTION |
|---|---|
agent | Ignored; may be TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The value of |
str | has removed the key or replaced its value with non-text since |
str | registration. |
Examples:
Notes
Never raises, per the ComponentHandler contract. A missing or non-text value is logged as mapping_handler.key_missing.
Source code in src/gepa_adk/adapters/components/mapping_handler.py
apply ¶
Write new text to the mapping key and return the previous text.
| PARAMETER | DESCRIPTION |
|---|---|
agent | Ignored; may be TYPE: |
value | The new text for the key. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The text the key held before this call. |
Examples:
Source code in src/gepa_adk/adapters/components/mapping_handler.py
restore ¶
Write the original text back to the mapping key.
| PARAMETER | DESCRIPTION |
|---|---|
agent | Ignored; may be TYPE: |
original | The text returned by TYPE: |
Examples:
Source code in src/gepa_adk/adapters/components/mapping_handler.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
- gepa_adk.adapters.components.component_handlers.ComponentHandlerRegistry.get: Underlying registry method.
Notes
Shortcut for component_handlers.get(name).
Source code in src/gepa_adk/adapters/components/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
- gepa_adk.adapters.components.component_handlers.ComponentHandlerRegistry.register: Underlying registry method.
Notes
Shortcut for component_handlers.register(name, handler).
Source code in src/gepa_adk/adapters/components/component_handlers.py
register_mapping_components ¶
register_mapping_components(
mapping: MutableMapping[str, str],
*,
registry: ComponentHandlerRegistry | None = None,
) -> list[str]
Register a handler for every key of a mapping.
Each key becomes a component name backed by a MappingComponentHandler. Every key is validated before any handler is registered, so a rejected mapping leaves the registry unchanged.
| PARAMETER | DESCRIPTION |
|---|---|
mapping | The caller-owned mapping of prompt strings. TYPE: |
registry | The registry to register into. Defaults to the default TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[str] | The registered component names, in mapping order. |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If the mapping is empty, a key is not a Python identifier, or a key equals |
TypeError | If a value is not a |
Examples:
prompts = {"greeting": "hello", "farewell": "bye"}
names = register_mapping_components(prompts)
# names == ["greeting", "farewell"]
See Also
MappingComponentHandler: The handler registered for each key.
Notes
Registering a name that already has a handler replaces it.