Adk adapter
adk_adapter ¶
ADK adapter implementation for AsyncGEPAAdapter protocol.
This module provides the concrete implementation of AsyncGEPAAdapter for Google ADK agents, enabling evolutionary optimization of ADK agent instructions.
Terminology
- component: An evolvable unit with a name and text (e.g., instruction)
- component_text: The text content of a component being evolved
- trial: One performance record {input, output, feedback, trajectory}
- trials: Collection of trial records for reflection
- feedback: Critic evaluation {score, feedback_text, feedback_*} (stochastic)
- trajectory: Execution record {tool_calls, tokens, error} (deterministic)
- proposed_component_text: The improved text for the same component
Note
This adapter bridges GEPA's evaluation patterns to ADK's agent/runner architecture, handling instruction overrides, trace capture, and session management per ADK conventions.
ADKAdapter ¶
ADK implementation of AsyncGEPAAdapter protocol.
Bridges GEPA evaluation patterns to Google ADK's agent/runner architecture, enabling evolutionary optimization of ADK agents through instruction mutation and reflective learning.
| ATTRIBUTE | DESCRIPTION |
|---|---|
agent | The ADK LlmAgent to evaluate with different candidate instructions. TYPE: |
scorer | Scoring implementation for evaluating agent outputs. TYPE: |
max_concurrent_evals | Maximum number of concurrent evaluations to run in parallel. TYPE: |
trajectory_config | Configuration for trajectory extraction behavior (redaction, truncation, feature selection). TYPE: |
_session_service | Session service for managing agent state isolation. TYPE: |
_app_name | Application name used for session management. TYPE: |
_proposer | Mutation proposer for generating improved instructions via LLM reflection. |
_logger | Bound logger with adapter context for structured logging. TYPE: |
Examples:
Basic adapter setup:
from google.adk.agents import LlmAgent
from gepa_adk.adapters import ADKAdapter
from gepa_adk.adapters.agent_executor import AgentExecutor
agent = LlmAgent(
name="helper",
model="gemini-2.5-flash",
instruction="Be helpful and concise",
)
scorer = MyScorer() # Implements Scorer protocol
executor = AgentExecutor()
adapter = ADKAdapter(agent, scorer, executor)
# Evaluate with candidate instruction
batch = [{"input": "What is 2+2?", "expected": "4"}]
candidate = {"instruction": "Be very precise with math"}
result = await adapter.evaluate(batch, candidate)
Note
Adheres to AsyncGEPAAdapter[dict[str, Any], ADKTrajectory, str] protocol. All methods are async and follow ADK's async-first patterns.
Source code in src/gepa_adk/adapters/adk_adapter.py
55 56 57 58 59 60 61 62 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 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 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 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 465 466 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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | |
__init__ ¶
__init__(
agent: LlmAgent,
scorer: Scorer,
executor: AgentExecutorProtocol,
max_concurrent_evals: int = 5,
session_service: BaseSessionService | None = None,
app_name: str = "gepa_adk_eval",
trajectory_config: TrajectoryConfig | None = None,
proposer: AsyncReflectiveMutationProposer | None = None,
reflection_agent: LlmAgent | None = None,
reflection_output_field: str | None = None,
schema_constraints: SchemaConstraints | None = None,
video_service: VideoBlobServiceProtocol | None = None,
) -> None
Initialize the ADK adapter with agent and scorer.
| PARAMETER | DESCRIPTION |
|---|---|
agent | The ADK LlmAgent to evaluate with different instructions. TYPE: |
scorer | Scorer implementation for evaluating agent outputs. TYPE: |
executor | AgentExecutorProtocol implementation for unified agent execution. The executor handles session management and execution, enabling feature parity across all agent types. TYPE: |
max_concurrent_evals | Maximum number of concurrent evaluations to run in parallel. Must be at least 1. Defaults to 5. TYPE: |
session_service | Optional session service for state management. If None, creates an InMemorySessionService. TYPE: |
app_name | Application name for session identification. TYPE: |
trajectory_config | Configuration for trajectory extraction behavior. If None, uses TrajectoryConfig defaults (secure, all features enabled). TYPE: |
proposer | Optional mutation proposer for generating improved instructions via LLM reflection. If provided, takes precedence over reflection_agent. TYPE: |
reflection_agent | ADK LlmAgent to use for reflection operations. Either this or proposer must be provided. When provided, creates an ADK-based reflection function and passes it to a new proposer. TYPE: |
reflection_output_field | Field name to extract from structured output when reflection_agent has an output_schema. When the reflection agent returns structured output (dict), this specifies which field contains the proposed text. For schema evolution, use "class_definition" with a SchemaProposal output_schema. Only used when reflection_agent is provided. TYPE: |
schema_constraints | Optional SchemaConstraints for output_schema evolution. When provided, proposed schema mutations are validated against these constraints. Mutations that violate constraints (e.g., remove required fields) are rejected and the original schema is preserved. TYPE: |
video_service | Optional VideoBlobServiceProtocol for multimodal input support. When provided, enables processing of trainset examples with 'videos' field. If None, defaults to a new VideoBlobService instance. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
TypeError | If agent is not an LlmAgent instance. |
TypeError | If scorer does not satisfy Scorer protocol. |
TypeError | If reflection_agent is provided but not an LlmAgent instance. |
ValueError | If app_name is empty string or max_concurrent_evals < 1. |
ValueError | If neither proposer nor reflection_agent is provided. |
Examples:
Basic setup with reflection agent:
from gepa_adk.adapters.agent_executor import AgentExecutor
reflection_agent = LlmAgent(name="reflector", model="gemini-2.5-flash")
executor = AgentExecutor()
adapter = ADKAdapter(
agent, scorer, executor, reflection_agent=reflection_agent
)
With custom trajectory configuration:
config = TrajectoryConfig(
redact_sensitive=True,
max_string_length=5000,
)
executor = AgentExecutor()
adapter = ADKAdapter(
agent,
scorer,
executor,
reflection_agent=reflection_agent,
trajectory_config=config,
)
With shared session service:
from google.adk.sessions import InMemorySessionService
from gepa_adk.adapters.agent_executor import AgentExecutor
session_service = InMemorySessionService()
executor = AgentExecutor(session_service=session_service)
adapter = ADKAdapter(
agent,
scorer,
executor,
reflection_agent=reflection_agent,
session_service=session_service,
)
Note
Caches the agent's original instruction and restores it after each evaluation to ensure no side effects between evaluations.
Source code in src/gepa_adk/adapters/adk_adapter.py
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 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 | |
cleanup ¶
Clean up adapter resources and clear handler constraints.
Clears any schema constraints set on the OutputSchemaHandler to prevent constraint leakage between evolution runs. Should be called when the adapter is no longer needed.
Note
OutputSchemaHandler is a singleton, so constraints set during one evolution run could affect subsequent runs if not cleared.
Source code in src/gepa_adk/adapters/adk_adapter.py
evaluate async ¶
evaluate(
batch: list[dict[str, Any]],
candidate: dict[str, str],
capture_traces: bool = False,
) -> EvaluationBatch[ADKTrajectory, str]
Evaluate agent with candidate instruction over a batch of inputs.
| PARAMETER | DESCRIPTION |
|---|---|
batch | List of input examples, each with "input" key and optional "expected" key for scoring. TYPE: |
candidate | Component name to text mapping. If "instruction" key is present, it overrides the agent's instruction. TYPE: |
capture_traces | Whether to capture execution traces (tool calls, state deltas, token usage). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
EvaluationBatch[ADKTrajectory, str] | EvaluationBatch containing outputs, scores, and optional trajectories. |
Examples:
Basic evaluation without traces:
batch = [
{"input": "What is 2+2?", "expected": "4"},
{"input": "Capital of France?", "expected": "Paris"},
]
candidate = {"instruction": "Be concise"}
result = await adapter.evaluate(batch, candidate)
assert len(result.outputs) == 2
assert len(result.scores) == 2
With trace capture:
result = await adapter.evaluate(batch, candidate, capture_traces=True)
assert result.trajectories is not None
assert len(result.trajectories) == len(batch)
Note
Original instruction is restored after evaluation completes, even if an exception occurs during evaluation. Evaluations run in parallel with concurrency controlled by max_concurrent_evals parameter. Results maintain input order despite parallel execution.
Source code in src/gepa_adk/adapters/adk_adapter.py
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 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 | |
make_reflective_dataset async ¶
make_reflective_dataset(
candidate: dict[str, str],
eval_batch: EvaluationBatch[ADKTrajectory, str],
components_to_update: list[str],
) -> Mapping[str, Sequence[Mapping[str, Any]]]
Build trials from evaluation results for reflection.
Terminology
- trial: One performance record {input, output, feedback, trajectory}
- trials: Collection of trial records for a component
| PARAMETER | DESCRIPTION |
|---|---|
candidate | Current candidate component values. TYPE: |
eval_batch | Evaluation results including trajectories and optional scorer metadata (e.g., from CriticScorer). TYPE: |
components_to_update | List of component names to generate trials for. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, Sequence[Mapping[str, Any]]] | Mapping from component name to sequence of trials. |
Mapping[str, Sequence[Mapping[str, Any]]] | Each trial contains input, output, feedback (with score and |
Mapping[str, Sequence[Mapping[str, Any]]] | feedback_text), and optional trajectory. |
Examples:
Generate trials for reflection:
result = await adapter.evaluate(batch, candidate, capture_traces=True)
trials_dataset = await adapter.make_reflective_dataset(
candidate, result, ["instruction"]
)
assert "instruction" in trials_dataset
# Each trial has structured feedback
trial = trials_dataset["instruction"][0]
assert "input" in trial
assert "output" in trial
assert "feedback" in trial
assert trial["feedback"]["score"] == 0.75
Note
Operates on eval_batch trajectories (capture_traces=True required). Dataset format is compatible with proposer's trial-based interface. Scorer metadata (feedback_text, feedback_dimensions) from eval_batch.metadata is included in each trial's feedback dict.
Source code in src/gepa_adk/adapters/adk_adapter.py
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
propose_new_texts async ¶
propose_new_texts(
candidate: dict[str, str],
reflective_dataset: Mapping[
str, Sequence[Mapping[str, Any]]
],
components_to_update: list[str],
) -> dict[str, str]
Propose new component texts based on trials.
Delegates to AsyncReflectiveMutationProposer to generate improved component text via LLM reflection on trials. When the proposer returns None (no trials), falls back to unchanged candidate values.
| PARAMETER | DESCRIPTION |
|---|---|
candidate | Current candidate component texts (name → text). TYPE: |
reflective_dataset | Trials from make_reflective_dataset(). Maps component name to list of trial records. TYPE: |
components_to_update | Components to generate proposals for. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, str] | Dictionary mapping component names to proposed component text. |
dict[str, str] | When proposer returns None, returns unchanged candidate values. |
Examples:
Using the proposer to generate improved component text:
# After evaluation with traces
result = await adapter.evaluate(batch, candidate, capture_traces=True)
trials = await adapter.make_reflective_dataset(
candidate, result, ["instruction"]
)
# Propose new component text via LLM reflection on trials
new_texts = await adapter.propose_new_texts(
candidate, trials, ["instruction"]
)
# new_texts["instruction"] contains proposed component text
Note
Delegates to AsyncReflectiveMutationProposer for actual mutation generation. Falls back gracefully when no trials available.
Source code in src/gepa_adk/adapters/adk_adapter.py
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | |