Reflection agents
reflection_agents ¶
Component-aware reflection agent factories and registry.
This module provides factory functions that create reflection agents configured for specific component types. Each factory returns an ADK LlmAgent with appropriate instructions and validation tools.
The component registry enables automatic selection of the right reflection agent based on the component name being evolved, with support for custom validators.
| ATTRIBUTE | DESCRIPTION |
|---|---|
SCHEMA_REFLECTION_INSTRUCTION | Instruction template for schema reflection agents with validation guidance. TYPE: |
ComponentReflectionRegistry | Registry class for mapping component names to reflection agent factories. TYPE: |
create_schema_reflection_agent | Factory function that creates schema reflection agents with validation tools. TYPE: |
create_text_reflection_agent | Factory function that creates text reflection agents without tools. TYPE: |
get_reflection_agent | Convenience function for auto-selecting reflection agents based on component name. TYPE: |
component_registry | Default global registry instance with built-in component mappings. |
Examples:
Create a schema reflection agent:
from gepa_adk.engine.reflection_agents import create_schema_reflection_agent
agent = create_schema_reflection_agent(model="gemini-2.5-flash")
# Agent has validate_output_schema tool and schema-focused instruction
Auto-select agent based on component name:
from gepa_adk.engine.reflection_agents import get_reflection_agent
# Returns schema agent with validation tool
agent = get_reflection_agent("output_schema", "gemini-2.5-flash")
# Returns text agent without tools
agent = get_reflection_agent("instruction", "gemini-2.5-flash")
Register custom validator:
from gepa_adk.engine.reflection_agents import component_registry
def my_custom_factory(model: str):
return LlmAgent(name="custom", model=model, tools=[...])
component_registry.register("my_component", my_custom_factory)
See Also
create_adk_reflection_fn: Creates reflection callable.validate_output_schema: Schema validation tool.
ReflectionAgentFactory module-attribute ¶
SCHEMA_REFLECTION_INSTRUCTION module-attribute ¶
SCHEMA_REFLECTION_INSTRUCTION = '## Component Text to Improve\n{component_text}\n\n## Trials\n{trials}\n\n## Instructions\nPropose an improved version of the Pydantic schema based on the trials above.\n\nIMPORTANT: Before returning your final answer, you MUST use the validate_output_schema\ntool to verify your proposed schema is syntactically valid. If validation fails, fix\nthe errors and validate again until the schema is valid.\n\nReturn ONLY the improved Pydantic class definition (starting with "class"), nothing else.\nDo not wrap in markdown code fences.'
Reflection instruction for output_schema components.
This instruction explicitly guides the LLM to use the validation tool before returning, enabling self-correction of syntax errors. Follows ADK's pattern for tool-guided structured output (similar to SetModelResponseTool).
The instruction uses ADK template placeholders ({component_text}, {trials}) for session state substitution.
CONFIG_REFLECTION_INSTRUCTION module-attribute ¶
CONFIG_REFLECTION_INSTRUCTION = "## Current LLM Generation Config (YAML)\n{component_text}\n\n## Trials\n{trials}\n\n## Instructions\nPropose improved LLM generation configuration parameters based on the trial results above.\n\n### Parameter Guidelines\n\n| Parameter | Range | When to Adjust |\n|-----------|-------|----------------|\n| `temperature` | 0.0 - 2.0 | Lower (0.0-0.3) for deterministic, higher (0.7-1.5) for creative |\n| `top_p` | 0.0 - 1.0 | Lower (0.5-0.8) for focused, higher (0.9-1.0) for diverse output |\n| `top_k` | > 0 | Lower (10-30) constrains vocab, higher (50-100) allows diversity |\n| `max_output_tokens` | > 0 | Adjust based on expected response length |\n| `presence_penalty` | -2.0 - 2.0 | Positive values discourage repeated topics |\n| `frequency_penalty` | -2.0 - 2.0 | Positive values discourage repeated tokens |\n\n### Analysis Steps\n1. Review trial scores and identify patterns (high-scoring configs vs low-scoring)\n2. Consider the task type (creative, analytical, conversational)\n3. Propose incremental adjustments to promising parameters\n4. Keep parameters within valid ranges\n\nReturn ONLY valid YAML configuration (parameter: value pairs), nothing else.\nDo not wrap in markdown code fences."
Reflection instruction for generate_content_config components.
This instruction guides the LLM to analyze trial results and propose optimal generation parameters. It includes parameter descriptions and valid ranges to help the reflection agent make informed decisions.
The instruction uses ADK template placeholders ({component_text}, {trials}) for session state substitution.
component_registry module-attribute ¶
component_registry = ComponentReflectionRegistry()
Default component reflection registry.
Pre-configured with: - output_schema → create_schema_reflection_agent - generate_content_config → create_config_reflection_agent - Default fallback → create_text_reflection_agent
Use this registry or create your own for custom configuration.
ComponentReflectionRegistry ¶
Registry mapping component names to reflection agent factories.
Enables automatic selection of the appropriate reflection agent based on component type, with support for custom validator registration.
The registry uses exact string matching for component names. Unknown components fall back to the default text reflection agent.
Examples:
Register custom factory:
from gepa_adk.engine.reflection_agents import ComponentReflectionRegistry
from google.adk.agents import LlmAgent
registry = ComponentReflectionRegistry()
def custom_factory(model: str) -> LlmAgent:
return LlmAgent(
name="custom_reflector",
model=model,
instruction="Custom: {component_text}",
)
registry.register("my_component", custom_factory)
agent = registry.get_agent("my_component", "gemini-2.5-flash")
See Also
- [
get_reflection_agent]: Convenience function using default registry.
Source code in src/gepa_adk/engine/reflection_agents.py
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 | |
__init__ ¶
Initialize empty registry with default text factory.
register ¶
register(
component_name: str, factory: ReflectionAgentFactory
) -> None
Register a factory for a component name.
| PARAMETER | DESCRIPTION |
|---|---|
component_name | Component name to register (e.g., "output_schema"). Uses exact match - no pattern matching for MVP. TYPE: |
factory | Factory function that takes model name and returns LlmAgent. TYPE: |
Examples:
Register schema factory:
Source code in src/gepa_adk/engine/reflection_agents.py
get_factory ¶
get_factory(component_name: str) -> ReflectionAgentFactory
Get factory for a component name.
| PARAMETER | DESCRIPTION |
|---|---|
component_name | Component name to look up. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ReflectionAgentFactory | Registered factory if found, otherwise default text factory. |
Examples:
Get factory:
Source code in src/gepa_adk/engine/reflection_agents.py
get_agent ¶
Create a reflection agent for a component.
Convenience method that gets the factory and invokes it.
| PARAMETER | DESCRIPTION |
|---|---|
component_name | Component name. TYPE: |
model | Model name/identifier. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
LlmAgent | Configured LlmAgent for the component type. |
Examples:
Get agent directly:
Source code in src/gepa_adk/engine/reflection_agents.py
create_text_reflection_agent ¶
Create a reflection agent for text components.
Creates an LlmAgent configured for free-form text reflection with no validation tools. Suitable for instructions, descriptions, and other text components that don't require structured validation.
| PARAMETER | DESCRIPTION |
|---|---|
model | Model name/identifier (e.g., "gemini-2.5-flash"). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
LlmAgent | Configured LlmAgent with text reflection instruction and no tools. |
Examples:
Create agent for instruction reflection:
from gepa_adk.engine.reflection_agents import create_text_reflection_agent
agent = create_text_reflection_agent(model="gemini-2.5-flash")
# Agent has:
# - name="text_reflector"
# - instruction with {component_text} and {trials} placeholders
# - output_key="proposed_component_text"
# - No tools
See Also
REFLECTION_INSTRUCTION: Default instruction template.
Source code in src/gepa_adk/engine/reflection_agents.py
create_schema_reflection_agent ¶
Create a reflection agent for output_schema components.
Creates an LlmAgent configured with: - Schema-focused reflection instruction - validate_output_schema tool for self-validation - Explicit instruction to use validation tool before returning
This agent can validate proposed Pydantic schemas before returning them, reducing wasted evolution iterations on invalid syntax.
| PARAMETER | DESCRIPTION |
|---|---|
model | Model name/identifier (e.g., "gemini-2.5-flash"). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
LlmAgent | Configured LlmAgent with schema validation tool. |
Examples:
Create agent for schema reflection:
from gepa_adk.engine.reflection_agents import create_schema_reflection_agent
agent = create_schema_reflection_agent(model="gemini-2.5-flash")
# Agent has:
# - name="schema_reflector"
# - tools=[FunctionTool(validate_output_schema)]
# - instruction with validation guidance
# - output_key="proposed_component_text"
See Also
- [
SCHEMA_REFLECTION_INSTRUCTION]: Schema instruction template. - [
validate_output_schema]: Validation tool function.
Note
The agent uses output_key for text output extraction, not output_schema. This avoids ADK's limitation where tools and output_schema cannot be used together. The agent returns plain text (the Pydantic class definition).
Source code in src/gepa_adk/engine/reflection_agents.py
create_config_reflection_agent ¶
Create a reflection agent for generate_content_config components.
Creates an LlmAgent configured for LLM generation configuration reflection. The agent receives parameter guidelines and valid ranges to help propose optimal configurations based on trial results.
| PARAMETER | DESCRIPTION |
|---|---|
model | Model name/identifier (e.g., "gemini-2.5-flash"). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
LlmAgent | Configured LlmAgent with config reflection instruction. |
Examples:
Create agent for config reflection:
from gepa_adk.engine.reflection_agents import create_config_reflection_agent
agent = create_config_reflection_agent(model="gemini-2.5-flash")
# Agent has:
# - name="config_reflector"
# - instruction with parameter guidelines
# - output_key="proposed_component_text"
# - No tools (validation happens in handler)
See Also
- [
CONFIG_REFLECTION_INSTRUCTION]: Config instruction template.
Note
The agent does not use validation tools because config validation is built into the GenerateContentConfigHandler.apply() method. Invalid configs are gracefully rejected there.
Source code in src/gepa_adk/engine/reflection_agents.py
get_reflection_agent ¶
Get appropriate reflection agent for a component.
Convenience function that uses the global registry to create an agent based on component name. Falls back to text reflection for unknown components.
| PARAMETER | DESCRIPTION |
|---|---|
component_name | Component being evolved (e.g., "output_schema"). TYPE: |
model | Model name/identifier. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
LlmAgent | Configured LlmAgent for the component type. |
Examples:
Auto-select agent:
from gepa_adk.engine.reflection_agents import get_reflection_agent
# Returns schema agent with validation tool
schema_agent = get_reflection_agent("output_schema", "gemini-2.5-flash")
# Returns text agent without tools
text_agent = get_reflection_agent("instruction", "gemini-2.5-flash")
# Unknown components get text agent (fallback)
fallback_agent = get_reflection_agent("custom", "gemini-2.5-flash")
See Also
- [
component_registry]: Default registry.