Agent executor
agent_executor ¶
Protocol and types for unified agent execution.
This module defines the AgentExecutorProtocol and supporting types for executing any ADK agent type (generator, critic, reflection) with consistent behavior, session management, and result handling.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ExecutionStatus | Status of agent execution (SUCCESS/FAILED/TIMEOUT), valued by the lowercase strings TYPE: |
ExecutionResult | Result of an agent execution. TYPE: |
RetryPolicy | Retry policy for transient session-service errors. TYPE: |
AgentExecutorProtocol | Protocol for unified agent execution. TYPE: |
Examples:
Using an executor:
from gepa_adk.ports.agent_executor import (
AgentExecutorProtocol,
ExecutionResult,
ExecutionStatus,
)
executor: AgentExecutorProtocol = AgentExecutor()
result = await executor.execute_agent(
agent=my_agent,
input_text="Hello",
timeout_seconds=60,
)
if result.status == ExecutionStatus.SUCCESS:
print(result.extracted_value)
See Also
gepa_adk.adapters.execution.agent_executor: AgentExecutor implementation.
Notes
This module follows hexagonal architecture principles. The protocol is defined here in the ports layer, while implementations live in the adapters layer.
ExecutionStatus ¶
Bases: str, Enum
flowchart TD
gepa_adk.ports.agent_executor.ExecutionStatus[ExecutionStatus]
click gepa_adk.ports.agent_executor.ExecutionStatus href "" "gepa_adk.ports.agent_executor.ExecutionStatus"
Status of agent execution.
This enum represents the outcome status of an agent execution, used by ExecutionResult to indicate how the execution completed. Members are str values ("success", "failed", "timeout"), so they compare equal to those strings.
| ATTRIBUTE | DESCRIPTION |
|---|---|
SUCCESS | Agent completed execution normally with valid output. TYPE: |
FAILED | Agent encountered an error during execution. TYPE: |
TIMEOUT | Agent execution exceeded configured timeout. TYPE: |
Examples:
Checking execution status:
from gepa_adk.ports.agent_executor import ExecutionStatus
if result.status == ExecutionStatus.SUCCESS:
print(f"Output: {result.extracted_value}")
elif result.status == ExecutionStatus.TIMEOUT:
print(f"Timed out after {result.execution_time_seconds}s")
elif result.status == ExecutionStatus.FAILED:
print(f"Error: {result.error_message}")
Notes
All status values reflect execution outcome, not output quality. A successful execution may still produce low-quality output that fails scoring.
Source code in src/gepa_adk/ports/agent_executor.py
ExecutionResult dataclass ¶
Result of an agent execution.
Provides consistent return type across all agent types (generator, critic, reflection) with status, output, timing, and debugging information.
| ATTRIBUTE | DESCRIPTION |
|---|---|
status | Outcome status of the execution. TYPE: |
session_id | ADK session identifier used for this execution. TYPE: |
extracted_value | Output text extracted from agent response. None if agent produced no output or execution failed. TYPE: |
error_message | Error details if status is FAILED or TIMEOUT. None for successful executions. TYPE: |
execution_time_seconds | Duration of execution in seconds. TYPE: |
captured_events | ADK events captured during execution for debugging and trajectory analysis. None if event capture disabled. TYPE: |
Examples:
Successful execution:
result = ExecutionResult(
status=ExecutionStatus.SUCCESS,
session_id="sess_123",
extracted_value="Hello, world!",
execution_time_seconds=1.5,
)
Failed execution:
result = ExecutionResult(
status=ExecutionStatus.FAILED,
session_id="sess_456",
error_message="Model rate limit exceeded",
execution_time_seconds=0.2,
)
Notes
All captured_events contain raw ADK Event objects for debugging and trajectory recording. Events are captured even on timeout or failure.
Source code in src/gepa_adk/ports/agent_executor.py
RetryPolicy dataclass ¶
Retry policy for transient session-service errors.
An executor that honours this policy retries an agent run whose session service raised a transient error, such as sqlite's database is locked, sleeping backoff_seconds before the second attempt and multiplying the sleep by backoff_multiplier before each later one.
| ATTRIBUTE | DESCRIPTION |
|---|---|
max_attempts | Total attempts including the first; TYPE: |
backoff_seconds | Sleep before the second attempt, in seconds. Defaults to 0.5. TYPE: |
backoff_multiplier | Factor applied to the sleep after each retry. Defaults to 2.0. TYPE: |
Examples:
The default policy tries three times, sleeping 0.5 s then 1.0 s:
from gepa_adk.ports.agent_executor import RetryPolicy
policy = RetryPolicy()
assert policy.max_attempts == 3
A stricter policy for a heavily loaded disk:
policy = RetryPolicy(max_attempts=5, backoff_seconds=1.0)
executor = AgentExecutor(session_service=service, retry_policy=policy)
Notes
The policy is a frozen value object. Validation runs in __post_init__ and raises ValueError naming the bad field.
Source code in src/gepa_adk/ports/agent_executor.py
__post_init__ ¶
Validate the policy fields.
| RAISES | DESCRIPTION |
|---|---|
ValueError | If |
Source code in src/gepa_adk/ports/agent_executor.py
AgentExecutorProtocol ¶
Bases: Protocol
flowchart TD
gepa_adk.ports.agent_executor.AgentExecutorProtocol[AgentExecutorProtocol]
click gepa_adk.ports.agent_executor.AgentExecutorProtocol href "" "gepa_adk.ports.agent_executor.AgentExecutorProtocol"
Protocol for unified agent execution.
Defines the interface for executing any ADK agent type (LlmAgent, workflow agents) with consistent behavior, session management, and result handling.
This protocol enables: - Feature parity across generator, critic, and reflection agents - Dependency injection for testing - Single point of change for new ADK features
Examples:
Using an executor:
executor: AgentExecutorProtocol = AgentExecutor()
result = await executor.execute_agent(
agent=my_agent,
input_text="Hello",
timeout_seconds=60,
)
if result.status == ExecutionStatus.SUCCESS:
print(result.extracted_value)
With instruction override (for evolution):
result = await executor.execute_agent(
agent=my_agent,
input_text="Hello",
instruction_override="You are a helpful assistant.",
)
See Also
ExecutionResult: Return type for execute_agent.ExecutionStatus: Status enum for execution outcomes.
Notes
All implementations should validate that the agent parameter is a valid ADK LlmAgent. The Any type is used here to avoid coupling the ports layer to ADK types.
Source code in src/gepa_adk/ports/agent_executor.py
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 | |
execute_agent async ¶
execute_agent(
agent: Any,
input_text: str,
*,
input_content: Any | None = None,
instruction_override: str | None = None,
output_schema_override: Any | None = None,
session_state: dict[str, Any] | None = None,
existing_session_id: str | None = None,
timeout_seconds: int = 300,
) -> ExecutionResult
Execute an agent and return structured result.
Runs the specified agent with the given input, optionally applying instruction or schema overrides for evolution scenarios. Manages session lifecycle and captures execution events.
| PARAMETER | DESCRIPTION |
|---|---|
agent | ADK LlmAgent to execute. The agent's tools, output_key, and other ADK features are preserved during execution. TYPE: |
input_text | User message to send to the agent. Used as fallback if input_content is not provided. TYPE: |
input_content | Optional ADK Content object for multimodal input (e.g., video parts). If provided, takes precedence over input_text. TYPE: |
instruction_override | If provided, replaces the agent's instruction for this execution only. Original agent is not modified. TYPE: |
output_schema_override | If provided, replaces the agent's output schema for this execution only (type[BaseModel]). Used for schema evolution. TYPE: |
session_state | Initial state to inject into the session. Used for template variable substitution (e.g., {component_text}). TYPE: |
existing_session_id | If provided, reuses an existing session instead of creating a new one. Useful for critic accessing generator state. TYPE: |
timeout_seconds | Maximum execution time in seconds. Defaults to 300. Execution terminates with TIMEOUT status if exceeded. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ExecutionResult | ExecutionResult with status, output, and debugging information. |
| RAISES | DESCRIPTION |
|---|---|
SessionNotFoundError | If existing_session_id is provided but session does not exist. |
Examples:
Basic execution:
result = await executor.execute_agent(
agent=greeter,
input_text="Hello!",
)
print(result.extracted_value) # "Hello! How can I help?"
With session state for reflection:
result = await executor.execute_agent(
agent=reflector,
input_text="Improve the instruction",
session_state={
"component_text": "Be helpful.",
"trials": '[{"score": 0.5}]',
},
)
Session sharing between agents:
# Generator creates session
gen_result = await executor.execute_agent(
agent=generator,
input_text="Write a story.",
)
# Critic reuses generator's session
critic_result = await executor.execute_agent(
agent=critic,
input_text=f"Evaluate: {gen_result.extracted_value}",
existing_session_id=gen_result.session_id,
)
Notes
The agent parameter is typed as Any to avoid coupling to ADK types in the ports layer. Implementations should validate that the agent is a valid LlmAgent.
Source code in src/gepa_adk/ports/agent_executor.py
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 | |