Adk reflection
adk_reflection ¶
ADK-based reflection function factory.
This module provides the factory function for creating reflection functions that use Google ADK agents. The returned function can be passed to AsyncReflectiveMutationProposer as the adk_reflection_fn parameter.
Terminology
- component_text: The current text content of a component being evolved
- trial: One performance record {feedback, trajectory}
- feedback: Critic evaluation {score, feedback_text, feedback_*} (stochastic)
- trajectory: Execution record {input, output, trace} (deterministic)
- trials: Collection of trial records for reflection
- proposed_component_text: The improved text for the same component
| ATTRIBUTE | DESCRIPTION |
|---|---|
create_adk_reflection_fn | Factory that creates a ReflectionFn using an ADK LlmAgent for reflection. The returned function produces TYPE: |
is_length_stop | Whether a finish reason (ADK enum, its name in any case, or litellm's TYPE: |
ADKReflectionFn | Call signature of the function that TYPE: |
Examples:
Create a reflection function with custom agent:
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
from gepa_adk.engine.adk_reflection import create_adk_reflection_fn
from gepa_adk.adapters.execution.agent_executor import AgentExecutor
agent = LlmAgent(
name="reflector",
model=LiteLlm(model="ollama_chat/gpt-oss:20b"),
instruction="Improve: {component_text}\nTrials: {trials}",
)
executor = AgentExecutor()
reflection_fn = create_adk_reflection_fn(agent, executor=executor)
proposed, reasoning, usage = await reflection_fn("Be helpful", [], "instruction")
See Also
gepa_adk.engine.proposer: Proposer that uses reflection functions.AgentExecutorProtocol: Executor that runs the reflection agent in a fresh session per call.
ADKReflectionFn ¶
Bases: Protocol
flowchart TD
gepa_adk.engine.adk_reflection.ADKReflectionFn[ADKReflectionFn]
click gepa_adk.engine.adk_reflection.ADKReflectionFn href "" "gepa_adk.engine.adk_reflection.ADKReflectionFn"
Call signature of the reflection function built from an ADK agent.
It matches ReflectionFn positionally, so the proposer accepts it, and also names its third parameter so callers may pass component_name by keyword or omit it. It may return (text, reasoning) or (text, reasoning, token_usage); the proposer counts a 2-tuple, or a None token_usage, as a reflection call of unknown usage.
Examples:
reflect: ADKReflectionFn = create_adk_reflection_fn(agent, executor=executor)
proposed, reasoning, usage = await reflect(
"Be helpful", [], component_name="instruction"
)
Source code in src/gepa_adk/engine/adk_reflection.py
__call__ async ¶
__call__(
component_text: str,
trials: list[dict[str, Any]],
component_name: str = "",
) -> (
tuple[str, str | None]
| tuple[str, str | None, TokenUsage | None]
)
Propose improved text for one component.
| PARAMETER | DESCRIPTION |
|---|---|
component_text | The current component text to improve. TYPE: |
trials | Trial records from evaluation. TYPE: |
component_name | Name of the component being evolved. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, str | None] | tuple[str, str | None, TokenUsage | None] | Tuple of (proposed_component_text, reasoning), optionally |
tuple[str, str | None] | tuple[str, str | None, TokenUsage | None] | followed by the call's |
Source code in src/gepa_adk/engine/adk_reflection.py
is_length_stop ¶
Report whether a finish reason marks output cut off at the token limit.
| PARAMETER | DESCRIPTION |
|---|---|
finish_reason | A TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | True when the reason is |
bool | False for |
Examples:
from google.genai import types
is_length_stop(types.FinishReason.MAX_TOKENS) # True
is_length_stop("length") # True
is_length_stop("STOP") # False
is_length_stop(None) # False
Source code in src/gepa_adk/engine/adk_reflection.py
create_adk_reflection_fn ¶
create_adk_reflection_fn(
reflection_agent: Any,
executor: AgentExecutorProtocol,
output_key: str = "proposed_component_text",
timeout_seconds: int | None = None,
reasoning_tags: Sequence[str] = _DEFAULT_REASONING_TAGS,
) -> ADKReflectionFn
Create a reflection function from an ADK LlmAgent.
This factory function creates an async callable that uses the Google ADK framework for reflection. The returned function can be passed to AsyncReflectiveMutationProposer as the adk_reflection_fn parameter.
The caller is responsible for selecting the appropriate reflection agent. See gepa_adk.api.evolve() for the standard wiring pattern.
| PARAMETER | DESCRIPTION |
|---|---|
reflection_agent | ADK LlmAgent configured with instruction containing TYPE: |
executor | AgentExecutorProtocol implementation for unified agent execution. Handles session management and execution, enabling feature parity across all agent types. TYPE: |
output_key | Key in session state where ADK stores the agent's output. Defaults to "proposed_component_text". When set, the agent's output_key is configured to this value, and output is retrieved from session state after execution. Falls back to event-based extraction if the output_key is not found in session state. TYPE: |
timeout_seconds | Seconds the reflection agent may run per call. Passed to TYPE: |
reasoning_tags | Reasoning tag names (without angle brackets) that mark an incomplete proposal when the stripped text opens with one of them and never closes it. Matched case-insensitively. Defaults to TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ADKReflectionFn | Async callable matching ReflectionFn signature that generates proposed |
ADKReflectionFn | component text via the ADK agent and returns |
ADKReflectionFn |
|
ADKReflectionFn |
|
ADKReflectionFn | passed by keyword. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError | If ADK agent execution fails (propagated from executor). |
ReflectionTimeoutError | From the returned callable, when the executor reports |
IncompleteProposalError | From the returned callable, when the last captured event with a finish reason reports a length stop, or the proposal opens one of |
Examples:
Basic usage with executor:
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
from gepa_adk.adapters.execution.agent_executor import AgentExecutor
from gepa_adk.engine.adk_reflection import create_adk_reflection_fn
agent = LlmAgent(
name="InstructionReflector",
model=LiteLlm(model="ollama_chat/gpt-oss:20b"),
instruction="""Improve this component text:
{component_text}
Based on these trials:
{trials}
Return proposed component text only."""
)
executor = AgentExecutor()
reflection_fn = create_adk_reflection_fn(agent, executor=executor)
trials = [{"input": "Hi", "output": "Hey", "feedback": {"score": 0.5}}]
proposed, reasoning, usage = await reflection_fn(
"Be helpful", trials, "instruction"
)
See Also
gepa_adk.engine.proposer: Module containing ReflectionFn type alias and AsyncReflectiveMutationProposer class.gepa_adk.api.evolve: Standard wiring pattern for constructing the reflection chain.
Notes
Opens a fresh ADK session for each invocation via AgentExecutor, ensuring complete isolation between reflection operations. State is initialized with component_text (str) and trials (JSON-serialized list of trial records). A timed-out execution logs reflection.timeout and raises ReflectionTimeoutError; reflection.empty_response is logged only for a completed run that produced no text. An incomplete proposal logs reflection.incomplete at warning and raises IncompleteProposalError; the length check runs before the empty-response path, so a length stop with no text is incomplete.
Source code in src/gepa_adk/engine/adk_reflection.py
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 | |