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 |
|---|---|
REFLECTION_INSTRUCTION | Default instruction template with TYPE: |
SESSION_STATE_KEYS | Expected keys and types in ADK session state for reflection agent access. TYPE: |
create_adk_reflection_fn | Factory that creates a ReflectionFn using an ADK LlmAgent for reflection. TYPE: |
Examples:
Create a reflection function with custom agent:
from google.adk.agents import LlmAgent
from gepa_adk.engine.adk_reflection import create_adk_reflection_fn
agent = LlmAgent(
name="reflector",
model="gemini-2.5-flash",
instruction="Improve: {component_text}\nTrials: {trials}",
)
reflection_fn = create_adk_reflection_fn(agent)
See Also
gepa_adk.engine.proposer: Proposer that uses reflection functions.
SESSION_STATE_KEYS module-attribute ¶
Expected keys and types in ADK session state for reflection.
The reflection agent accesses these keys via {key} template syntax in its instruction. ADK's inject_session_state() automatically substitutes placeholders with session state values.
Keys
component_text: The text content being evolved (str). trials: JSON-serialized list of trial records (str). Each trial contains {input, output, feedback, trajectory}.
REFLECTION_INSTRUCTION module-attribute ¶
REFLECTION_INSTRUCTION = "## Component Text to Improve\n{component_text}\n\n## Trials\n{trials}\n\nPropose an improved version of the component text based on the trials above.\nReturn ONLY the improved component text, nothing else."
Default instruction template for reflection agents.
Uses ADK's native template substitution syntax ({key}) to inject session state values. ADK automatically replaces these placeholders with values from session.state[key] during instruction processing.
The template contains two placeholders:
{component_text}: The current text being evolved (str){trials}: JSON-serialized list of trial records (str)
The instruction is processed by ADK's inject_session_state() function before being sent to the LLM.
Examples:
Use the default instruction with a custom agent:
from google.adk.agents import LlmAgent
from gepa_adk.engine.adk_reflection import REFLECTION_INSTRUCTION
agent = LlmAgent(
name="reflector",
model="gemini-2.5-flash",
instruction=REFLECTION_INSTRUCTION,
)
Note
This replaces the previous workaround of embedding data in user messages via Python f-strings.
create_adk_reflection_fn ¶
create_adk_reflection_fn(
reflection_agent: Any | None,
executor: AgentExecutorProtocol,
session_service: Any | None = None,
output_key: str = "proposed_component_text",
output_field: str | None = None,
component_name: str | None = None,
model: str | None = None,
) -> ReflectionFn
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.
Supports automatic agent selection based on component name when reflection_agent is None. Use this for component-aware reflection where different component types (e.g., output_schema vs instruction) require different validation tools and instructions.
| 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: |
session_service | Optional session service for state management. Defaults to InMemorySessionService if None. Use custom services (e.g., DatabaseSessionService) for production deployments requiring session persistence. 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: |
output_field | Optional field name to extract from structured output. When the reflection agent has an output_schema (Pydantic model), the output is stored as a dict in session state. This parameter specifies which field to extract from that dict. If None (default), the entire output is returned as a string. TYPE: |
component_name | Optional component name for automatic agent selection. When reflection_agent is None, this is used to select the appropriate reflection agent from the component registry. Examples: "output_schema", "instruction". If None and reflection_agent is None, raises ValueError. TYPE: |
model | Model name/identifier for automatic agent selection. Required when reflection_agent is None. Examples: "gemini-2.5-flash", "gemini-2.5-flash". Ignored when reflection_agent is provided. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ReflectionFn | Async callable matching ReflectionFn signature that generates proposed |
ReflectionFn | component text via the ADK agent. |
| RAISES | DESCRIPTION |
|---|---|
Exception | If ADK agent execution fails (propagated from ADK Runner). |
Examples:
Basic usage with executor:
from google.adk.agents import LlmAgent
from gepa_adk.adapters.agent_executor import AgentExecutor
from gepa_adk.engine.adk_reflection import create_adk_reflection_fn
agent = LlmAgent(
name="InstructionReflector",
model="gemini-2.5-flash",
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 = await reflection_fn("Be helpful", trials)
With output_schema for structured output (e.g., schema evolution):
from pydantic import BaseModel, Field
class SchemaProposal(BaseModel):
class_definition: str = Field(description="The Pydantic class definition")
reasoning: str = Field(description="Why this change was made")
agent = LlmAgent(
name="schema_reflector",
model="gemini-2.5-flash",
instruction="Improve the schema based on feedback...",
output_schema=SchemaProposal,
)
# Extract only the class_definition field from structured output
executor = AgentExecutor()
reflection_fn = create_adk_reflection_fn(
agent,
executor=executor,
output_field="class_definition",
)
See Also
gepa_adk.engine.proposer: Module containing ReflectionFn type alias and AsyncReflectiveMutationProposer class.
Note
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).
Source code in src/gepa_adk/engine/adk_reflection.py
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 | |